Short-Circuit Evaluation
Let me go off on a tangent here and go over a topic that is fairly important when evaluating
conditional statements.
All C-based languages support something called short-circuit evaluation. This is a very
helpful performance tool, but it can cause some problems for you if you want to perform
some fancy code tricks.
If you know your binary math rules, then you know that with an and statement, if either
one of the operands is false, then the entire thing is false. Table 2.7 lists the logical and and
logical or result tables.
Look at the table, specifically the two lines where x is false. For the operation “x and y,” it
doesn’t matter what y is because the result is always going to be false. Likewise, if you look
at the first two lines, you’ll notice that whenever x is true, the operation “x or y” is true,
no matter what y is.