Logical Operator Facts
As mentioned earlier in this chapter, the C++ logical OR and logical AND operators have a lower precedence than relational operators. This means that an expression such as this
x > 5 && x < 10
is interpreted this way:
(x > 5) && (x < 10)
The ! operator, on the other hand, has a higher precedence than any of the relational or arithmetic operators. Therefore, to negate an expression, you should enclose the expression in parentheses, like this:
!(x > 5) // is it false that x is greater than 5!x > 5 // is !x greater than 5
Incidentally, the second expression here is always false because !x can have only the values true or false, which get converted to 1 or 0.
The logical AND operator has a higher precedence than the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access