# Logical Expressions

A logical expression consists of one or more logical operators and logical, numeric, or relational operands. The following are logical operators:

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, B, or both, are true.
.NEQV. | `A .NEQV. B` | Logical inequivalence (exclusive OR): the expression is true if either A or B is true, but false if both are true.
.XOR. | `A .XOR. B` | Same as .NEQV.
.EQV. | `A .EQV. B` | Logical equivalence: the expression is true if both A and B are true, or both are false.
.NOT.1 | `.NOT. A` | Logical negation: the expression is true if A is false and false if A is true.
1 .NOT. is a unary operator.

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 Resulting from Logical Operations

Logical operations on logical operands produce single logical values (.TRUE. or .FALSE.) of logical type.

Logical operations on integers produce single values of integer type. The operation is carried out bit-by-bit on corresponding bits of the internal (binary) representation of the integer operands.

Logical operations on a combination of integer and logical values also produce single values of integer type. The operation first converts logical values to integers, 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 == X*Y+DM/ZZ .AND. .NOT. K*B > TT

This expression is evaluated in the following sequence:

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

As with numeric 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 evaluated 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 might 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 the compiler evaluates A first, and A is false, the compiler might determine that the expression is false and might not call the subprogram F(X,Y).

Parent topic: Expressions

## See Also

Summary of Operator Precedence