Genki
Genki

Reputation: 101

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?

Upvotes: 10

Views: 57849

Answers (12)

Luke W
Luke W

Reputation: 1

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.

Upvotes: 0

Coder588377
Coder588377

Reputation: 21

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}

Upvotes: 2

David Balažic
David Balažic

Reputation: 1473

There is also this one, to complete the topic:

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

Upvotes: 0

Anonymous k
Anonymous k

Reputation: 1

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

Upvotes: -1

SyntaxT3rr0r
SyntaxT3rr0r

Reputation: 28303

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 : )

Upvotes: 4

Maarten Terpstra
Maarten Terpstra

Reputation: 426

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.

Upvotes: 1

tsm
tsm

Reputation: 3658

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>
}

Upvotes: 9

Rocky Pulley
Rocky Pulley

Reputation: 23311

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

for(;;);

Upvotes: 2

Kyle
Kyle

Reputation: 2018

Dont forget mistakes like

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

It's not as uncommon as it should be.

Upvotes: 4

Becuzz
Becuzz

Reputation: 6864

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.

Upvotes: 6

Bala R
Bala R

Reputation: 108957

for(;;){}

is same as

while(true){}

Upvotes: 17

Howard
Howard

Reputation: 39207

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

for(;;) {
}

Upvotes: 10

Related Questions