Logical AND and OR Operators

The overall result of the logical AND operator is true if and only if both its operands evaluate to true. The logical OR (||) operator evaluates as true if either of its operands evaluates as true.

The logical AND and OR operators always evaluate their left operand before the right. Moreover, the right operand is evaluated if and only if the left operand does not determine the result. This strategy is known as short-circuit evaluation:

• The right side of an && is evaluated if and only if the left side is true.

• The right side of an || is evaluated if and only if the left side is false.

Several of the programs in Chapter 3 used the logical AND operator. Those programs used the left-hand operand to test whether it ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.