Chapter 30. Short-circuit!

The && and || operators provide a Boolean evaluation of the two expressions, as in:

if(x && y) // Evaluates to true if both x and y are true
if(x || y) // Evaluates to true if either x or y are true

Both these operators have what is known as short-circuit evaluation semantics [Stro1997]. Since the && operator provides a logical AND of the two arguments, there is no need to evaluate the second argument if the first is false. Conversely, for the || operator, which provides a logical OR of its arguments, the second argument does not need to be evaluated if the first is true. Since C is all about efficiency, the second arguments are only evaluated when they are needed. This is a very useful facility since it allows us to ...

Get Imperfect C++ Practical Solutions for Real-Life Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.