Learn Java
import com.learnjava.controlflow.NestedLoops;

Nested Loops in Java

@Concept

A nested loop is simply a loop written inside the body of another loop, and it's the standard way to work across two dimensions at once — rows and columns in a grid, or every pair from two separate lists. The outer loop runs once per row, so to speak, and for every single one of those outer iterations, the entire inner loop runs completely from start to finish before the outer loop moves on to its next iteration.

Total iteration count multiplies rather than adds — an outer loop running 3 times paired with an inner loop running 4 times each runs the inner body 12 times total, not 7. That matters directly for reasoning about a nested loop's performance as the inputs grow.

@HowItWorks
1

The basic shape

One loop's body contains an entirely separate loop, with its own counter and its own condition.

2

Outer loop controls repetition of the whole inner loop

Each single iteration of the outer loop runs the complete inner loop from start to finish.

3

Iteration counts multiply

An outer loop of size m paired with an inner loop of size n runs the innermost body m times n times in total.

4

A natural fit for grids

Nested loops are the standard way to visit every cell of a two-dimensional structure, row by row and column by column.

5

Watch performance as inputs grow

Because counts multiply, a nested loop over two inputs of size n each does n times n work, which grows quickly as n increases.

@Example
public class NestedLoopDemo {
public static void main(String[] args) {
for (int row = 1; row <= 2; row++) {
for (int col = 1; col <= 3; col++) {
System.out.println("Row " + row + ", Col " + col);
}
}
}
}
Output
Row 1, Col 1
Row 1, Col 2
Row 1, Col 3
Row 2, Col 1
Row 2, Col 2
Row 2, Col 3
1When row is 1, the entire inner loop runs from col equal to 1 through 3, printing three lines before the outer loop moves on.
2Only once that complete inner pass finishes does row become 2.
3The inner loop then runs completely again, from col 1 through 3, for this second value of row.
4Two outer iterations times three inner iterations gives six lines of output in total, not five.
@Complexity
time O(m × n)
space O(1)

Work scales with the product of the two loop bounds, not their sum — doubling either one roughly doubles the total number of times the inner body runs.

@UseCases
Visiting every cell of a two-dimensional array or grid, row by row
Comparing every pair of elements from two separate lists
Printing a multiplication table or other two-axis pattern
Estimating how a nested loop's total work grows as both inputs get larger

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.