Shortcut Evaluation
Shortcut evaluation of an expression is a computer optimization. For example, if you know three things must all be true in order to proceed, you do not need to check conditions 2 and 3 if condition one is already known to be false. (See Logic)
Shortcut Evaluation
In most languages we use the && notation for AND and the || notation for OR. The interesting thing about these operations is that they behave in what is known as "short cut" evaluation.
Assume we have three boolean expressions, A, B, and C. If we put them in a conditional statement using &&, we get:
We know that the only way this expression can be true is if A and B and C are all true. BUT, if A is false, we don't even need to consider B and C because we already know the entire expression is false.
Short-circuit evaluation means that as soon as the program can determine that the expression is false, no further evaluation takes place.
The OR version is similar to AND; OR can also terminate early. For example, if (A || B || C) {do something;}. What can the computer do if A is known to be true? Does the value of B or even C matter at that point?