位置:首页 > 高级语言 > Fortran在线教程 > Fortran逻辑运算符

Fortran逻辑运算符

试试下面的例子就明白所有在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