For expression evaluation in programming languages, we termed this specially as **Short Circuit Evaluation**. It evaluates a expression from left to right and stops as soon as the result can be determined. So without evaluating all of the operands and/or operators, result is determined.

For example, the value of the arithmetic expression

(13 * a) * (b / 13 – 1)

is independent of the value of (b / 13 – 1) if **a** is 0. Because 0 * x = 0 for any x. So, when **a** is 0, there is no need to evaluate (b / 13 – 1) or perform the second multiplication.

However, in arithmetic expressions on programming languages, this shortcut is not easily detected during execution, so it is never taken.

### Boolean Expressions Evaluation

The value of the Boolean expression

(a >= 0) && (b < 10)

is independent of the second relational expression if a < 0, because the expression (FALSE && (b < 10)) is FALSE for all values of b. So, when a<0, there is no need to evaluate b, the constant 10, the second relational expression, or the && operation. Unlike the case of arithmetic expressions, this shortcut can be easily discovered during execution.

To illustrate a potential problem with non-short-circuit evaluation of Boolean expressions, suppose Java Language did not use short-circuit evaluation. And now look at the following Java code segment.

[code languague=”java”] index = 0;while ((index < listlen) && (list[index] != key)){ index = index + 1; } [/code]

The variable **list** is an array and the length of this array defined by **listlen** variable. This is a Java code for a lookup, assuming that list, which has listlen elements to be lookup for the value in the **key **variable.

If evaluation is not short-circuit, both relational expressions in the Boolean expression of the while statement are evaluated, regardless of the value of the first. Thus, if key is not in list, the program will terminate with a subscript out-of-range exception. The same iteration that has** index == listlen** will reference **list[listlen]**, which causes the indexing error because list is declared to have **listlen-1** as an upper-bound subscript value.

If a language provides short-circuit evaluation of Boolean expressions and it is used, this is not a problem. In the preceding example, a short-circuit evaluation scheme would evaluate the first operand of the AND operator, but it would skip the second operand if the first operand is false.

A language that provides short-circuit evaluations of Boolean expressions and also has side effects in expressions allows subtle errors to occur. Suppose that short-circuit evaluation is used on an expression and part of the expression that contains a side effect is not evaluated; then the side effect will occur only in complete evaluations of the whole expression. If program correctness depends on the side effect, short-circuit evaluation can result in a serious error.

For example, consider the Java expression

(a > b) || ((b++) / 3)

In this expression, b is changed (in the second arithmetic expression) only when a <= b. If the programmer assumed b would be changed every time this expression is evaluated during execution (and the program’s correctness depends on it), the program will fail.

So before you write your conditional statements, think before and write. Best way is to structure them, thinking how it effects on the whole code.

In the C-based languages, the usual AND and OR operators, && and ||, respectively, are short-circuit. All of the logical operators of Ruby, Perl, ML, F#, and Python are shortcircuit evaluated.

If we summarized the points we mentioned about Boolean AND (&&) and OR (||)

Expression1 && Expression2

If Expression1 is FALSE, then no further computation is done. Which means machine do not care hell of a thing what happened on Expression2.

But if Expression1 is TRUE, then we have to compute the result of Expression2.

Expression1 || Expression2

If Expression1 is TRUE, then no further computation is done. Which means machine do not care hell of a thing what happened on Expression2.

But if Expression1 is FALSE, then we have to compute the result of Expression2.

## Leave a Reply

Be the First to Comment!