0

The following code snippet:

int i=-3,j=2,k=0,m;

m=++i && ++j || ++k;

can be evaluated using two concepts,I believe:

1.Since ++ operator has greater precedence than the logical operators,so first all increment operators will be evaluted,then && having higher precedence than || will be computed.In this process,k will be incremented.

2.First && operator will be evaluated.For this ++ i and ++j will be computed.Since the result of the && operator is 1,no need to evaluate the ++k.So k will not be incremented.

When I try it on a system, the result proves reasoning 2 to be correct and 1 to be wrong. Why is it so?

Floris
  • 45,857
  • 6
  • 70
  • 122
user1369975
  • 430
  • 1
  • 6
  • 19

3 Answers3

2

Oli is right... You're confusing precedence with evaluation order.

Precedence means that the expression is interpreted as:

m = ((((++i) && (++j)) || (++k));

As opposed to, say:

m = (++(i && ++(j || (++k)))

Precedence doesn't change the fact that the LHS of the || operator will always be evaluated before the RHS.

Taylor Brandstetter
  • 3,523
  • 15
  • 24
  • I don't understand - maybe I'm overthinking this. `++(j || (++k))` isn't even a valid expression? I don't think it is - and my `gcc` compiler doesn't like it. And if it did - wouldn't `++(any logical expression)` always evaluate to `true` since `false == 0` and `true == 1` (by the definition of the standard - `(int)(true) === 1`. Maybe you meant `((++i && (++j || (==k)))`. It makes the same point I think. – Floris Jun 21 '13 at 17:49
  • 2
    @Floris It isn't a valid expression, that's why it's a good thing precedence works the way it does. And I used that as an example to explicitly illustrate what would happen if the `++` operator's precedence was reversed, since the `++` operator is what the OP was most concerned with. – Taylor Brandstetter Jun 21 '13 at 18:05
0

In attempting to be efficient, evaluation of an OR statement (executed from left to right) stops when the LHS is true. There is no need to start evaluating the RHS - there is no concept of "precedence" except within the same group of an expression (when it matters to the value of the expression whether you first do A or B. Example: 5 + 3 * 2 should evaluate to 11. But in evaluating ( 5 + 6 > 3 * 2) it doesn't matter whether you do the addition before the multiplication - it doesn't change the result of the comparison. And in practice this gets evaluated left-to-right. Thus you get the result you observed.

See also this earlier answer

Community
  • 1
  • 1
Floris
  • 45,857
  • 6
  • 70
  • 122
  • "in practice this gets evaluated left-to-right" - This is specifically true for the short-circuit operators, but not in general. – Oliver Charlesworth Jun 21 '13 at 15:53
  • @Roris thanks a lot but couldn't get the line concept of precedence within same group of expression – user1369975 Jun 21 '13 at 15:55
  • @OliCharlesworth you are right - the linked answer shows explicitly for what operators this is true. – Floris Jun 21 '13 at 15:56
  • What I mean with "concept of precedence": unless two operators are being used in the SAME expression, AND the order in which you evaluate them influences the outcome, there is no reason why one should be executed before the other. In my simple comparison the addition can happen before the multiplication without changing the final expression. That's what I was trying to say: "if the order doesn't matter, then 'precedence' is not a useful concept". – Floris Jun 21 '13 at 15:59
  • @Taylor Brandstetter thanks a lot for your answer.all the answers were good but yours was the one that really addressed my problem – user1369975 Jun 21 '13 at 17:19
0

The && and || operators force left-to-right evaluation. So i++ is evaluated first. If the result of the expression is not 0, then the expression j++ is evaluated. If the result of i++ && j++ is not 1, then k++ is evaluated.

The && and || operators both introduce sequence points, so the side effects of the ++ operators are applied before the next expression is evaluated. Note that this is not true in general; in most circumstances, the order in which expressions are evaluated and the order in which side effects are applied is unspecified.

John Bode
  • 119,563
  • 19
  • 122
  • 198