Grade by Grade
C. Algebra · Patterns, Equations, Inequalities, Coding and Modelling

Coding with Nested Loops

C3.1 A1About 45 minutes
Learning goals
  • I can explain what sequential, concurrent, repeating and nested events are.
  • I can write code with a loop inside another loop.
  • I can run nested-loop code in my head and find the outcome.
  • I can use code to show math, like arrays and skip counting.

Words to know

code
Instructions written for a computer or robot to follow.
sequential events
Steps that happen one after another, in order.
concurrent events
Two or more sets of steps that happen at the same time.
loop (repeating event)
Code that repeats a group of steps a number of times.
nested loop
A loop inside another loop. The inside loop runs all the way through each time the outside loop runs once.
outcome
What happens when the code runs.

Remember from Grade 3

In Grade 3 you wrote code for Beep the robot. You used sequential events (steps in order), concurrent events (two robots moving at the same time) and loops (REPEAT 3 times). Steps inside a loop are pushed in (indented).

In Grade 4 you will learn about nested events: putting a loop inside another loop.

Let's start with a dance

Your class makes up a winter dance: clap, clap, stomp. You do it 3 times.

The clap, clap part repeats, so it can be a loop. And the whole clap, clap, stomp repeats too. That's a loop inside a loop!

WHEN GO is pressed
REPEAT 3 times
REPEAT 2 times
👏 CLAP
🦶 STOMP
STOP

The REPEAT 2 loop is nested inside the REPEAT 3 loop.

How to run a nested loop

  1. Start the outside loop (time 1).
  2. Run the inside loop all the way: clap, clap.
  3. Do the rest of the outside loop: stomp.
  4. Go back to the top of the outside loop (time 2) and do it all again.

The outcome: clap, clap, stomp, clap, clap, stomp, clap, clap, stomp.

How many claps? The inside loop runs 2 claps, and it happens 3 times: 3 × 2 = 6 claps. The stomp is only in the outside loop, so there are 3 stomps.

Read the indents

The indent shows which loop a line belongs to.

  • CLAP is pushed in twice, so it is inside both loops. It runs 3 × 2 = 6 times.
  • STOMP is pushed in once, so it is only inside the outside loop. It runs 3 times.

Nested loops make arrays

Loops are great for math. Look at this code. The inside loop draws one row of dots. The outside loop makes 4 rows.

WHEN GO is pressed
REPEAT 4 times
REPEAT 6 times
DRAW a dot
MOVE right 1
GO to the start of the next row
STOP

Code to draw an array.

row 1row 2row 3row 4
The outcome: 4 rows of 6 dots. DRAW a dot runs 4 × 6 = 24 times.
Worked example

Example 1: Run the array code

How many dots does the code above draw? Explain using the loops.

  1. The inside loop draws 6 dots (one row).
  2. The outside loop runs 4 times, so it makes 4 rows.
  3. 4 rows of 6 is 4 × 6 = 24.

24 dots. A nested loop shows multiplication: outside loop × inside loop.

Nested loops for a robot

Beep (A) needs to get to the star. Beep must go right 3, up 1, then right 3, up 1 again.

A
Beep (A) starts in the bottom-left square. The dashed path goes right 3, up 1, right 3, up 1 to the star.
Worked example

Example 2: Write nested-loop code for Beep

Write code with a nested loop to move Beep to the star.

  1. Without loops it is long: right, right, right, up, right, right, right, up (8 steps).
  2. Move right 1 repeats 3 times in a row. Put it in an inside loop: REPEAT 3 times → move right 1.
  3. The whole group (right 3, then up 1) happens 2 times. Put it in an outside loop: REPEAT 2 times.

WHEN GO is pressed

REPEAT 2 times

    REPEAT 3 times

        ➡ Move right 1

    ⬆ Move up 1

STOP

Beep moves right 3, up 1, right 3, up 1, and lands on the star. That is 8 moves, but only a few lines of code!

Worked example

Example 3: A skip-counting nested loop

What does this code say?

SET total = 0

REPEAT 3 times

    REPEAT 4 times

        ADD 5 to total

    SAY total

  1. Outside time 1: add 5 four times: 5, 10, 15, 20. SAY 20.
  2. Outside time 2: add 5 four more times: 25, 30, 35, 40. SAY 40.
  3. Outside time 3: 45, 50, 55, 60. SAY 60.
  4. ADD 5 ran 3 × 4 = 12 times, and 12 × 5 = 60.

It says 20, 40, 60. The outcome is a total of 60.

Sequential, concurrent, repeating and nested: all together

  • Sequential: lines run in order, top to bottom.
  • Concurrent: two characters run their code at the same time. For example, Beep runs the stair code while Boop runs the clap code.
  • Repeating: a loop repeats steps.
  • Nested: a loop is inside another loop.
Worked example

Example 4: Code a repeating pattern

Write code that draws ⭐⭐⭐🌙 four times in a row.

  1. The core is ⭐⭐⭐🌙. The ⭐ repeats 3 times, so use an inside loop for it.
  2. Then draw 🌙 once.
  3. The core repeats 4 times, so put everything in an outside loop.

REPEAT 4 times

    REPEAT 3 times

        DRAW ⭐

    DRAW 🌙

It draws 4 × 3 = 12 stars and 4 moons.

Just for fun

Why did Beep the robot dance all night long?

Somebody forgot to tell the loop when to stop!

Hands-on activity

Be the robot: nested loop dances

You need: paper, a pencil and some space to move. (Optional: if you have a computer or tablet, a free block-coding app like Scratch or ScratchJr, with a grown-up.)

Step 1. Your grown-up reads the clap, clap, stomp code. You are the robot: do exactly what it says. Count the claps. Was it 6?

Step 2. Write your own nested-loop dance on paper, like:

REPEAT 2 times

    REPEAT 3 times

        JUMP

    SPIN

Step 3. Before your grown-up does your dance, predict how many jumps and spins there will be (6 jumps, 2 spins). Then watch and check.

Step 4. Make a nested loop that draws an array on paper, like 3 rows of 5 Xs. Predict the number of Xs, then draw it.

What to look for: your child can say which steps are inside both loops and multiplies to predict how many times they happen.

Your turn

1

What is the outcome?

REPEAT 2 times

    REPEAT 3 times

        SAY "ho"

    SAY "hey"

2

A nested loop has an outside loop of 5 and an inside loop of 4. How many times does a line inside both loops run?

3

Write a nested loop that draws an array with 3 rows of 7 dots.

4

What does this say?

SET total = 0

REPEAT 2 times

    REPEAT 5 times

        ADD 2 to total

    SAY total

5

What is the difference between concurrent and sequential events?

6

Write code for the pattern 🔺🔺🟩🔺🔺🟩🔺🔺🟩.

7

In the code for question 6, how many times is 🔺 drawn? How many times is 🟩 drawn?

More practice: warm-up

1

What is a nested loop?

2

REPEAT 4 times

    JUMP

How many jumps?

3

Outside loop 2, inside loop 2. How many times does the inside step run?

4

In code, how do you show that a line is inside a loop?

5

Outside loop 3, inside loop 10. How many times does the inside step run?

More practice: on your own

1

Write nested-loop code to move a robot right 2, up 1 three times.

2

What does this say?

SET n = 0

REPEAT 3 times

    REPEAT 2 times

        ADD 10 to n

    SAY n

3

Write code to draw 8 rows of 5 stars. How many stars?

4

A robot must say "tick" 4 times and then "tock" once, and do that 6 times. Write the code. How many ticks?

5

Beep runs a loop, REPEAT 4 times: Move right 1. At the same time, Boop runs a loop, REPEAT 4 times: Move up 1. What kind of events are these?

Challenge

1

Write code that makes the number pattern 5, 10, 15, 20, 25, 30, saying a number each time. Do you need a nested loop? (Hint: one loop is enough here! When would you need a nested loop?)

2

You want a line of code to run 36 times using a nested loop. Give two different pairs of loop numbers.

3

What does this say?

SET total = 0

REPEAT 4 times

    ADD 1 to total

    REPEAT 3 times

        ADD 2 to total

SAY total

Check your understanding

Quiz

1.

What is a nested loop?

2.

CLAP is inside a REPEAT 5 times loop, and that loop is inside a REPEAT 3 times loop. How many claps?

3.

Which events happen at the same time?

4.

What does this code draw?

REPEAT 2 times

    REPEAT 3 times

        DRAW ⭐

    DRAW 🌙

5.

Code to draw an array uses REPEAT 6 outside and REPEAT 7 inside. How many dots?

6.

In nested code, a line is indented once, under the outside loop only. It runs...

For parents and teachers

Big idea: The Ontario curriculum (C3.1) asks Grade 4 students to solve problems and create computational representations of mathematical situations by writing and executing code, including code with sequential, concurrent, repeating and nested events. Nested loops are new in Grade 4. They connect nicely to multiplication: outside loop × inside loop.

Common difficulties:

  • Running the inside loop only once. Remind your child that the inside loop starts over every time the outside loop goes around.
  • Ignoring indents. Have your child trace each indent with a finger to see which loop a line belongs to.
  • Adding instead of multiplying (3 and 5 → 8 claps). Act it out to see why it's 15.

How to help: No computer is needed. Act out code as dances or drawings. If you have a device, free block-based tools such as Scratch or ScratchJr let your child build loops inside loops. Encourage predicting the outcome before running the code, and staying calm when code doesn't work the first time (Strand A: perseverance).