// Nov 2026

Pyramid Star Pattern Using Nested Loops: Spaces, Stars and the Flowchart

Instructor smiling beside the title Spaces + Stars and a growing row of stars: 1, 3, 5

Split every row into spaces and stars, find one formula for each, and the flowchart with one outer loop and two inner loops follows

In the previous lesson we printed a simple star triangle with two loops. This time the pattern is a step harder: a pyramid star pattern, centred, with 1 star at the top and 9 stars in the fifth row.

The method is the one that solves every complex pattern: break the logic down first. Every row is two jobs, spaces and then stars. Find a formula for each, and the flowchart almost draws itself.

The pyramid star pattern we want

    *
   ***
  *****
 *******
*********

Five rows. The stars grow by two each row (1, 3, 5, 7, 9), and the spaces in front shrink by one (4, 3, 2, 1, 0). The spaces are what centre the pyramid.

Step 1: count spaces and stars in every row

Draw the pattern on a grid (rows down the side, columns across) and count:

Smart board: the slide 'Part 1: build the top half of the star' with the 5-row pyramid drawn on a grid, and hand-written tables of outer loop rows 1-5 with spaces 4, 3, 2, 1, 0 and stars 1, 3, 5, 7, 9
Counting first: spaces 4 to 0 (IL1 = 5 – OL) and stars 1 to 9 (IL2 = 2 x OL – 1).

| Row (OL) | Spaces | Stars | |—|—|—| | 1 | 4 | 1 | | 2 | 3 | 3 | | 3 | 2 | 5 | | 4 | 1 | 7 | | 5 | 0 | 9 |

The outer loop (OL) goes through the rows. Each row then needs two inner loops: inner loop 1 (IL1) for the spaces, inner loop 2 (IL2) for the stars.

Step 2: the spaces loop, IL1 <= 5 – OL

Look at the spaces column: 4, 3, 2, 1, 0. Each value is 5 minus the row number: 5 – 1 = 4, 5 – 2 = 3, and so on down to 5 – 5 = 0.

So inner loop 1 runs while IL1 <= 5 – OL. That one line is the relation between the spaces loop and the outer loop.

Step 3: the stars loop, IL2 <= 2 x OL – 1

The stars column is 1, 3, 5, 7, 9: the odd numbers. Row n has 2n – 1 stars: 2 x 1 – 1 = 1, 2 x 2 – 1 = 3, 2 x 3 – 1 = 5, 2 x 4 – 1 = 7, 2 x 5 – 1 = 9.

So inner loop 2 runs while IL2 <= 2 x OL – 1.

Step 4: the flowchart, one outer loop and two inner loops

  1. Start, then OL = 1.
  2. OL <= 5? If no, End.
  3. IL1 = 1, then IL1 <= 5 – OL? Yes: print space, IL1 = IL1 + 1, and back to the IL1 decision.
  4. When IL1’s condition is no: IL2 = 1, then IL2 <= 2 x OL – 1? Yes: print star, IL2 = IL2 + 1, and back.
  5. When IL2’s condition is no: move to a new line, OL = OL + 1, and back to the OL decision.
The finished hand-drawn flowchart on the smart board: the spaces loop on the left and the stars loop on the right, joined by the outer loop
The finished flowchart: the spaces loop on the left, the stars loop on the right, inside one outer loop.

On the board in the video, two boxes are labelled ‘OL2’ by mistake: the increment after ‘print space’ is IL1 = IL1 + 1, and the second inner loop starts with IL2 = 1. The video shows a correction label at both places. There is only one outer loop.

Dry run of the first two rows

| OL | IL1 prints | IL2 prints | Row | |—|—|—|—| | 1 | 4 spaces (IL1 = 1..4) | 1 star (IL2 = 1) | * | | 2 | 3 spaces (IL1 = 1..3) | 3 stars (IL2 = 1..3) | * |

In every turn of the outer loop both inner loops run once each: first all the spaces, then all the stars, then a new line.

From flowchart to code

In pseudo-code the same flowchart is three loops:

for OL = 1 to 5
    for IL1 = 1 to 5 - OL
        print " "
    for IL2 = 1 to 2 * OL - 1
        print "*"
    print new line

Once the two formulas are found, nothing hard is left: the flowchart and the code are translation.

Video chapters

  • 0:00 A harder star pattern
  • 0:29 Count spaces and stars in each row
  • 1:53 Spaces loop: inner loop 1 = 5 – outer loop
  • 2:37 Stars loop: inner loop 2 = 2 x outer loop – 1
  • 4:12 Drawing the flowchart
  • 7:27 Wrap-up: break the logic down first

Watch the short versions

Short on time? These Shorts from this lesson each make one point in under a minute.

Key takeaways

  • Break a complex pattern into what each row prints: here, spaces and then stars.
  • Spaces: inner loop 1 runs 5 – OL times (4, 3, 2, 1, 0).
  • Stars: inner loop 2 runs 2 x OL – 1 times (1, 3, 5, 7, 9).
  • One outer loop for rows, two inner loops inside it, then a new line.
  • Find the relation between each inner loop and the outer loop first; the flowchart and code follow.

Frequently asked questions

Why does the pyramid need spaces?

The spaces push each row to the right so the stars sit in the centre. Without them you get a left-aligned triangle.

Where does 2n – 1 come from?

The star counts are 1, 3, 5, 7, 9: they grow by 2 per row and start at 1, which is 2n – 1 for row n.

How do I print 7 rows instead of 5?

Use OL <= 7 and 7 – OL spaces; the stars formula 2 x OL – 1 stays the same.

Further reading


Practice: print the upside-down pyramid (9 stars first, 1 star last). What are the two formulas now?

Watch the video, subscribe to the channel and follow the series in order.

More in this series