What happens here: sum += i++;?

Viewed 1146

I have this simple piece of code but I don't understand this part: sum += i++ .

int num1 = 5;
int sum = 0;
         
if (num1 < 100) {
    for (int i = 0; i < num1; i++)
        sum += i++; //?
    System.out.println("sum = " + sum);
} else {
    sum = -1;
}
System.out.print(sum);  

The result I get is 6 and I don't understand how and why.

4 Answers

sum += i++ is equivalent to sum = sum + i; i = i + 1


This translates your code to:

int num1 = 5;
int sum = 0;
         
if (num1 < 100) {
    for (int i = 0; i < num1; i++) //<-- i is incremented
        sum = sum + i; //<-- sum gets incremented by i here
        i = i + 1; //<-- i is incremented again, increment/loop-cycle ratio = 2
    System.out.println("sum = " + sum);
} else {
    sum = -1;
}
System.out.print(sum);  

i is incremented by 2 each loop cycle and sum is incremented by i every loop cycle.


The result is 6 because:

You start the loop where i is 0 and sum is 0.

On the first loop cycle sum is incremented by 0, i is incremented by 2 (2).

On the second loop cycle, sum is incremented by 2 (2) and i is incremented by 2 (4).

On the third loop cycle, sum is incremented by 4 (6) and i is incremented by 2 (6).

At this point, i no longer satisfies the condition i < num1 where num1 is 5, and ends the loop.

In every round, the variable i is getting incremented before a new loop round (i++ in the loop header) and after the line sum += i++;.

This leads to i being 0, 2, 4 consecutively for each time, the line mentioned is called. After i=4 and i being incremented by the loop, the loop stops. Thus, sum=0+2+4=6 is your output.

This behavior is to be expected because of the postfix incrementation, with the operators in i++ following after the variable. This implies i being incremented after the definition of sum. If you analoguously try the prefix notation ++i, you should get a different result.

The loop is executed 3 times, first i=0, then sum is not incremented since i++ is post-increment, note that i is incremented twice at each iteration.

Second iteration i = 2, third iteration i = 4

sum = 0 +2 +4 = 6

Use paper to write a table as below, use a debugger and/or add some more printing to code.

| num1 | sum |  i  | i++ | values before statement is executed, result on next line 
|      |     |     |     | num1 = 5
|   5  |     |     |     | sum = 0
|   5  |  0  |     |     | if (num1 < 100)  // true, exxecute if block
|   5  |  0  |     |     | for (int i = 0;
|   5  |  0  |  0  |     | for (.........; i < num1;  // true, iterate
|   5  |  0  |  0  |     |    ...... i++ // i is increment, old value summed
|   5  |  0  |  1  |  0  |    sum += " (0)
|   5  |  0  |  1  |     | for (.........; ........; i++)
|   5  |  0  |  2  |  1  | for (.........; i < num1;  // true, iterate
|   5  |  0  |  2  |     |   ...... i++ // i is increment, old value summed|
|   5  |  0  |  3  |  2  |   sum += " (2)
|   5  |  2  |  3  |     | // and so on
.
.
.

(debugger may be a bit more complicated to start, but it is a good way to understand what a program is doing and almost essential for debugging code)

Related