I was reading this post Precedence of && over || . It says that even though the precedence of && is more than ||, we need to evaluate the expression from left to right and if the first expression turns out to be true, then we don't need to evaluate the second expression. But precedence is the order of evaluation and accordingly all pre increments should be evaluated first and then (++j && ++k) should be evaluated. The post seems to be ambiguous on this point. Is it not?
Asked
Active
Viewed 606 times
-3
-
4Precedence is not the same as order of evaluation. Precedence means "Where are parentheses auto-inserted?" Order of evaluation is "What order are each of the pieces evaluated?" – Raymond Chen Aug 11 '15 at 17:14
-
3Precedence affects grouping not order of evaluation. `a || b && c` is affected by precedence in that the `&&` grabs the `b` before the `||` does, leaving the `||` to grab the entire `&&` expression: `a || (b && c)`. – Galik Aug 11 '15 at 17:18
-
@RaymondChen: Seems to be a common misunderstanding. :( Mistaught? – Lightness Races in Orbit Aug 11 '15 at 17:21
-
Ok. I got you that precedence just performs grouping of operands with the operators but how do we come to know about the order of evaluation? Is it defined somewhere? – Navdeep Sony Aug 11 '15 at 17:44
-
Can anybody tell me the difference between order of evaluation of logical expressions vs order of evaluation for arithmetic expressions.? – Navdeep Sony Aug 11 '15 at 18:05
-
Don't confuse *short circuiting* with precedence. The logical operators have the same precedence, *but different short circuiting* rules. – Thomas Matthews Aug 11 '15 at 19:31
-
@NavdeepSony: For arithmetic expressions, the order of evaluation is *unspecified*; given an expression like `a + b * c`, each of `a`, `b`, and `c` may be evaluated in any order (and that order does not have to be consistent). The result of `b * c` must be evaluated before it can be added to `a`, but that does not mean that both `b` and `c` must be evaluated before `a`. The only operators that force a particular order of evaluation are `&&`, `||`, `?:`, and the comma operator. – John Bode Aug 11 '15 at 21:02
-
@JohnBode Thanks for nice explanation. – Navdeep Sony Aug 12 '15 at 14:03
1 Answers
1
From the standard
§5.14 Logical AND operator
The
&&
operator groups left-to-right. The operands are both contextually converted tobool
. The result is true if both operands are true and false otherwise. Unlike&
,&&
guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.The result is a
bool
. If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression.
So, in your example:
(++j && ++k)
The first evaluation will be
++j
Then (assuming j
hasn't become 0
and therefore falsey)
++k
Then
j && k // with both values incremented

Cory Kramer
- 114,268
- 16
- 167
- 218
-
1OP is referencing `++i || ++j && ++k` from the linked question and that they think the `++j && ++k` should be evaluated first. – crashmstr Aug 11 '15 at 17:14
-
1