Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post Increment Infinite loop i += i++; [duplicate]

Tags:

java

increment

I am not understanding why this post increment equation does not increase. I would have thought that after the += operation the value would increment by 1 and then the second time around i would have a 1 value. But the output is an infinite loop of 0 zero. Is anyone able to explain why 'i' doesn't increase.

int i = 0;
for(; ; ) {
  if ( i >= 10) break;
  i += i++;
}
System.out.println(i);
like image 382
tapalot Avatar asked Feb 14 '17 05:02

tapalot


3 Answers

While the answer from @njzk2 is correct, it is useful to point out why it is correct.

There are other possibilities - for example, why doesn't Java execute the postincrement operator after the assignment? (Answer: because that's not what the Java Language Designers chose)

The evaluation order for compound assignments (things like +=) is specified in the Java Language Specification section 15.26.2. I'll quote how it is defined for Java 8:

  • First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.

  • Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

  • Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

  • Otherwise, the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.

The most important thing is that the value of the left hand expression is saved first, then the right hand is completely evaluated, and then the result of the compound operation is stored in the variable on the left hand side.

like image 136
Erwin Bolwidt Avatar answered Sep 21 '22 13:09

Erwin Bolwidt


Let's examine i += i++;

i++ means read the value for i, then increment i.

i += x means evaluate i, then evaluate x and add the 2 and put the result in i.

So, what happens is:

  • i gets evaluated, it is 0
  • i++ gets evaluated. it returns 0, and the value for i is set to 1
  • i += i++ is now i = 0 + 0.
  • i = 0

Try with ++i to get the result of the incrementation before reading its value.

like image 32
njzk2 Avatar answered Sep 23 '22 13:09

njzk2


No surprise you are getting into infinity loop..

class Demo{
  public static void main(String[] args){
    int i = 0;
    for( ; ; ) {
        if (i >= 10) 
            break;
        i = i+i++;
        System.out.println(i);
    }
  }
}

assume the above code, I have just replaced your line with increment with what compiler will substitute.

Now, initially i=0; So

i = i+i++

results in i = 0 + 0 // and now i is 1

but at the end you again make i equal to 0!

Hence infinity...

like image 22
Yash Soni Avatar answered Sep 21 '22 13:09

Yash Soni