Start Coding Now

Nested Loops in Java - Loops Inside Loops

Understand nested loops in Java. Learn how to place a loop inside another loop with examples.

Nested Loops

It is possible to place a loop inside another loop. This is called a nested loop.

The "inner loop" will be executed one time for each iteration of the "outer loop".

// Outer loop
for (int i = 1; i <= 2; i++) {
  System.out.println("Outer: " + i);
  
  // Inner loop
  for (int j = 1; j <= 3; j++) {
    System.out.println(" Inner: " + j);
  }
}

Frequently Asked Questions