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

Coding with Loops: Repeating Events

C3.1 A1About 45 minutes
Learning goals
  • I can write code with steps in order (sequential), steps at the same time (concurrent) and steps that repeat.
  • I can use a loop to make my code shorter.
  • I can run (execute) code with a loop and tell the outcome.
  • I can write code with a loop to solve a math problem, like skip counting or multiplying.

Words to know

code
Instructions written so a computer or robot can follow them.
sequential events
Steps that happen one after another, in order.
concurrent events
Things that happen at the same time.
repeating events
Steps that happen again and again.
loop
A part of the code that repeats. REPEAT 3 times is a loop.
execute (run)
To follow the code to see what happens.

Remember from Grade 2

In Grade 2 you wrote code for Beep (A) and Boop (B), the robots. You used:

  • Sequential events: steps that happen one after another.
  • Concurrent events: two robots doing their steps at the same time.

This year you will learn a coding superpower: the loop, which makes steps repeat.

Why we need loops

Beep needs to climb a staircase to the star. Each move is one square.

A
Beep (A) starts in the bottom-left square. The dashed line shows the staircase path to the star: right 1, up 1, three times.

Here is code with no loop. It works, but it is long:

WHEN GO is pressed
➡ Move right 1
⬆ Move up 1
➡ Move right 1
⬆ Move up 1
➡ Move right 1
⬆ Move up 1
STOP

Six movement lines. Do you see a pattern? Right 1, up 1 repeats 3 times.

When a group of steps repeats, we can put them in a loop. The steps inside the loop are pushed in (indented). The loop runs them again and again.

WHEN GO is pressed
REPEAT 3 times
➡ Move right 1
⬆ Move up 1
STOP

The same staircase with a loop. Lines 3 and 4 are inside the loop, so they run 3 times.

How to run a loop

Run the steps inside the loop, top to bottom. Then go back to the top of the loop. Count each time through. When you have done it the right number of times, go on to the line after the loop.

It is a lot like a repeating pattern: the steps inside the loop are the core.

Worked example

Example 1: Run the staircase code

Run the loop code above. Does Beep reach the star?

  1. Time 1: right 1, up 1. Beep is 1 right and 1 up from the start.
  2. Time 2: right 1, up 1. Beep is 2 right and 2 up.
  3. Time 3: right 1, up 1. Beep is 3 right and 3 up.
  4. The loop is done 3 times, so we go to STOP.

Yes. Beep ends 3 squares right and 3 squares up, right on the star.

Loops for math

Loops are great for math, because many math ideas repeat. Skip counting is adding the same number again and again!

START with number = 0
REPEAT 5 times
ADD 25 to number
SAY number
STOP

A skip-counting loop.

Worked example

Example 2: Run a counting loop

What does the code above say? What is the outcome?

  1. Start: number = 0.
  2. Time 1: 0 + 25 = 25. Say 25.
  3. Time 2: 25 + 25 = 50. Say 50.
  4. Time 3: 75. Time 4: 100. Time 5: 125.

It says 25, 50, 75, 100, 125. The outcome is 125. It is counting by 25s, like counting quarters!

Worked example

Example 3: Write code to solve a problem

Tickets to the winter carnival cost $3 each. Write code that finds the cost of 6 tickets.

  1. Each ticket adds $3, so ADD 3 is the step that repeats.
  2. There are 6 tickets, so the loop repeats 6 times.
  3. Code: START with cost = 0, REPEAT 6 times: ADD 3 to cost, SAY cost.
  4. Run it: 3, 6, 9, 12, 15, 18.

The code says 18. 6 tickets cost $18. (That is 6 × 3 = 18.)

START with cost = 0
REPEAT 6 times
ADD 3 to cost
SAY cost
STOP

Code for the ticket problem. It shows that 6 × 3 means adding 3 six times.

Loops at the same time

We can mix ideas! When GO is pressed, both robots start their code at the same time (concurrent). Each robot has its own loop. On each beat, each robot does one step.

AB
Beep (A) starts at the bottom left. Boop (B) starts at the bottom right. There are two stars: one at the top of Beep's column, and one 3 squares to the left of Boop.
Beep (A)Boop (B)
WHEN GO is pressedWHEN GO is pressed
REPEAT 3 timesREPEAT 3 times
⬆ Move up 1 ⬅ Move left 1
Two pieces of code that run at the same time.
Worked example

Example 4: Run two loops at once

Run both codes. Where does each robot end? Do they ever bump into each other?

  1. Beat 1: Beep moves up 1. At the same time, Boop moves left 1.
  2. Beat 2: Beep up 1 more (2 up). Boop left 1 more (2 left).
  3. Beat 3: Beep is 3 up. Boop is 3 left.
  4. Beep leaves the bottom row on beat 1. Boop stays in the bottom row and stops 2 squares away from Beep's starting square. Beep is never in the bottom row when Boop gets close, so they never meet.

Beep ends on the top star (3 up). Boop ends on the bottom star (3 left). They never bump.

Just for fun

What if you forgot to tell your loop when to stop? REPEAT forever: Dance would keep your robot dancing all night long! That's why loops say how many times.

Drawing shapes with a loop

Some robots carry a pen and draw as they move. This robot can Move forward (in the way it is facing) and Turn right (a quarter turn). To draw a square, it does the same two steps 4 times.

WHEN GO is pressed
PEN DOWN
REPEAT 4 times
Move forward 3
Turn right (quarter turn)
STOP

A square has 4 equal sides and 4 square corners, so the loop repeats 4 times.

start3 steps3 steps3 steps
The robot starts at the bottom-left corner facing up. It draws a square with sides of 3 steps.

Your turn

1

What is a loop in code?

2

How many times does Beep move? REPEAT 5 times: Move right 1

3

Run this code: START with number = 0, REPEAT 4 times: ADD 10, SAY number. What does it say?

4

Run this code: START with number = 100, REPEAT 3 times: SUBTRACT 20, SAY number.

5

Write a short loop that moves Beep up 6 squares, 1 square at a time.

6

Rewrite this code with a loop: Move right 2, Move right 2, Move right 2, Move right 2

7

Two robots run their code at the same time. Beep: REPEAT 4 times: Move up 1. Boop: REPEAT 4 times: Move right 1. After 4 beats, how far has each one moved?

Hands-on activity

Be the robot

You need: a grown-up, some floor space, and a small toy or paper star as the target.

  1. Stand in an open space. You are the robot! Each step forward is one move.
  2. A grown-up puts the toy on the floor, a few steps away.
  3. Write code on paper to reach it, using at least one loop. For example: REPEAT 3 times: step forward, then Turn right, then REPEAT 2 times: step forward.
  4. The grown-up reads your code out loud, line by line, and you follow it exactly.
  5. Did you reach the toy? If not, fix your code and try again!
  6. Bonus: write a loop to walk in a square: REPEAT 4 times: step forward 2, turn right.

Safety: move slowly, and clear the space of things you could trip on.

Math talk: "Which part of my code repeats? How many times?"

More practice: warm-up

1

What are steps that happen one after another called?

2

What are steps that happen at the same time called?

3

How many times do the steps run in REPEAT 7 times?

4

Run: START with number = 0, REPEAT 3 times: ADD 5, SAY number.

5

Which is shorter: 8 lines of Move up 1, or REPEAT 8 times: Move up 1?

More practice: on your own

1

Run: START with number = 0, REPEAT 4 times: ADD 50, SAY number. What pattern does it make?

2

Write code with a loop that finds 5 × 4. Run it.

3

Beep runs REPEAT 2 times: Move right 2, Move up 1. Where does Beep end, compared to the start?

4

A robot draws with REPEAT 4 times: Move forward 5, Turn right (quarter turn). What shape does it draw? How long is each side?

5

Write code that counts backward from 50 by 10s and says each number until it reaches 0.

Challenge

1

Code: START with number = 1, REPEAT 4 times: DOUBLE number, SAY number. What does it say?

2

Beep has to collect 3 stars. They are 2, 4 and 6 squares to the right of Beep. Write code with a loop and a COLLECT step.

3

Two robots run at the same time. Beep: REPEAT 3 times: Move right 2. Boop starts 10 squares to the right of Beep and runs REPEAT 3 times: Move left 1. How far apart are they at the end?

Check your understanding

Quiz

1.

What does a loop do in code?

2.

Run: START with number = 0, REPEAT 3 times: ADD 4, SAY number. What does it say?

3.

Which code moves Beep right 6 squares?

4.

Steps that happen at the same time are called…

5.

A pen robot runs REPEAT 4 times: Move forward 2, Turn right (quarter turn). What does it draw?

6.

Which code finds 3 × 5?

For parents and teachers

Big idea: Grade 3 coding (C3.1) adds repeating events (loops) to the sequential and concurrent events from Grade 2. A loop is like the core of a repeating pattern: a set of steps that runs a given number of times. Loops connect nicely to skip counting and multiplication (6 × 3 = adding 3 six times).

Common difficulties:

  • Running the loop only once, or running the whole program again instead of only the lines inside the loop. Point to the indented lines.
  • Losing count. Encourage tally marks for each time through the loop.
  • Concurrent code. Act it out: you and your child each follow one robot's code, one step per clap.

How to help: No computer is needed. Play "Be the robot", write loops for dance moves (REPEAT 4 times: clap, stomp) or for daily routines. If you do use a device, free block-based coding apps for children also have repeat blocks.