for (int i = 0; i < 10;) {
i=i++;
System.out.println("Hello World" );
}
Basically the value of i remains unchanged, and stays 0, so it is infinite. But Why doesnt it change?
If I changed i=i++
to i++
, it works. (not infinite loop).
for (int i = 0; i < 10;) {
i=i++;
System.out.println("Hello World" );
}
Basically the value of i remains unchanged, and stays 0, so it is infinite. But Why doesnt it change?
If I changed i=i++
to i++
, it works. (not infinite loop).
Logically, the assignment is done after evaluation of the right hand side, as for any other Java assigment. However, "The value of the postfix increment expression is the value of the variable before the new value is stored." (JLS, 15.14.2. Postfix Increment Operator ++)
The value of i
before the incremented value is stored stays zero, because of the assignment.