Fortran - Relational Operators



Following table shows all the relational operators supported by Fortran. Assume variable A holds 10 and variable B holds 20, then −

Operator Equivalent Description Example
== .eq. Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
/= .ne. Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> .gt. Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< .lt. Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= .ge. Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= .le. Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Example

Try the following example to understand all the logical operators available in Fortran −

! this program checks relational operators
implicit none  

   ! variable declaration
   integer :: a, b
   
   ! assigning values 
   a = 10   
   b = 20
   
   if (a .eq. b) then
      print *, "Line 1 - a is equal to b"
   else
      print *, "Line 1 - a is not equal to b"
   end if

   if (a > b) then
      print *, "Line 2 - a is greater than b"
   else
      print *, "Line 2 - a is less than b"
   end if
   
   if (a <= b) then
      print *, "Line 3 - a is less than or equal to b"
   else
      print *, "Line 3 - a is greater than b"
   end if
   
   a = 20   
   b = 20
   
   if (a .eq. b) then
      print *, "Line 4 - a is equal to b"
   else
      print *, "Line 4 - a is not equal to b"
   end if
end

When you compile and execute the above program it produces the following result −

Line 1 - a is not equal to b
Line 2 - a is less than b
Line 3 - a is less than or equal to b
Line 4 - a is equal to b
fortran_operators.htm
Advertisements