2.3.4 Logical Expressions

A logical expression is a single logical element or a combination of logical elements and logical, arithmetic, or relational operators.

Logical elements can be any one of the following:

The logical operators are as follows:

Operator  Example  Meaning 
.AND.  A .AND. B       Logical conjunction: The expression is true if both A and B are true. 
.OR.  A .OR. B       Logical disjunction (inclusive OR): The expression is true if either A or B, or both, are true. 
.NEQV.  A .NEQV. B   Logical exclusive OR: The expression is true if A and B have different logical values; but the expression is false if both elements have the same logical value. 
.XOR.       A .XOR. B   Same as .NEQV. 
.EQV.  A .EQV. B       Logical equivalence: The expression is true if both A and B have the same logical value, whether true or false. 
.NOT.  .NOT. A       Logical negation: The expression is true if A is false and false if A is true. 

Delimiting periods are required. Periods cannot appear consecutively except when the second operator is .NOT. For example, the following logical expression is valid:

A+B/(A-1) .AND. .NOT. D+B/(D-1)

Data Types that Result from Logical Operations

On logical elements, logical operations produce single logical values (.TRUE. or .FALSE.) with a logical data type.

On integers, logical operations produce single values with an integer data type; they are carried out bit-by-bit on corresponding bits of the internal (binary) representation of the integer elements.

On a combination of integer and logical values, logical operations also produce single values with an integer data type. The operation first converts logical values to integers and then operates as it does with integers.

Logical operations cannot be performed on other data types.

Evaluation of Logical Expressions

Logical expressions are evaluated according to the precedence of their operators. Consider the following expression:

A*B+C*ABC .EQ. X*Y+DM/ZZ .AND. .NOT. K*B .GT. TT

This expression is evaluated in the following sequence:

(((A*B)+(C*ABC)) .EQ. ((X*Y)+(DM/ZZ))) .AND. (.NOT. ((K*B). GT. TT))

The following list shows all the operators that can appear in a logical expression in the order of their precedence:

Operator  Precedence 
**  First (highest) 
*, /  Second 
+, -, //  Third 
Relational Operators  Fourth 
.NOT.  Fifth 
.AND.  Sixth 
.OR.  Seventh 
.XOR., .EQV., .NEQV.   Eighth (lowest) 

As with arithmetic expressions, you can use parentheses to alter the sequence of evaluation.

When operators have equal precedence, the compiler can evaluate them in any order, as long as the result is the same as the result gained by the algebraic left-to-right order of evaluation (except for exponentiation, which is associated from right to left).

You should not write logical expressions whose results might depend on the evaluation order of subexpressions. The compiler is free to evaluate subexpressions in any order. In the following example, either (A(I)+1.0) or B(I)*2.0 could be evaluated first:

(A(I)+1.0) .GT. B(I)*2.0

Some subexpressions may not be evaluated if the compiler can determine the result by testing other subexpressions in the logical expression. Consider the following expression:

A .AND. (F(X,Y) .GT. 2.0) .AND. B

If A is false, and if the compiler evaluates A first, then the compiler can determine that the expression is false and may not call the subprogram F(X,Y).


Previous Page Next Page Table of Contents