Relational Precedence

Relational operators and logical operators, being C++ expressions, each return a value: 1 (true) or 0 (false). Like all expressions, they have a precedence order that determines which relations are evaluated first. This fact is important when determining the value of the statement

if ( x > 5 && y > 5  || z > 5)

It might be that the programmer wanted this expression to evaluate true if both x and y are greater than 5 or if z is greater than 5. On the other hand, the programmer might have wanted this expression to evaluate true only if x is greater than 5 and if it is also true that either y is greater than 5 or z is greater than 5.

If x is 3, and y and z are both 10, the first interpretation will be true (z is greater than ...

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.