April 19, 2026
Table of Contents
Loops in Java are control flow statements that allow a block of code to be executed repeatedly based on a specified conditions. The loop continues execution until the condition evaluates to false, thereby eliminating the need for redundant code and improving efficiency.
a) Initialization: -> Initialization is the step where a loop-control variable (controls how many times the loop executes) is declared and assigned its starting value before the loop begins execution. This variable is used to track how many times the loop has executed.
For example, int i = 0 means the loop starts counting from 0.
b) Condition: -> The condition is a boolean expression involving the loop-control variable that is checked before each iteration (in for and while loops). The loop continues to execute as long as this condition evaluates to true, and stops when it becomes false.
For example, i < 5 means the loop runs while i is less than 5.
a) Update Expression: -> The update expression refers to the part where the loop control variable is modified after each iteration of the loop body. This update ensures that the loop moves towards its termination condition.
For example, i++ increases the value of i by 1 after each iteration.
There are primarily four types of loops in Java:
Each of these has its own syntax, behavior, and ideal use case.
The for loop is one of the most commonly used loops in Java, designed for situations where the number of iterations is known in advance. Among all the other loops, it is considered compact because it combines initialization, condition, and update in a single line.
Syntax:
for (initialisation ; condition ; update) {
// code block
} How it works:
For example:
class Test {
public static void main(String[] args) {
for (int i = 0 ; i<5 ; i++) {
System.out.println(i);
}
} Output:
0
1
2
3
4 Explanation:
Syntax:
for ( ; ; ) {
System.out.println("Infinite loop");
} Output:
Infinite Loop
Infinite Loop
Infinite Loop
Infinite Loop
.
.
.
(till infinity) Explanation:
Syntax:
for (int i=0,j=10 ; i<j ; i++,j--) {
System.out.println(i + " " + j);
} Output:
0 10
1 9
2 8
3 7
4 6 Supports multiple initializations and updates and is used for complex iteration logic.
Explanation:
A nested for loop refers to placing one for-loop inside another. When the outer loop runs one iteration, control moves to the inner loop, which then executes completely from start to finish. Only after the inner loop finishes all its iterations does the program return to the outer loop for the next iteration.
class Test {
public static void main(String[] args) {
for (int i=0 ; i<=2 ; i++) {
for (int j=0 ; j<=2 ; j++) {
System.out.print(i + " " + j + " , ");
}
}
}
} Output:
0 0 , 0 1 , 0 2 , 1 0 , 1 1 , 1 2 , 2 0 , 2 1 , 2 2 Explanation:
To explore more about loops in Java, you can refer to the official documentation for Java for loop.
The while loop is an entry-controlled loop that repeatedly executes a block of code as long as the specified condition remains true. It is typically used when the number of iterations is not known in advance.
Syntax:
while (condition) {
// code block
} How it works:
For example:
class Test {
public static void main(String[] args) {
while (true) {
System.out.println("Infinite loop");
}
}
} Output:
Infinite Loop
Infinite Loop
Infinite Loop
Infinite Loop
.
.
.
(till infinity) Explanation:
class Test {
public static void main(String[] args) {
while (true) {
System.out.println(i);
i++;
}
}
} Output:
0
1
2
3
4
.
.
.
(till infinity) Explanation:
The do-while loop is similar to the while loop but guarantees that the loop body executes at least once regardless of the condition. In this type of loop, the condition is evaluated after the execution of the loop body.
Syntax:
do {
// code block
} while (condition); How it works:
For example:
class Test {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
}
} Output:
0
1
2
3
4 Explanation:
To explore more about loops in Java, you can refer to the official documentation for Java while and do-while loop.
The enhanced for loop (also called the for-each loop) is a simplified loping construct used to iterate over elements of arrays and collections sequentially, without using an index variable.
Syntax:
for (dataType variable : collection) {
// code block
} How it works:
For example:
class Test {
public static void main(String[] args) {
int[] arr = {10, 20, 30};
for (int x : arr) {
System.out.println(x);
}
}
} Output:
10
20
30 Explanation:
Limitations of Enhanced for loop:
When working with loops in Java, sometimes situations arise where you may want to exit a loop early, skip certain iterations, or control the flow of execution more precisely. This is where loop control statements in Java become important.
Loop control statements make the program more efficient and flexible. Instead of strictly following the loop condition from start to end, these statements give you the ability to control how and when the loop should continue or stop.
Java provides three main loop control statements:
Let us now understand each loop control statement in detail.
In Java, the break statement is used to immediately terminate the loop, regardless of whether the loop condition is still true. Once a break statement is executed, control moves to the next statement after the end of the loop.
For example:
class Test {
public static void main(String[] args) {
for (int i = 0 ; i<5 ; i++) {
if(i == 3) {
break;
}
System.out.println(i);
}
}
} Output:
0
1
2 Explanation:
Use cases:
Common Mistakes:
In Java, the continue statement is used to skip the current iteration and move directly to the next iteration of the loop.
For Example:
class Test {
public static void main(String[] args) {
for (int i=0 ; i<5 ; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}
}
} Output:
0
1
3
4 Explanation:
Use cases:
Common Mistakes:
When using break and continue in nested loops in Java, They affect only the innermost loop by default. To control outer loops instead, labeled statements are used.
A label is like giving a name to the outer loop so you can directly control it.
Example for Labeled break:
class Test {
public static void main(String[] args) {
outer:
for (int i=0 ; i<3 ; i++) {
for (int j=0 ; j<3 ; j++) {
if (j == 2) {
break outer;
}
System.out.println(i + " " + j);
}
}
}
} Output:
0 0
0 1 Explanation:
Example for Labeled continue:
class Test {
public static void main(String[] args) {
outer:
for (int i=0 ; i<3 ; i++) {
for (int j=0 ; j<3 ; j++) {
if (j == 2) {
continue outer;
}
System.out.println(i + " " + j);
}
}
}
} Output:
0 0
0 1
1 0
1 1
2 0
2 1 Explanation:
To explore more about loops in Java, you can refer to the official documentation for Java branching statements.
NOTE:
Unlike break which exits a loop, the return statement exits the entire method and stops all further execution. We will explore the return statement in detail in a later tutorial on Java methods.
Understand the Stack, Master the System.
Clear explanations of programming fundamentals and problem-solving strategies.
© 2026 deepinthestack.com. All rights reserved.