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 ...

Get C++ Primer Plus 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.