Complex Logical Operations

From BR Wiki
Jump to navigation Jump to search

The parts of an expression containing AND or OR operators are evaluated only until it’s known whether the condition is true or false. Thus, evaluation of the expression

sex == "M" AND age >= 65

stops immediately if sex is not equal to "M" (i.e., at that point, it’s certain that the entire expression is false) and continues if gender is equal to "M" (i.e., the entire expression could still be true if the condition age >= 65 is true). This feature of conditional AND and conditional OR expressions is called short-circuit evaluation.

Common Error in Complex Logical Operations

In expressions using operator AND, a condition—which we refer to as the dependent condition— may require another condition to be true for the evaluation of the dependent condition to be meaningful. In this case, the dependent condition should be placed after the other condition, or an error might occur.

For example, in the expression

x <> 0 AND 4 / x == 1

the second condition must appear after the first condition, or a divide-by-zero error might occur.