21

I'm doing some research about Java and find this very confusing:

for (int i = 0; i < 10; i = i++) {
  System.err.print("hoo... ");
}

This is never ending loop!

Anybody has good explanation why such thing happens?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Michał Kupisiński
  • 3,765
  • 1
  • 19
  • 23

5 Answers5

39
for (int i = 0; i < 10; i = i++) {

The above loop is essentially the same as: -

for (int i = 0; i < 10; i = i) {

the 3rd part of your for statement - i = i++, is evaluated as: -

int oldValue = i; 
i = i + 1;
i = oldValue;  // 3rd Step 

You need to remove the assignment from there, to make it work: -

for (int i = 0; i < 10; i++) {

(On OP request from Comments)

Behaviour of x = 1; x = x++ + x++; : -

As far as your issue as specified in the comment is concerned, the result of the following expression: -

x = 1; 
x = x++ + x++;

is obtained as follows: -

Let's mark different parts of the second statement: -

x = x++ + x++;
R    A     B

Now, first the RHS part (A + B) will be evaluated, and then the final result will be assignmed to x. So, let's move ahead.

First A is evaluated: -

old1 = x;  // `old1 becomes 1`
x = x + 1; // Increment `x`. `x becomes 2`
//x = old1; // This will not be done. As the value has not been assigned back yet.

Now, since the assignment of A to R is not done here, the 3rd step is not performed.

Now, move to B evaluation: -

old2 = x;  // old2 becomes 2. (Since `x` is 2, from the evaluation of `A`)
x = x + 1; // increment `x`. `x becomes 3`.
// x = old2; // This will again not be done here.

Now, to get the value of x++ + x++, we need to do the last assignment that we left in the evaluation of A and B, because now is the value being assigned in x. For that, we need to replace: -

A --> old1
B --> old2   // The last assignment of both the evaluation. (A and B)

/** See Break up `x = old1;` towards the end, to understand how it's equivalent to `A = old1; in case of `x = x++`, considering `x++ <==> A` in this case. **/

So, x = x++ + x++, becomes: -

x = old1 + old2;
  = 1 + 2;
  = 3;  // Hence the answer

Break up of 3rd part of x = x++, to see how it works in x = x++ + x++ case: -

Wonder why the replacement is done as A --> old1 and not x --> old1, as in case of x = x++.

Take a deep look at x = x++ part, specially the last assignment: -

x = oldValue;

if you consider x++ to be A here, then the above assignment can be broken into these steps: -

A = oldValue;
x = A;

Now, for the current problem, it is same as: -

A = old1;
B = old2;
x = A + B;

I hope that makes it clear.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 2
    This is the only answer that explained the difference between `i = i++` and plain `i++`. – Luiggi Mendoza Jan 28 '13 at 21:13
  • Okey, this looks possible but I found somewhere an example of `int x = 1; x = x++` and after that `x` is still `1` but here: `x = x++ + x++` x won't be still `1` or `2` but `3` - how?? – Michał Kupisiński Jan 28 '13 at 21:15
  • 2
    Got curious about that and googled this, explaining why `i=i++` does not work https://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment – SJuan76 Jan 28 '13 at 21:15
  • @emka86.. May be because `x++` is evaluated to `1`, but `x++ + x++` is evaluated to `3`. But still that's quite interesting. Will have a look at it in detail. – Rohit Jain Jan 28 '13 at 21:22
  • @emka86.. That really deserves a place for new question. You should ask it. that `x = x++ + x++;` one. For now, I'm trying to device the evaluation order. – Rohit Jain Jan 28 '13 at 21:28
  • @emka86.. Added some explanation about your doubt. You can have a look at the revised answer. – Rohit Jain Jan 28 '13 at 22:05
8

You're using post-increment: i = i++;, it means something like this:

temp = i;
i = i + 1;
i = temp;

because 15.14.2 Postfix Increment Operator ++:

The value of the postfix increment expression is the value of the variable before the new value is stored.

That is why you have the old value.

For-loop done right:

for (int i = 0; i < 10; i++) {
  System.err.print("hoo... ");
}
4

because of i=i++

for (int i = 0; i < 10;  i++) {
  System.err.print("hoo... ");
}
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
4

i++ will report the value of i, and THEN increment. This also means that you don't need to set i equal to i++, just change to

for (int i = 0; i < 10; i++) {
DavidDraughn
  • 1,110
  • 1
  • 8
  • 15
0

The problem is in the statement i=i++, this statement does three operations in sequence to complete. these are two operations in i++ and one assignment( = ) operation. These are;

i++ does two operation before any operation

  1. Returns i

  2. Increments i

finally assignment operation

  1. Assigns returned value

So i is incremented before assignment making it to have old value.

Lets see i=i++ in the first loop( where i = 0 ) with the three operations

  1. returns 0 , it will not be assigned to i until the next operation is done
  2. increments i ( i= 0 + 1 = 1 )
  3. assigns returned value 0 to i, ( i = 0), finally i having its old value.

In i++ first i is returned, but immediately before any operation is done it is incremented(i becomes i+1 ), However we are yet left with assignment operation to complete so that the returned value will be assigned to i making it to have old value, thereby entering infinite loop.

so replace i=i++ with i++or i=i+1

The Scientific Method
  • 2,374
  • 2
  • 14
  • 25