目录

Show 例子

下表显示了Fortran支持的所有关系运算符。 假设变量A保持10,变量B保持20,则 -

操作者 当量 描述
==.eq. 检查两个操作数的值是否相等,如果是,则条件变为真。 (A == B)不是真的。
/=.ne. 检查两个操作数的值是否相等,如果值不相等则条件变为真。 (A!= B)是真的。
>.gt. 检查左操作数的值是否大于右操作数的值,如果是,则条件变为真。 (A> B)不是真的。
<.lt. 检查左操作数的值是否小于右操作数的值,如果是,则条件变为真。 (A < B) 为真
>=.ge. 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件变为真。 (A> = B)不是真的。
<=.le. 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件变为真。 (A <= B)是真的。

例子 (Example)

尝试以下示例以了解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
↑回到顶部↑
WIKI教程 @2018