0

Why does this code produce an infinite loop?

for(int i=0; i<10; ) {
  i = i++;
  System.out.println("Hello World!");
}
Jinzu
  • 1,325
  • 2
  • 10
  • 22
  • for what reason would you need to do this when you can just say (int i = 0; i < 10; i++) – BaneDad Jul 31 '17 at 21:06
  • This is infinite. You, every time, resetting i to 0 after each for iteration. YOu can test below code for understanding. ` for(int i=0; i<10; ) { i = i++; System.out.println(i); }` – Uddhav P. Gautam Jul 31 '17 at 21:09
  • Because `i = i++;` should just be `i++;`. If you tried to do `for (int i=0; i<10; i = i++)` you would have the same problem, for reasons explained in the duplicate question. – Sean Van Gorder Jul 31 '17 at 21:20

0 Answers0