Java Infinite For Loop

A Java For Loop becomes infinite when it never reaches a condition that evaluates to false. This can be intentional, such as for (;;), or accidental, such as when the control variable is never updated.

In Java, a for loop has initialization, condition, and update sections. An infinite for loop can be created by omitting the condition, using a condition that is always true, or writing the loop so that its condition can never become false.

Java Infinite For Loop Syntax with for (;;)

The most direct syntax for an intentional infinite for loop is to omit all three expressions:

</>
Copy
for (;;) {
    // statements to repeat
}

The two semicolons are still required because they separate the initialization, condition, and update sections of a Java for statement. When the condition is omitted, the loop behaves as though its condition is always true.

You can also write an explicit always-true condition:

</>
Copy
for (; true; ) {
    // statements to repeat
}

Flowchart of a Java Infinite For Loop

Following is the flowchart of infinite for loop in Java.

Java Infinite For Loop

As long as the loop condition remains true, execution follows the loop path again. If the condition can never become false, program control does not continue to the statement after the loop unless the loop is exited explicitly, for example with break.

Example 1 – Java Infinite For Loop with True for Condition

A Java for loop condition is a boolean expression. Therefore, using the boolean value true as the condition creates an infinite loop unless some statement inside the loop exits it.

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

The output shown above is only the beginning. The program continues printing hello because the condition never becomes false. If you are running the program from a command prompt or terminal, you can usually stop it with Ctrl+C. If you are using an IDE, use its stop or terminate control.

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

Instead of using the literal true, the loop can use an expression that always evaluates to true. For example, 1 == 1 and 0 == 0 never become false.

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

This loop is functionally infinite for the same reason as for (; true; ): its condition is true every time Java evaluates it. For intentional infinite loops, for (;;) or for (; true; ) communicates the intent more directly than an arbitrary expression such as 1 == 1.

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

An accidental infinite for loop often occurs when a variable used in the condition is not updated.

In the following example, i starts at 10 and the loop condition is i > 0. To make the loop end, i would need to change until the condition becomes false. Because the update section is empty and i is not changed in the loop body, its value remains 10.

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

Since i stays at 10, the condition i > 0 remains true and the output continues indefinitely.

Fixing a Java Infinite For Loop Caused by a Missing Update

If the intention is to run the previous loop ten times, add an update that moves i toward the terminating condition. In this case, decrement i after each iteration.

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

Here, i-- reduces i by one after each iteration. Once i becomes 0, the condition i > 0 is false and the loop ends.

Java Infinite For Loop Using for (;;)

Java also allows the condition to be omitted completely. When a for loop has no condition, Java does not have a false condition that would stop normal iteration.

</>
Copy
public class InfiniteForLoop {
    public static void main(String[] args) {
        for (;;) {
            System.out.println("hello");
        }
    }
}

Output

hello
hello
hello
hello
...

The ellipsis indicates that the program keeps producing the same line. This form is useful when the loop is deliberately intended to continue until an internal event or exit statement stops it.

Stopping a Java Infinite For Loop with break

An infinite for loop can contain an internal condition that exits the loop with break. The break statement immediately transfers control to the first statement after the nearest enclosing loop.

</>
Copy
public class InfiniteForLoopWithBreak {
    public static void main(String[] args) {
        int count = 1;

        for (;;) {
            System.out.println(count);

            if (count == 5) {
                break;
            }

            count++;
        }

        System.out.println("Loop ended");
    }
}

Output

1
2
3
4
5
Loop ended

The for (;;) statement itself has no terminating condition, but the loop stops when count == 5 because the break statement is executed.

Why a Java For Loop Becomes Infinite by Mistake

If a for loop runs forever unexpectedly, check the condition and the values that affect it. Common causes include:

  • Missing update: a variable in the condition is never changed.
  • Wrong update direction: the loop increases a value when it needs to decrease it, or decreases a value when it needs to increase it.
  • Always-true condition: the condition cannot become false for the values produced by the loop.
  • Update does not affect the condition: the loop changes one variable while the condition depends on another variable that never changes.
  • Exit logic is unreachable: a break statement exists, but the condition guarding it can never be satisfied.

Java Infinite For Loop When the Update Changes the Wrong Variable

A for loop can have an update expression and still be infinite when that expression does not change the variable used by the loop condition.

</>
Copy
public class WrongVariableForLoop {
    public static void main(String[] args) {
        for (int i = 0, j = 0; i < 5; j++) {
            System.out.println(j);
        }
    }
}

The condition checks i < 5, but the update expression changes j. Since i remains 0, the condition stays true and the loop does not terminate normally. To fix the loop, update the variable that controls the condition, for example by using i++ when that matches the intended logic.

How to Debug an Accidental Java Infinite For Loop

When a Java for loop does not terminate as expected, trace how its condition changes from one iteration to the next.

  1. Read the loop condition and identify every variable used in it.
  2. Check the initialization values for those variables.
  3. Check the update expression and any updates inside the loop body.
  4. Confirm that the updates move the program toward making the loop condition false.
  5. Inspect conditional branches and continue statements that may skip an update in the loop body.
  6. Temporarily print relevant values or use an IDE debugger to see how they change on each iteration.
  7. If the loop is intentionally infinite, verify that its intended exit mechanism, such as break, can actually be reached.

Java Infinite For Loop and Infinite While Loop Compared

Both for (;;) and while (true) can represent intentional infinite loops in Java. The main difference is syntax rather than the fact that they repeat indefinitely. A for loop is often chosen when initialization and update logic naturally belong in the loop header, while a while loop expresses repetition around a boolean condition directly.

</>
Copy
for (;;) {
    // repeat indefinitely
}

while (true) {
    // repeat indefinitely
}

For either form, make the termination path clear when the loop is expected to stop under a specific condition.

Java Infinite For Loop Editorial QA Checklist

  • Verify that each loop described as infinite really lacks a reachable false condition during the behavior being explained.
  • For accidental infinite-loop examples, identify the exact control variable or condition that fails to progress toward termination.
  • For for (;;) examples, keep both semicolons because they are required by Java’s for statement syntax.
  • When an internal break is used, verify that its condition can be reached.
  • Describe output from truly infinite examples as partial output rather than complete output.
  • Check update expressions carefully so that examples intended to terminate move their control variables toward a false condition.

Java Infinite For Loop Summary

In this Java Tutorial, we learned that a for loop becomes infinite when its condition never becomes false or when the condition is omitted. We used true, an always-true expression, and for (;;) to create infinite loops, examined missing and incorrect updates that can cause accidental infinite loops, and used break to exit an otherwise infinite loop.