7. Logical expressions

Logical expressions can only have the value .TRUE. or .FALSE.. A logical expression can be formed by comparing arithmetic expressions using the following relational operators:

      .LT.  meaning <
      .LE.          <=
      .GT.          >
      .GE.          >=
      .EQ.          =
      .NE.          /=

So you cannot use symbols like < or = for comparison in FORTRAN 77, you have to use the correct two-letter abbreviation enclosed by dots.

Logical expressions can be combined by the logical operators .AND. .OR. and .NOT. which have the obvious meanings.

Logical variables and assignment

Truth values can be stored in logical variables. The assignment is analogous to the arithmetic assignment. For example:

      logical a, b
      a = .TRUE.
      b = a .AND. 3 .LT. 5/2

The order of precedence is important, as the last example shows. The rule is that arithmetic expressions are evaluated first, then relational operators, and finally logical operators. Hence b will be assigned .FALSE. in the example above. Among the logical operators the precedence (in the absence of parenthesis) is that .NOT. is done first, then .AND., then .OR. is done last.

While logical variables are seldom used in Fortran, logical expressions are frequently used in conditional statements such as the if statement.

Copyright © 1995-7 by Stanford University. All rights reserved.

[ Index ]