-2

Up until today I thought I understood C++ operators and precedence. I give you the following simple code:

int i = 0, j = 0
i++;
j++;

cout << i << ' ' << j << endl;

Naturally, we expect the output values for i and j to be 1 and 1, respectively. Now what happens if we try to put our increments into one line?

int i = 0, j = 0
i++ && j++;

cout << i << ' ' << j << endl;

My reasoning here is that a boolean operator has no effect on the output. But our output is surprisingly i = 1 and j = 0? So what's going on here?

What's more strange to me is that by switching from postfix increment to prefix increment or using another boolean operator, the result is as expected. I.e:

//Expected i=1, j=1
++i && ++j;

//Expected i=1, j=1
i++ || j++;

P.S. I know that the right use case to update both variables in one line looks like this:

i++, j++;

But curiosity got the best of me and now I'm wondering why using the boolean AND operator has the unsurprising result.

Max
  • 2,072
  • 5
  • 26
  • 42
  • 6
    The output of the second code snippet is not what you claim it is: https://ideone.com/61YZL6 – Oliver Charlesworth Dec 02 '17 at 23:44
  • I think he is expecting the left side of the && expression to evaluate to 0, as it should, and therefore no need to check the right side – Abraham McIlvaine Dec 02 '17 at 23:51
  • 3
    The "right use case to update both variables" is `i++; j++;`. And you gain nothing from putting them in a single line. – Bo Persson Dec 02 '17 at 23:54
  • `But our output is surprisingly i = 1 and j = 1?` it is not, it is 1 and 0 – Killzone Kid Dec 03 '17 at 00:08
  • I expected the output to be 1 and 1 but it is instead 1 and 0. I had a clerical error by saying I got 1 and 1. @OliverCharlesworth – Max Dec 03 '17 at 01:06
  • @BoPersson I actually need to put them in one line as I am using them in a ternary operator – Max Dec 03 '17 at 01:08
  • @KillzoneKid correct, I would not have expected 1 and 0. I fixed my typo – Max Dec 03 '17 at 01:26
  • 1
    @Max: using them in a ternary operator doesn't necessarily mean you have to increment them in the ternary operator. Particularly if you want to increment them unconditionally. – Michael Burr Dec 03 '17 at 01:26
  • @MichaelBurr I simplified the case for above, I do want to conditionally increment – Max Dec 03 '17 at 01:36

1 Answers1

1

i++ increments i but the result of the expression is 0 (the increment is a side-effect). So the expression i++ && j++ never evaluates the right hand side of the && operator.

The result of the pre-increment operator, ++i, is the incremented value. That is why ++i && ++j also increments j.

Note that none of this has to do with precedence. The most important thing is understanding exactly what the difference is between the pre and post increment operators. The other factor is that the && operator is defined to "short circuit" - it must not evaluate the right hand side of the expression if the left hand side evaluates to 0.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Thanks Michael, I failed to remember that because the left hand side evaluates to 0, we will never evaluate the post-increment for `j` – Max Dec 03 '17 at 02:10