Skip to lesson

Nested loops and star patterns

A nested loop is a loop inside another loop. The outer loop moves from row to row, and for every row the inner loop runs all the way through. This is how tables, grids and star patterns are printed.

A loop inside a loop

A cinema hall has 2 rows of 3 seats, and you want to list every seat. A nested loop is a loop inside a loop: the outer loop picks the row, and for each row the inner loop runs through all the seats.

Example 1 · Every seat in the hall

Seats.javaJava
public class Seats {    public static void main(String[] args) {        for (int row = 1; row <= 2; row++) {            for (int seat = 1; seat <= 3; seat++) {                System.out.println("Row " + row + ", seat " + seat);            }        }    }}
Output
Row 1, seat 1
Row 1, seat 2
Row 1, seat 3
Row 2, seat 1
Row 2, seat 2
Row 2, seat 3

What this code does

  • For row = 1 the inner loop runs seat = 1, 2, 3. Only then does row become 2, and the inner loop starts again from 1.
  • The inner body runs 2 × 3 = 6 times in all.

Give each loop its own counter name, usually i for the outer loop and j for the inner one.

Rows and columns with print and println

In a star pattern each row is one line of text. Use print inside the inner loop so a row's stars stay on one line, then call println() once after it to end the row.

A grid of 3 rows and 4 stars. The outer loop i = 1 to 3 picks the row; the inner loop j = 1 to 4 prints the stars of that row from left to right; println() at the end of each row moves to the next line. 12 stars in all.

Example 2 · Box of 3 rows and 4 stars

StarRectangle.javaJava
public class StarRectangle {    public static void main(String[] args) {        for (int i = 1; i <= 3; i++) {            for (int j = 1; j <= 4; j++) {                System.out.print("* ");            }            System.out.println();        }    }}
Output
* * * *
* * * *
* * * *

What this code does

  • print("* ") keeps the 4 stars of a row on one line, with a space between them.
  • println() sits after the inner loop, so it runs once per row, not once per star.

The picture above builds this box star by star. The stepper below runs a smaller 2 × 3 box.

Trace the code

Two rows of three stars

public class StarBox {
public static void main(String[] args) {
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Variables

i
1

Output

(nothing printed yet)

Line 3Outer start: i = 1. Is 1 <= 2? Yes, so row 1 begins.

Step 1 of 19

Let the row number decide

A right triangle has 1 star in row 1, 2 in row 2 and 3 in row 3, so the inner loop cannot run a fixed number of times. Make its stop depend on the outer counter: j <= i.

Example 3 · Right triangle of stars

StarTriangle.javaJava
public class StarTriangle {    public static void main(String[] args) {        for (int i = 1; i <= 4; i++) {            for (int j = 1; j <= i; j++) {                System.out.print("* ");            }            System.out.println();        }    }}
Output
*
* *
* * *
* * * *

What this code does

  • The inner condition j <= i gives row 1 one star, row 2 two stars, and so on.
  • Only the inner stop changed from the box: i instead of a fixed 4.

Example 4 · Number triangle

NumberTriangle.javaJava
public class NumberTriangle {    public static void main(String[] args) {        for (int i = 1; i <= 4; i++) {            for (int j = 1; j <= i; j++) {                System.out.print(j + " ");            }            System.out.println();        }    }}
Output
1
1 2
1 2 3
1 2 3 4

What this code does

  • Same loops as the star triangle; the body prints j instead of a star.
  • Print i instead and each row repeats its own row number: 1, 2 2, 3 3 3.

For any new pattern, find the rule with a small table first:

  1. Number the rows: i = 1, 2, 3, 4.
  2. For each row, write down what it prints: how many spaces, then how many stars or which numbers.
  3. Turn each count into a rule using i, such as stars = i or stars = 5 - i.
  4. Write one inner loop per rule, then println().

Spaces first: the pyramid

A centred pyramid of 4 rows has 3 spaces and 1 star in row 1, down to 7 stars in row 4. The table method gives spaces = 4 - i and stars = 2 × i - 1: each row needs two inner loops.

Example 5 · Pyramid of 4 rows

Pyramid.javaJava
public class Pyramid {    public static void main(String[] args) {        int n = 4;        for (int i = 1; i <= n; i++) {            for (int s = 1; s <= n - i; s++) {                System.out.print(" ");            }            for (int k = 1; k <= 2 * i - 1; k++) {                System.out.print("*");            }            System.out.println();        }    }}
Output
   *
  ***
 *****
*******

What this code does

  • The first inner loop prints n - i spaces: 3, 2, 1, 0.
  • The second prints 2 × i - 1 stars: 1, 3, 5, 7. Both run inside the same outer round.
  • Change n and the same rules draw a pyramid of any height.

Next step

Check your understanding

9 quick questions