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

Reading, Changing and Debugging Code with Loops

C3.2 A1About 40 minutes
Learning goals
  • I can read code with loops and predict the outcome before I run it.
  • I can change (alter) code, like the number of repeats, and describe how my change affects the outcome.
  • I can find and fix a bug in code with sequential, concurrent and repeating events.
  • I can keep trying when my code does not work the first time.

Words to know

predict
Say what you think will happen before it happens.
alter
To change something.
outcome
What happens at the end when the code is run.
bug
A mistake in code that makes it do the wrong thing.
debug
To find and fix a bug.

Remember from Grade 2

In Grade 2 you read code, changed it and fixed bugs (mistakes). Now our code has loops, so there are new things to read and new kinds of bugs to find!

Real coders read and fix code all the time. A bug is not a failure. It is a puzzle to solve.

The coder's way: predict, run, check

  1. Read the code line by line.
  2. Predict the outcome. Write it down.
  3. Run the code (act it out or trace it on paper).
  4. Check: was your prediction right? If the outcome is wrong, look for the bug.

Change 1: The number of repeats

This code makes Beep climb the staircase from the last lesson.

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

Outcome: Beep ends 3 right and 3 up.

Worked example

Example 1: Alter the loop

Change line 2 to REPEAT 5 times. How does the outcome change?

  1. The steps inside the loop stay the same: right 1, up 1.
  2. Now they run 5 times instead of 3.
  3. Beep moves 5 right and 5 up.

Beep climbs 2 more steps: it ends 5 right and 5 up instead of 3 right and 3 up. Changing the number in REPEAT changes how many times the steps happen.

Change 2: The steps inside the loop

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

Outcome: it says 125.

Worked example

Example 2: Alter a step

Change line 3 to ADD 50 to number. What is the new outcome? Describe the change.

  1. The loop still runs 5 times.
  2. Now it adds 50 each time: 50, 100, 150, 200, 250.
  3. Before, the outcome was 125. Now it is 250.

The outcome is 250. Adding 50 instead of 25 makes each jump twice as big, so the outcome is double.

Change 3: Inside or outside the loop?

It matters where a line is. A line inside the loop (pushed in) runs every time. A line after the loop runs only once.

Code A: SAY inside the loopCode B: SAY after the loop
START with number = 0START with number = 0
REPEAT 4 timesREPEAT 4 times
ADD 10 ADD 10
SAY numberSAY number
Says: 10, 20, 30, 40Says: 40
The same lines, but in Code A, SAY is inside the loop. In Code B, SAY is after the loop.

Change 4: The order of steps

There is a wall (the dark square) next to Beep. Robots cannot go through walls.

A
Beep (A) is in the bottom-left square. The wall is the dark square just to the right of Beep. The star is 2 right and 2 up.
WHEN GO is pressed
REPEAT 2 times
➡ Move right 1
⬆ Move up 1
STOP

Beep's code. Is there a bug?

Worked example

Example 3: Debug the order

Run the code. What goes wrong? Fix it.

  1. Line 3 says Move right 1. But the square to the right of Beep is the wall! Bump! That is the bug.
  2. Idea: switch the order of the two steps inside the loop. Go up first, then right.
  3. New code: REPEAT 2 times: Move up 1, Move right 1.
  4. Run it: up to the 2nd row, right 1, up to the 3rd row, right 1. That is 2 right and 2 up. It never touches the wall.

Switch lines 3 and 4. Beep reaches the star without hitting the wall. Both codes move 2 right and 2 up, but the path is different.

A
The fixed path: up, right, up, right. Beep goes around the wall to the star.
Worked example

Example 4: Debug a shape

Ava wants her pen robot to draw a square. Her code: REPEAT 3 times: Move forward 4, Turn right (quarter turn). What does it draw? Fix it.

  1. Run it: forward 4, turn; forward 4, turn; forward 4, turn. Then the loop stops.
  2. Only 3 sides are drawn. The square is open on one side.
  3. A square has 4 sides, so the loop must run 4 times.

The bug is the 3. Change it to REPEAT 4 times.

Just for fun

Funny but true: In 1947, people working on an early computer found a real moth stuck inside it. They taped it into their notebook as the first actual bug ever found in a computer!

Bug-hunting questions

  • Does the loop repeat the right number of times?
  • Are the steps inside the loop in the right order?
  • Is each line in the right place: inside or outside the loop?
  • For two robots at once: do they bump into each other on the same beat?

Your turn

1

Predict: START with number = 0, REPEAT 6 times: ADD 5, SAY number.

2

Change that code to REPEAT 3 times. What is the new outcome? Describe the change.

3

Beep's code is REPEAT 4 times: Move up 1. The star is 6 squares up. Fix the bug.

4

Code A: REPEAT 3 times: ADD 2, SAY number (SAY inside). Starting at 0, what does it say?

5

Code B: REPEAT 3 times: ADD 2, then SAY number (SAY after the loop). Starting at 0, what does it say?

6

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

Hands-on activity

Bug detective

You need: paper, a pencil, and a grown-up.

  1. The grown-up hides a small toy somewhere in a room.
  2. The grown-up writes code to walk to the toy, using loops, but puts one bug in it on purpose. For example, the real path is REPEAT 5 times: step forward, but they write REPEAT 3 times.
  3. You read the code and predict where it will take you.
  4. Act it out, step by step. Did you reach the toy?
  5. Find the bug, fix the code and try again. Say what your change did: "I changed 3 to 5, so I took 2 more steps."
  6. Switch: you write buggy code for your grown-up!

Safety: clear the floor so no one trips.

Math talk: "How did my change affect the outcome?"

More practice: warm-up

1

What is a bug in code?

2

What does debug mean?

3

REPEAT 4 times: Move right 1. If you change 4 to 6, how many more squares does the robot move?

4

Predict: START with number = 10, REPEAT 2 times: ADD 10, SAY number.

5

True or false: The lines inside a loop run only once.

More practice: on your own

1

Code: START with number = 0, REPEAT 4 times: ADD 3, SAY number. Change it so the outcome is 24. Describe two different ways.

2

Leo wants his code to say 100, 200, 300. His code is START with number = 0, REPEAT 3 times: ADD 100, then SAY number after the loop. It only says 300. Fix it.

3

Beep starts at the bottom left. The star is 3 right and 1 up. Beep's code: REPEAT 3 times: Move right 1, Move up 1. Where does Beep end? What is the bug?

4

Beep and Boop are in the same row, 5 squares apart, facing each other. At the same time, Beep runs REPEAT 3 times: Move right 1 and Boop runs REPEAT 3 times: Move left 1. Will they bump? Explain.

5

Code: START with number = 50, REPEAT 5 times: SUBTRACT 10, SAY number. Predict the outcome. Then change one number so the outcome is 30.

Challenge

1

Code: START with number = 1, REPEAT 3 times: DOUBLE number, SAY number. Predict. Then predict what happens if you change 3 to 4.

2

Mia's code is meant to count by 5s to 50 and say each number: START with number = 5, REPEAT 10 times: SAY number, ADD 5. What does it say? Is there a bug?

3

A pen robot runs REPEAT 4 times: Move forward 2, Turn right (quarter turn). Change the code to draw a square with sides twice as long. What else changes?

Check your understanding

Quiz

1.

Code: REPEAT 5 times: Move up 1. You change 5 to 2. What happens?

2.

Predict: START with number = 0, REPEAT 4 times: ADD 20, SAY number.

3.

A line that is inside a loop runs…

4.

Code to draw a square: REPEAT 3 times: Move forward 2, Turn right (quarter turn). What is the bug?

5.

Code: START with number = 0, REPEAT 3 times: ADD 10. Which change makes the outcome 60?

6.

What should you do before running code?

For parents and teachers

Big idea: Reading and changing code (C3.2) builds careful thinking. In Grade 3, the code includes loops, so children describe how changing the number of repeats, a step inside the loop, the order of steps, or where a line sits (inside or after the loop) changes the outcome.

Common difficulties:

  • Not predicting first. Children like to jump in. Ask them to write a prediction before acting it out.
  • Missing where a line is. Point out the indent: pushed-in lines belong to the loop.
  • Frustration with bugs. Normalize mistakes: professional programmers spend lots of time debugging. Praise persistence and clear explanations (this is part of the social-emotional learning in strand A).

How to help: Play "Bug detective" with loops in walking directions, recipes ("stir 3 times") or dance moves. Ask, "What would happen if we changed this number?"