No, it does not evaluate the expression after learning that the answer is going to be NO
. This is called short-circuiting, and it is an essential part of evaluating boolean expressions in C, C++, Objective C, and other languages with similar syntax. The conditions are evaluated left to right, making the evaluation scheme predictable.
The same rule applies to the ||
operator: as soon as the code knows that the value is YES
, the evaluation stops.
Short-circuiting lets you guard against invalid evaluation in a single composite expression, rather than opting for an if
statement. For example,
if (index >= 0 && index < Length && array[index] == 42)
would have resulted in undefined behavior if it were not for short-circuiting. But since the evaluation skips evaluation of array[index]
when index
is invalid, the above expression is legal.