Understanding Nested Loops in Java
Introduction
Loops are a fundamental concept in programming, allowing us to execute a block of code multiple times. Sometimes, we need a loop within another loop this is known as a nested loop. In this guide, we’ll explore nested loops in Java with examples to illustrate how they work.
What Are Nested Loops?
A nested loop is a loop inside the body of another loop. The inner loop executes completely for each iteration of the outer loop. This structure is useful in scenarios like working with multi-dimensional data structures, pattern printing, and complex iterations.
Example 1: Nested for
Loop
class NestedForLoop {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) { // Outer loop
System.out.println("Outer: " + i);
for (int j = 0; j < 2; j++) { // Inner loop
System.out.println(" Inner: " + j);
}
}
System.out.println("After nested for loops");
}
}
class NestedForLoop {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) { // Outer loop
System.out.println("Outer: " + i);
for (int j = 0; j < 2; j++) { // Inner loop
System.out.println(" Inner: " + j);
}
}
System.out.println("After nested for loops");
}
}
Output:
Outer: 0
Inner: 0
Inner: 1
Outer: 1
Inner: 0
Inner: 1
After nested for loops
Outer: 0
Inner: 0
Inner: 1
Outer: 1
Inner: 0
Inner: 1
After nested for loops
Here, the inner loop runs twice for every iteration of the outer loop. Once the outer loop reaches its condition (i = 2
), the execution stops, and the statement after the loops is executed.
Example 2: while
Loop Inside a for
Loop
class NestedLoop {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
System.out.println("Outer For Loop: " + i);
int counter = 0;
while (counter < 2) { // Inner while loop
System.out.println(" Inner While Loop: " + counter);
counter++;
}
}
}
}
class NestedLoop {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
System.out.println("Outer For Loop: " + i);
int counter = 0;
while (counter < 2) { // Inner while loop
System.out.println(" Inner While Loop: " + counter);
counter++;
}
}
}
}
Output:
Outer For Loop: 0
Inner While Loop: 0
Inner While Loop: 1
Outer For Loop: 1
Inner While Loop: 0
Inner While Loop: 1
Outer For Loop: 0
Inner While Loop: 0
Inner While Loop: 1
Outer For Loop: 1
Inner While Loop: 0
Inner While Loop: 1
Example 3: do...while
Loop Inside a while
Loop
class NestedLoop {
public static void main(String[] args) {
int m = 0;
while (m < 2) { // Outer while loop
System.out.println("Outer While Loop: " + m);
int n = 0;
do { // Inner do-while loop
System.out.println(" Inner Do-While Loop: " + n);
n++;
} while (n < 2);
m++;
}
}
}
class NestedLoop {
public static void main(String[] args) {
int m = 0;
while (m < 2) { // Outer while loop
System.out.println("Outer While Loop: " + m);
int n = 0;
do { // Inner do-while loop
System.out.println(" Inner Do-While Loop: " + n);
n++;
} while (n < 2);
m++;
}
}
}
Output:
Outer While Loop: 0
Inner Do-While Loop: 0
Inner Do-While Loop: 1
Outer While Loop: 1
Inner Do-While Loop: 0
Inner Do-While Loop: 1
Outer While Loop: 0
Inner Do-While Loop: 0
Inner Do-While Loop: 1
Outer While Loop: 1
Inner Do-While Loop: 0
Inner Do-While Loop: 1
Example 4: for
Loop Inside a for
Loop
class NestedLoop {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) { // Outer loop
System.out.println("Outer For Loop: " + i);
for (int j = 0; j < i; j++) { // Inner loop
System.out.println(" Inner For Loop: " + j);
}
}
}
}
class NestedLoop {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) { // Outer loop
System.out.println("Outer For Loop: " + i);
for (int j = 0; j < i; j++) { // Inner loop
System.out.println(" Inner For Loop: " + j);
}
}
}
}
Output:
Outer For Loop: 1
Inner For Loop: 0
Outer For Loop: 2
Inner For Loop: 0
Inner For Loop: 1
Outer For Loop: 3
Inner For Loop: 0
Inner For Loop: 1
Inner For Loop: 2
Outer For Loop: 1
Inner For Loop: 0
Outer For Loop: 2
Inner For Loop: 0
Inner For Loop: 1
Outer For Loop: 3
Inner For Loop: 0
Inner For Loop: 1
Inner For Loop: 2
Here, the inner loop runs incrementally based on the current value of i
. The iterations are:
i = 1
→ Inner loop runs once.i = 2
→ Inner loop runs twice.i = 3
→ Inner loop runs three times.
Summary
- A nested loop is a loop inside another loop.
- The inner loop runs completely for each iteration of the outer loop.
- Loops can be nested in any combination (
for
, while
, do...while
).
- Nested loops are useful for handling multi-dimensional structures and complex iterations.
for
, while
, do...while
).