-5
public class Temp { 
public static  void main(String[] args) { 
   for (int i = 0; i < 10;){
       i = i++;
       System.out.println("Hello world");
   }   
 }
}

why i does not get increment as we are incrementing it still it does not get increment
as we can see in the for loop when we are doing post-increment it isnt working and it goes to infinite loop, when we are doing pre-increment i = ++i its working properly and even when we are doing like i = (i=i+1) here also it working
but not when we are doing post increment i is assign to i++ and that time value of i was 0 but immediately after it got increment by 1 than it should reflect on next loop iteration
why it is not reflecting?

Michael
  • 41,989
  • 11
  • 82
  • 128

1 Answers1

1

i++ returns the original value of i (prior to the increment), so if you assign that value back to i, you revert i back to its original value.

You don't have to assign i++ or ++i to i.

Eran
  • 387,369
  • 54
  • 702
  • 768