Sometimes one loop is not enough to solve a problem. For example, imagine you want to print the seats in a movie theater where there are multiple rows and each row has multiple seats. You need one loop to go through the rows and another loop to go through the seats in each row.
When we place one loop inside another loop, it is called a nested loop.
What is a Nested Loop?
A nested loop is simply a loop inside another loop.
For example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println("i = " + i + ", j = " + j);
}
}
Here, the outer for loop contains another for loop. The first loop is called the outer loop, and the loop inside it is called the inner loop.
The important thing to understand is that the inner loop runs completely for every single iteration of the outer loop.
How Does It Work?
Let's make the example a little simpler:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println(j);
}
}
The outer loop runs 3 times.
But every time the outer loop runs, the inner loop also runs 3 times.
So the inner loop runs:
3 × 3 = 9 times
The execution roughly looks like this:
Outer loop i = 1
Inner loop j = 1
Inner loop j = 2
Inner loop j = 3
Outer loop i = 2
Inner loop j = 1
Inner loop j = 2
Inner loop j = 3
Outer loop i = 3
Inner loop j = 1
Inner loop j = 2
Inner loop j = 3
This is the most important idea behind nested loops.
Printing a Pattern
Nested loops are commonly used to create patterns.
For example, let's print a rectangle of stars:
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 4; column++) {
System.out.print("* ");
}
System.out.println();
}
Output:
* * * *
* * * *
* * * *
Here, the outer loop controls the rows, while the inner loop controls the columns.
The inner loop prints four stars for each row, and the outer loop repeats that process three times.
Nested Loops with Numbers
We can also use nested loops to print numbers.
For example:
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 4; column++) {
System.out.print(column + " ");
}
System.out.println();
}
Output:
1 2 3 4
1 2 3 4
1 2 3 4
Again, the outer loop controls how many rows we have, while the inner loop controls what happens inside each row.
Nested Loops with Different Values
We can also use the outer and inner loop variables together.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println(i + " " + j);
}
}
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Notice how j goes from 1 to 3 every time i changes.
That's because the inner loop starts again from the beginning for every iteration of the outer loop.
Real-World Example
Let's go back to our movie theater example.
Imagine there are 3 rows and each row has 4 seats. We can use nested loops to display every seat:
for (int row = 1; row <= 3; row++) {
for (int seat = 1; seat <= 4; seat++) {
System.out.println("Row " + row + ", Seat " + seat);
}
}
The program will go through every seat in every row.
This is exactly the kind of situation where nested loops are useful: one thing contains multiple groups of another thing.
Nested while Loops
Nested loops are not limited to for loops. We can also put a while loop inside another while loop.
int row = 1;
while (row <= 3) {
int column = 1;
while (column <= 3) {
System.out.print("* ");
column++;
}
System.out.println();
row++;
}
Output:
* * *
* * *
* * *
The idea is exactly the same: one loop controls the outer repetition, while the other handles the repetition inside it.
One Important Thing to Remember
Nested loops can become expensive when the number of iterations becomes very large.
For example, if the outer loop runs 1,000 times and the inner loop also runs 1,000 times, the inner code may execute:
1,000 × 1,000 = 1,000,000 times
So while nested loops are extremely useful, we should use them carefully when working with large amounts of data.
The easiest way to remember nested loops is:
Outer loop → controls the bigger repetition
Inner loop → completes its work for every outer iteration
Once you understand this relationship, patterns, tables, grids, and many other programming problems become much easier to solve.