This is a Short Circuit

In the previous part, we saw the operators for doing Boolean AND, OR and NOT. In this part, we're going to look at one particular aspect of these operators. That feature is called short-circuiting. The term short circuiting means that we don't evaluate the second part of an AND or OR unless we really need to. So for example, if we have an expression like:

if( a < b && c < d )

we don't bother evaluating the c < d part if a is not less than b since it doesn't matter what c and d's relationship are; the condition is false no matter what. Similarly if we have

if( a < b || c < d )

we don't need to evaluate c < d if a is less than b since the OR will be true regardless of what c and d are.

The importance of this feature is not just that it avoids unnecessary computation. The main thing is that if the second part of such an expression has side effects, they won't always take place. For example,

if( a < b && f( a, b ))

will only execute the function f() if a is less than b.

Rule of Thumb

The main thing to get from this feature for now is the following rule of thumb:
Unless you are intentionally taking advantage of the short-circuiting when using && or ||, don't put anything with side-effects on the right hand side.

What is the value of x after
x = 7 ;
y = 15 ;
if( x == 7 || y == x++ )
   if( y == 15 && x < 8 )
      x = 12 ;
      x = 13 ;
   if( x > y || y < 100 )
      x = 14 ;
      x = 15 ;