SuivantPrec.Bas prec.BasNiv. sup.

7.7 Logical Expressions 

Logical expressions can be used in logical assignment statements, but are most commonly encountered in IF statements where there is a compound condition, for example:
        IF(AGE .GE. 60 .OR. (STATUS .EQ. 'WIDOW' .AND.
      $   NCHILD .GT. 0) THEN
This combines the values of three relational expressions, two of them comparing arithmetic values, the other character values. The logical operators such as .AND. and .OR. also need decimal points at either end to distinguish them from symbolic names. The .OR. operator performs an inclusive or, the exclusive or operator is called .NEQV..

Rules 

A logical expression can have any of the following forms:

Where: logical-term can be any of the following: and the logical operator can be any of the following:
.AND. logical and
.OR. logical inclusive or
.EQV. logical equivalence
.NEQV. logical non-equivalence (i.e. exclusive or).
Note that the rules of logical expressions only allow two successive operators to occur if the second of them is the unary operator .NOT. which negates the value of its operand. The effects of the four binary logical operators are shown in the table below for the four possible combinations of operands, x and y.
x y x .AND. yx .OR. yx .EQV. yx .NEQV. y
falsefalse false false true false
truefalse false true false true
falsetrue false true false true
truetrue true true true false
Note that a logical expression can have operands which are complete relational expressions, and these can in turn contain arithmetic expressions. The complete order of precedence of the operators in a general expression is as follows:
  1.  arithmetical operators (in the order defined in section 6.1 above).
  2.  relational operators
  3.  .NOT.
  4.  .AND.
  5.  .OR.
  6.  .EQV. and .NEQV.
If the operators .EQV. and .NEQV. are used at the same level in an expression they are evaluated from left to right.

These rules reduce the need for parentheses in logical expressions, thus:       (X .GT. A) .OR. (Y .GT. B) would have exactly the same meaning if all the parentheses had been omitted.

A Fortran system is not required to evaluate every term in a logical expression completely if its value can be determined more simply. In the above example, if X had been greater than A then it would not be necessary to compare Y and B for the expression would have been true in either case. This improves efficiency but means that functions with side-effects should not be used.

Guidelines 

Complicated logical and relational expressions can be hard to read especially if they extend on to several successive lines. It helps to line up similar conditions on successive lines, and to use parentheses.

SuivantPrec.Bas prec.HautNiv. sup.