Can a for Loop Be Written to Create an Infinite Loop or Is It Only While Loops That Do That?

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

3

12 Answers

for(;;){}

is same as

while(true){}
1

You can also do such with for loops. E.g. the following is identical to while(true):

for(;;) {
}
1

Apart from issues of scope and one other thing, this:

for(<init>; <test>; <step>) {
  <body>
}

is the same as:

<init>
while(<test>) {
  <body>
  <step>
}

As other people have alluded to, in the same way that you can have a while loop without an <init> form or <step> form, you can have a for loop without them:

while(<test>) {
  <body>
}

is the same as

for(;<test>;) {
  <body>
} //Although this is terrible style

And finally, you could have a

for(;true;) {
  <body>
}

Now, remember when I said there was one other thing? It's that for loops don't need a test--yielding the solution everyone else has posted:

for(;;) {
  <body>
}
1

Sure you can

for(int i = 0; i == i; i++) {}

Any loop can be made infinite as long as you make a way to never hit the exit conditions.

Dont forget mistakes like

for (int i=0; i<30; i++)
{
    //code
   i--;
}

It's not as uncommon as it should be.

Just for fun (and this too long for a comment): a lot of people will be very surprised to learn that for a lot of very practical purposes the following is nearly an infinite loop:

    for (long i = Long.MIN_VALUE; i < Long.MAX_VALUE; i++) {
        ...
    }

If the thread executing this loops can do 4 billions cycles per second and can do the increment and the check in one cycle (quite beefy for a single thread) and if my maths ain't totally off, I think the above code needs about 150 years to execute : )

I'll just add this since nobody did this version:

for(;;);

The way I made my server to run as long until I shut it down is

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

Ofcourse for loops can cause infinite loops. An example is:

for(int i = 0; i < 99; i /= 2){ ... }

Because i is never incremented, it will stay in the body of the for loop forever until you quit the program.

There is also this one, to complete the topic:

do {something();} while(true);

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.

you can declare a subpart of the code to be another part in a for loop, example -

public class (classname) {
    for(int i = 1; i <= 4; i++) {
        for(int j = 1; i <= 4; j++) {
    system.out.println(i + "*" + j + "=" (i*j));
}

}

it is almost in infinite loop; if you change int to long, and add more variables, you can practically make it last 25 x 10^12 minutes long

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest