Logical Expressions

Logical expressions combine the results of Boolean expressions, such as comparisons, to construct complex conditional expressions. The forms of a <logical_expr> are as follows:

<logical_operand> or <logical_operand> -- true if either operand is true

<logical_operand> and <logical_operand> -- true if both operands are true

[ not ] <logical_operand> -- true if operand is false

where <logical_operand> is one of the following:





Logical operators only operate on Boolean values, so each <logical_operand> must evaluate to true or false.


a > 0 and a < n + 1

not x and y

a and b or c and d and not e

As with math expressions, <logical_expr> is a recursive definition, meaning a logical operand can be another logical expression. You can have arbitrary sequences containing and, or, and not, and the order of evaluation is defined by the following precedence ordering (in decreasing order of precedence):

not -- right associative

and -- left associative

or -- left associative

This means that in an unparenthesised logical expression, not is evaluated before and which is evaluated before or.

Logical operators have a lower precedence than comparison operators, math, and function calls. Therefore, these operations are always evaluated before logical operators, which follows convention. The previous examples would be evaluated in the following order:

(a > 0) and (a < (n + 1))

(not x) and y

(a and b) or ((c and d) and (not e))

See also

Short-Circuiting Logical Expressions