Empty Loop in Java
Table of Content:
🔹 Definition:
If a loop does not contain any statement in its loop-body, it is called an empty loop.
In such cases, Java implicitly includes a null statement (;) as the loop body.
That means the loop executes but performs no operation during each iteration.
🧩 Syntax:
for (initialization; condition; update);
The semicolon (
;) after the loop header represents a null statement (empty body).
✅ Example 1: Simple Empty Loop
public class EmptyLoopExample { public static void main(String[] args) { int j; // Empty for loop for (j = 20; j >= 0; j--); System.out.println("Loop completed, final value of j = " + j); } }
🧠 Explanation:
-
The loop body is empty (only a semicolon
;). -
The variable
jstarts from 20 and keeps decreasing until it becomes -1. -
The loop does not perform any action inside — it just updates the variable.
💬 Output:
Loop completed, final value of j = -1
🕒 Example 2: Empty Loop as a Time Delay Loop
Empty loops are sometimes used to create a delay, especially in embedded systems or simple console programs (though not recommended in real applications today).
public class TimeDelayExample { public static void main(String[] args) { int t; System.out.println("Please wait..."); // Empty loop to create time delay for (t = 0; t < 300000000; t++); // Loop does nothing System.out.println("Done!"); } }
⚙️ Explanation:
-
This loop runs 300 million times doing nothing — just consuming CPU time.
-
It introduces a time delay before printing
"Done!". -
In real programs,
Thread.sleep(milliseconds)is preferred instead of using empty loops for delays.
📘 In short:
| Feature | Description |
|---|---|
| Loop body | Contains only a null statement (;) |
| Purpose | To repeat variable updates without performing actions |
| Common use | Time delay loops or waiting for a condition change |