当前位置: 首页 > 文档资料 > Fortran 中文教程 >

Show 例子 3

优质
小牛编辑
118浏览
2023-12-01

下表显示了Fortran支持的所有逻辑运算符。 假设变量A保持.true。 变量B保持.false。 那么 -

操作者描述
.and.称为逻辑AND运算符。 如果两个操作数都不为零,则条件成立。(A。和.B)是假的。
.or.称为逻辑OR运算符。 如果两个操作数中的任何一个非零,则条件变为真。(A。或.B)是真的。
.not.称为逻辑非运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则Logical NOT运算符将为false。!(A。和。B)是真的。
.eqv.称为逻辑等效运算符。 用于检查两个逻辑值的等效性。(A .eqv.B)是假的。
.neqv.称为逻辑非等价运算符。 用于检查两个逻辑值的非等价性。(A .neqv.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