下表列出了所有的Fortran支持的关系运算符。假设变量A=10和变量B=20,则:
操作符 | 等同 | 描述 | 示例 |
---|---|---|---|
== | .eq. | 检查两个操作数的值相等与否,如果是,则条件变为真。 | (A == B) i不为 true. |
/= | .ne. | 检查,两个操作数的值相等与否,如果值不相等,则条件变为真。 | (A != B) 为 true. |
> | .gt. | 检查,左操作数的值大于右操作数的值,如果是的话那么条件为真。 | (A > B)不为true. |
< | .lt. | 检查,左操作数的值是否小于右操作数的值,如果是的话那么条件为真。 | (A < B) 是 true. |
>= | .ge. | 检查,左边的操作数的值是否大于或等于右操作数的值,如果是,则条件变为真。 | (A >= B) 不为 true. |
<= | .le. | 检查,左边的操作数的值是否小于或等于右操作数的值,如果是,则条件变为真。 | (A <= B) 为 true. |
示例
试试下面的例子就明白所有在Fortran语言可用的逻辑运算符:
program logicalOp ! this program checks logical operators implicit none ! variable declaration logical :: a, b ! assigning values a = .true. b = .false. if (a .and. b) then print *, "Line 1 - Condition is true" else print *, "Line 1 - Condition is false" end if if (a .or. b) then print *, "Line 2 - Condition is true" else print *, "Line 2 - Condition is false" end if ! changing values a = .false. b = .true. if (.not.(a .and. b)) then print *, "Line 3 - Condition is true" else print *, "Line 3 - Condition is false" end if if (b .neqv. a) then print *, "Line 4 - Condition is true" else print *, "Line 4 - Condition is false" end if if (b .eqv. a) then print *, "Line 5 - Condition is true" else print *, "Line 5 - Condition is false" end if end program logicalOp
当编译并执行上述程序,将产生以下结果:
Line 1 - Condition is false Line 2 - Condition is true Line 3 - Condition is true Line 4 - Condition is true Line 5 - Condition is false