Java Infinite For Loop

To make a Java For Loop run indefinitely, the condition in for statement has to be true whenever it is evaluated. To make the condition always true, there are many ways.

In this tutorial, we will learn some of the ways to create an infinite for loop. We shall learn these methods with the help of example Java programs.

Flowchart – Java Infinite For Loop

Following is the flowchart of infinite for loop in Java.

Java Infinite For Loop

As the condition is never going to be false, the control never comes out of the loop, and forms an Infinite Loop as shown in the above diagram, with blue paths of execution.

Example 1 – Java Infinite For Loop with True for Condition

Firstly, we know that the condition in for loop statement has to always evaluate to true for it to become infinite Loop. Secondly, we also know that the condition evaluates to a boolean value. So, considering these two statements, we can provide the boolean value true, in place of condition, and the result is a infinite for loop.

Java Program

</>
Copy
/**
 * Java Program - Infinite For Loop
 */

public class InfiniteForLoop {

	public static void main(String[] args) {
		for (; true; ) {
			System.out.println("hello");
		}
	}
}

Output

hello
hello
hello
hello

Note: You will see the string hello print to the console infinitely, one line after another. If you are running from command prompt or terminal, to terminate the execution of the program, enter Ctrl+C from keyboard. If you are running the program from an IDE, click on stop button provided by the IDE.

Example 2 – Java Infinite For Loop with Condition that is Always True

Instead of giving true boolean value for the condition in for loop, you can also give a condition that always evaluates to true. For example, the condition 1 == 1 or 0 == 0 is always true. No matter how many times the loop runs, the condition is always true and the for loop will run forever.

Java Program

</>
Copy
/**
 * Java Program - Infinite For Loop
 */

public class InfiniteForLoop {

	public static void main(String[] args) {
		for (; 1 == 1; ) {
			System.out.println("hello");
		}
	}
}

Output

hello
hello
hello
hello

Example 3 – Java Infinite For Loop with No Update to Control Variables

These type of infinite for loops may result when you forget to update the variables participating in the condition.

In the following example, we have initialized variable i to 10. Typically, in the following example, one would decrement i to print hello 10 times. But, if we forget the decrement statement in the for loop update section, i is never updated. This makes the loop an infinite for loop.

Java Program

</>
Copy
/**
 * Java Program - Infinite For Loop
 */

public class InfiniteForLoop {

	public static void main(String[] args) {
		for (int i = 10; i > 0; ) {
			System.out.println("hello");
		}
	}
}

Output

hello
hello
hello
hello

Conclusion

In this Java Tutorial, we learned how to write an Infinite For Loop in Java, with the help of example programs.