GENERATE/PREV.gifGENERATE/NEXT.gif

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:

<log_operand> or <log_operand>

<log_operand> and <log_operand>

[ not ] <log_operand>

where <log_operand> is one of:

<operand>

<compare_expr>

<function_call>

<logical_expr>

Logical operators only operate on Boolean values, so the <log_operand>'s must all evaluate to true or false.

Examples:

a > 0 and a < n + 1

not x or y

a and b or c and d and not e

As with math expressions, <logical_expr> is a recursive definition, meaning that 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

and      -- left associative

or      -- left associative

This means that in an un-parenthesised 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, so these operations are always evaluated before logical operators, which follows convention. The above examples would be evaluated in the following order:

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

not (x or y)

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

See also:

Short-Circuiting Logical Expressions