Java Infinite While Loop
A Java While Loop becomes infinite when its condition never evaluates to false. The loop keeps executing the statements in its body until the program is stopped or the loop is exited explicitly with a statement such as break.
An infinite while loop can be intentional, such as while (true) in a program that waits continuously for work, or accidental because the variables used by the loop condition are not updated correctly.
- Use the boolean value
trueas the while loop condition. - Use an expression that always evaluates to
true, such as1 == 1. - Write a condition whose control variables never change enough to make the condition false.
- Skip an important update with a statement such as
continue, causing the same condition to repeat forever.
Syntax of an Intentional Java Infinite While Loop
The simplest way to deliberately create an infinite while loop in Java is to use true as the condition.
while (true) {
// statements to repeat
}
Because true never becomes false, Java repeatedly executes the loop body. The loop ends only if control leaves it explicitly, an exception terminates the flow, or the program itself is stopped.
Flowchart of a Java Infinite While Loop
The following flowchart shows the behavior of an infinite while loop in Java.
After each iteration, the condition is checked again. If that condition remains true every time, program control returns to the loop body instead of continuing with the statement after the loop.
Example 1 – Java Infinite While Loop Using true
A while statement requires a boolean condition. Therefore, placing the boolean literal true directly in the condition creates an intentional infinite 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
The output continues beyond the four lines shown above because the condition is always true. If you run the program from a command prompt or terminal, you can normally stop it with Ctrl+C. When running it in an IDE, use the IDE’s stop or terminate control.
Example 2 – Java Infinite While Loop with an Always-True Expression
The condition does not have to be the literal true. Any boolean expression that remains true can produce the same behavior. For example, 1 == 1 always evaluates to true.
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
This loop behaves like while (true). For code that is intentionally meant to run indefinitely, while (true) usually communicates that intention more directly than an expression such as 1 == 1.
Example 3 – Infinite While Loop Caused by a Missing Variable Update
A common accidental infinite loop occurs when a variable used in the while condition is never updated.
In the following example, i starts at 10, and the condition is i > 0. If the intention is to print hello ten times, i must eventually be reduced. Because no decrement statement is present, i stays equal to 10, so i > 0 remains true forever.
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
To make this loop terminate after ten iterations, update the control variable inside the loop so that the condition can eventually become false.
int i = 10;
while (i > 0) {
System.out.println("hello");
i--;
}
Java Infinite While Loop Caused by continue
An infinite loop can also occur when continue skips the statement responsible for updating a control variable.
In the following program, i starts at 10 and normally decreases after each iteration. When i reaches 5, however, the continue statement immediately starts the next iteration. The statement i-- is skipped, so i remains 5. Every later iteration reaches the same continue statement, and the loop never terminates.
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
Only five lines are printed because the program reaches i == 5 after printing for values 10 through 6. It then loops indefinitely without printing anything because continue is executed before both System.out.println() and i--.
How to Exit an Intentional Java Infinite While Loop with break
An intentionally infinite loop can still have an internal exit condition. The break statement immediately terminates the nearest enclosing loop when that condition is reached.
public class InfiniteWhileLoopWithBreak {
public static void main(String[] args) {
int count = 1;
while (true) {
System.out.println(count);
if (count == 5) {
break;
}
count++;
}
System.out.println("Loop ended");
}
}
Output
1
2
3
4
5
Loop ended
Although the while condition is always true, the loop terminates when count == 5 because break transfers control to the first statement after the loop.
Why a Java While Loop Becomes Infinite
If a while loop runs forever unexpectedly, examine the condition and every value that can affect it. Typical causes include the following.
- The control variable is never updated: for example,
i > 0is checked repeatedly whileikeeps the same positive value. - The variable changes in the wrong direction: a loop may require
ito decrease, but the code increases it instead. - The condition can never become false: an expression such as
1 == 1remains true regardless of the number of iterations. - An update is skipped:
continue, a conditional branch, or another control-flow path may bypass the required update. - The exit condition is unreachable: a
breakstatement may exist but its surrounding condition may never become true.
How to Debug an Accidental Infinite While Loop in Java
When a Java while loop does not terminate as expected, trace the variables involved in its condition from one iteration to the next.
- Identify every variable used in the
whilecondition. - Check where each variable is updated inside the loop.
- Confirm that the update moves the variable toward making the condition false.
- Inspect
continue,if, and other branches that may skip the update. - Temporarily print the relevant variable values or use an IDE debugger to observe how they change during each iteration.
- Verify that any intended
breakcondition can actually be reached.
For example, if the loop condition is i > 0, ask what value i has after each iteration and whether it will eventually reach 0 or a negative value.
Intentional vs Accidental Infinite While Loops in Java
An infinite loop is not necessarily an error. Some programs deliberately keep processing until an external event or an internal exit condition occurs. In such cases, using while (true) makes the indefinite behavior explicit, and a clear termination mechanism such as break can be added when appropriate.
An accidental infinite loop is different: the programmer expects the condition to become false, but a logic or control-flow error prevents that from happening. Missing updates and updates skipped by continue are common examples.
Java Infinite While Loop Editorial QA Checklist
- Verify that every example identified as infinite has a condition that remains true or a control-flow path that prevents termination.
- For loops intended to terminate, verify that the variables in the while condition are updated toward a false condition.
- Check whether any
continuestatement can skip a required counter or state update. - When
while (true)is used intentionally, confirm that the text clearly explains how the loop can be stopped or exited. - Make sure displayed output from infinite examples is described as partial output rather than the complete program output.
Summary of Java Infinite While Loops
In this Java Tutorial, we learned that a while loop becomes infinite when its condition never evaluates to false. We created intentional infinite loops with true and an always-true expression, examined accidental loops caused by missing updates and continue, and used break to exit an otherwise infinite loop.
TutorialKart.com