Nested loops and star patterns
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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
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); } } }}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 runsseat= 1, 2, 3. Only then doesrowbecome 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.
Example 2 · Box of 3 rows and 4 stars
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(); } }}* * * * * * * * * * * *
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
Variables
- i
- 1
Output
(nothing printed yet)Line 3Outer start: i = 1. Is 1 <= 2? Yes, so row 1 begins.
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
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(); } }}* * * * * * * * * *
What this code does
- The inner condition
j <= igives row 1 one star, row 2 two stars, and so on. - Only the inner stop changed from the box:
iinstead of a fixed 4.
Example 4 · Number triangle
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(); } }}1 1 2 1 2 3 1 2 3 4
What this code does
- Same loops as the star triangle; the body prints
jinstead of a star. - Print
iinstead 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:
- Number the rows: i = 1, 2, 3, 4.
- For each row, write down what it prints: how many spaces, then how many stars or which numbers.
- Turn each count into a rule using i, such as stars = i or stars = 5 - i.
- 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
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(); } }}* *** ***** *******
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
nand the same rules draw a pyramid of any height.
Next step
Check your understanding
9 quick questions