Writing logical or arithmetic expressions that depend on precedence of operators, rather than grouping operations with parentheses, is usually frowned upon for reasons of readability and maintainability of code. However, the importance of understanding the operator's precedence and their evaluation order cannot be overlooked. Here we are going to highlight a common misunderstanding associated with operator's precedence and their evaluation order.
C++ logical AND operator (&&) has higher precedence than logical OR (||) operator. This code uses
||operators in an expression:
int x=1, y=0, z=0; bool result = x || y && z++;
What do you think are values of result and z?