1

I am currently reading Barry Burd's Java For Dummies, and came upon this little exercise.

The exercise is regarding post and pre-increment. In the problem (please see the code) I was able to figure out the answers to all the lines(without the help of compiler) except for the last one. Which does not make sense according to my knowledge of post/pre-increment so far.

Please advise on how or why the result is not what I expected.

I tried initializing and declaring a new variable (int) with the value of "20" and then did "i = i++ + i++", but still received the same result (41).

Also tried doing out.println(i) twice, but it still printed 41.

import static java.lang.System.out;

public class Main
{
    public static void main(String[] args) {

        int i = 10;
        out.println(i++); //10(post11)
        out.println(--i); //10(pre11-1)

        --i; //9(pre10-1)
        i--; //9(post8)
        out.println(i); //8
        out.println(++i); //9(pre8+1)
        out.println(i--); //9(post8)
        out.println(i); //8
        i++; //8(post9)
        i = i++ + ++i; //i = 9(post10) + 10(pre9+1) = 19(post20)
        out.println(i);  //20
        i = i++ + i++; //i = 20(post21) + 20(post21) = 40(post42)
        out.println(i); //41 (result copied from compiler)  

    }
}

I expected the last line to print 42 rather than 41, since "20" gets added twice, and also gets incremented twice.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
F.A.
  • 15
  • 5
  • 3
    FWIW, I believe that combining post/pre-increment operators with other operators such as addition and assignment in the same statement is an abomination. – rgettman Aug 14 '19 at 20:53

3 Answers3

4
i = i++ + i++

This evaluates to (if i is 20):

i = 20 + 21

Since the first i++ is a post operator, and so doesn't affect it. However, it does affect the next usage of i.

I'll break it down step by step:

i =i+++ i++;, i == 20

i =20+ i++, i == 21

i = 20 +i++, i == 21

i = 20 +21, i == 22

i =41

Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
  • You might consider explicitly outlining how each `i++` is evaluated individually (one after the other) to result in two different values for "the same expression". – Zircon Aug 14 '19 at 20:54
  • Thank you so much for taking the time to answer this, albeit it's still puzzling but it makes relatively more sense. The part I can't get is how "i" became "22". – F.A. Aug 15 '19 at 23:07
  • ```i``` became 22 when the second post-increment took affect. – Michael Bianconi Aug 16 '19 at 13:09
0

When you use the assignment operator (=) it is done after the post increment operator. As such you get this:

int i = 10;
i = i++;
System.out.println(i); // print 10

When you are using the post increment twice in the same line, it is not increased after the line is completed, but after the instruction is completed (the instruction being i++). Thus, when doing i = i++ + i++; you are in fact doing this :

i = i++ + i++; // i=20 Original code
i = 20 + i++;  // i=21 The first i++ return 20 then increment i to 21
i = 20 + 21;   // i=22 The second i++ return 21 then increment i to 22
i = 41;        // i=22 The addition is computed 
41;            // i=41 and assigne to i

It all as to do with operator precedence.

Jonatan Cloutier
  • 899
  • 9
  • 26
  • Thank you so much for answering this, so my understanding is that regardless of how many times post-increment is used in the same line, the value keeps passing on to the next "i++" and essentially the first "i++" does not get incremented since the passing on ends after the last operation in the same line. Also, please explain the part where i becomes "22". Sorry for the inconvenience. – F.A. Aug 15 '19 at 23:09
  • i is incremented just after i++ return its current value. Thus, the first time on the line it return 20 and became 21. The second time on the line it return 21 then became 22. after that the addition is computed (20+21) and assigned to I which became 41. – Jonatan Cloutier Aug 16 '19 at 00:56
0

You can see what is happening by writing your own print routine.

      int i = 9;
      i = print(i++) + print(++i); // i = 9(post10) + 10(pre9+1) = 19(post20)
      System.out.println("\n" + i); // 20
      i = print(i++) + print(i++); // i = 20(post21) + 20(post21) = 40(post42)
      System.out.println("\n" + i); // 41 (result copied from compiler)

      public static int print(int i) {
          System.out.print(" i = " + i + " ");
          return i;
      }

WJS
  • 36,363
  • 4
  • 24
  • 39