



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:
- 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:
- arithmetical operators (in the order defined in section 6.1 above).
- relational operators
-
.NOT.
-
.AND.
-
.OR.
-
.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.



