Can a for loop be written to create an infinite loop or is it only while loops that do that?

Viewed 50210

Can a for loop be written in Java to create an infinite loop or is it only while loops that cause that problem?

12 Answers

There's lots of ways to make for loops infinite. This is the solution I found:

int j = 1;
for (int i = 0; i < j; i++) {
  //insert code here
  j++;
}

This works really well for me because it allows the loop to check an infinite amount of values as well as looping infinitely.

Related