Next: Guidelines Up: Logical Expressions Previous: Logical Expressions

### Rules

A logical expression can have any of the following forms:

• logical-term
• .NOT. logical-term
• logical-expression   logical-operator    logical-term
Where: logical-term can be any of the following:
• logical constant (literal or named),
• logical variable,
• logical array element,
• logical function reference,
• logical expression enclosed in parentheses,
• relational expression.

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. y x .OR. y x .EQV. y x .NEQV. y false false false false true false true false false true false true false true false true false true true true 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.

Next: Guidelines Up: Logical Expressions Previous: Logical Expressions
Helen Rowlands
8/27/1998