bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Tutorial
Java•Java Tutorial

Java Nested Loops

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java Nested Loops?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// ___ loop
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The "inner loop" will be executed one time for each iteration of the "outer loop":
It is also possible to place a loop inside another loop.
Multiplication Table Example

Nested Loops

It is also 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":

Example

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

Multiplication Table Example

This example uses nested loops to print a simple multiplication table (1 to 3):

Example

for (int i = 1; i <= 3; i++) {
  for (int j = 1; j <= 3; j++) {
    System.out.print(i * j + " ");
  }
System.out.println();
}

Nested loops are useful when working with tables, matrices, or multi-dimensional data structures .

Previous

Java For Loop

Next

Java For Each Loop