For loops run forever in Java [copy]

See English answer > what is X after "x = x + +"? 17

@H_ 419_ 9@

@H_ 419_ 9@

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

Output: @ h_ 419_ 9@

@H_ 419_ 9@

0
0
0
0

This code runs forever. In short, this is an infinite loop. Technically, when we see that I is allocated and then there is a post increment operation, the range is within the loop. Even if it is a post increment, it should increase the actual me and this should not run forever I want to know some interesting explanations from the experts@ H_ 419_ 9@

Solution

We always know that I return to I and then increment However, this is not entirely true Always return a value after performing an operation So what I really do is increase by 1 and then return to the value before increment So I = I first increments I and then sets it back to the value before the increment

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>