Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What declaration turns the below loop into infinite loop?

Tags:

java

loops

Place your declaration for i at line 3 so that the loop becomes an infinite loop.

public class Puzzel3 {
    public static void main(String[] args) {
        // Line 3
        while (i == i + 1) {
            System.out.println(i);
        }
        System.out.println("done");
    }
}
like image 895
user3386829 Avatar asked Mar 06 '14 06:03

user3386829


People also ask

How do you declare an infinite loop?

Basically, the infinite loop happens when the condition in the while loop always evaluates to true. This can happen when the variables within the loop aren't updated correctly, or aren't updated at all. Let's say you have a variable that's set to 10 and you want to loop while the value is less than 100.

How do you declare an infinite loop in Python?

We can create an infinite loop using while statement. If the condition of while loop is always True , we get an infinite loop.

Which loop will be an infinite loop?

An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called an indefinite loop or an endless loop. It either produces a continuous output or no output.

What conditions make an infinite loop?

Do While loop While loop has a condition like while loop. We can make place the same always true statement to make an infinite loop.


1 Answers

Math says, that Infinity + 1 == Infinity, so

// The declaration required
double i = Double.POSITIVE_INFINITY;

// It's infinite loop now...
while (i == i + 1) {
  System.out.println(i);
}

System.out.println("done");
like image 69
Dmitry Bychenko Avatar answered Sep 27 '22 23:09

Dmitry Bychenko