Java Infinite While Loop
To make a Java While Loop run indefinitely, the while condition has to be true forever. To make the condition always true, there are many ways. Some of these methods are:
- Write boolean value
true
in place of while loop condition. - Or, write a while loop condition that always evaluates to
true
, something like1==1
. - Write a while loop condition such that the control variables in condition are never updated.
In this tutorial, we will write Java programs to create infinite while loop, using above methods.
Flowchart – Java Infinite While Loop
Following is the flowchart of infinite while loop in Java.
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.
Example 1 – Java Infinite While Loop with True for Condition
Firstly, we know that the condition in while 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 while loop.
Java Program
/**
* Java Program - Infinite While Loop
*/
public class InfiniteWhileLoop {
public static void main(String[] args) {
while (true) {
System.out.println("hello");
}
}
}
Output
hello
hello
hello
hello
Note: You will see the string hello
print to the console infinitely. 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 the stop button provided by the IDE.
Example 2 – Java Infinite While Loop with Condition that is Always True
Instead of giving true
boolean value for the condition, 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 while loop will run forever.
Java Program
/**
* Java Program - Infinite While Loop
*/
public class InfiniteWhileLoop {
public static void main(String[] args) {
while (1 == 1) {
System.out.println("hello");
}
}
}
Output
hello
hello
hello
hello
Example 3 – Java Infinite While Loop with No Update to Control Variables
These type of infinite while 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 while body, i
is never updated. This makes the loop an infinite while loop.
Java Program
/**
* Java Program - Infinite While Loop
*/
public class InfiniteWhileLoop {
public static void main(String[] args) {
int i = 10;
while (i > 0) {
System.out.println("hello");
}
}
}
Output
hello
hello
hello
hello
Example – Java Infinite While Loop while working with Continue Statement
This also is a typical scenario where we use a continue statement in the while loop body, but forget to modify the control variable.
In the following example, we have initialized i
to 10
, and in the while loop we are decrementing i
by one during each iteration. The condition is that i
should be positive. When the while starts execution, and i
is decrementing, and when i
reaches 5
, we have a continue statement. And we have not updated the control variable i
. So, i
is ever going to be 5
. As a result, program control is never coming out of the while loop.
Java Program
/**
* Java Program - Infinite While Loop
*/
public class InfiniteWhileLoop {
public static void main(String[] args) {
int i = 10;
while (i > 0) {
if (i == 5)
continue;
System.out.println("hello");
i--;
}
}
}
Output
hello
hello
hello
hello
hello
Conclusion
In this Java Tutorial, we learned how to write an Infinite While Loop, in some of the many possible ways, with the help of example programs.