Logical Operators

Often you want to ask more than one relational question at a time. “Is it true that x is greater than y, and also true that y is greater than z?” A program might need to determine that both of these conditions are true, or that some other condition is true, in order to take an action.

Imagine a sophisticated alarm system that has this logic: “If the door alarm sounds AND it is after six p.m. AND it is NOT a holiday, OR if it is a weekend, then call the police.” The three logical operators of C++ are used to make this kind of evaluation. These operators are listed in Table 4.2.

Table 4.2. The Logical Operators
Operator Symbol Example
AND && expression1 && expression2
OR || expression1 || expression2
NOT ! !expression

Logical ...

Get Sams Teach Yourself C++ in 24 Hours, Third 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.