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

Reading, Changing and Debugging Code

C3.2 A1.3About 40 minutes
Learning goals
  • I can read code and predict the outcome before I run it.
  • I can change code to get a new outcome, and describe what my change did.
  • I can find and fix a bug in code, including code for two robots at the same time.
  • I can keep trying when my code does not work the first time.

Words to know

predict
To say what you think will happen before you run the code.
alter
To change something.
bug
A mistake in code.
debug
To find and fix a mistake in code.
outcome
What happens at the end when the code is run.

Be a code detective

Real coders spend lots of time reading code that someone else wrote. They predict what it will do, change it to do something new, and fix it when it has a bug.

Today you will do all three!

Read, predict, run, change

  1. Read the code, one step at a time.
  2. Predict the outcome. Say it out loud.
  3. Run it (with your finger, toys or in your head) to check.
  4. Change one thing and describe how the outcome changed.
A1234561234columnrow
Beep (A) is at column 1, row 1. The star is at column 5, row 4.
WHEN GO is pressed
➡ Move right 4
⬆ Move up 2
STOP

Beep's code. Does Beep reach the star?

Worked example

Read and predict

Read Beep's code. Predict where Beep stops. Does Beep reach the star?

  1. Start at column 1, row 1.
  2. Move right 4: column 1 → 5. Beep is at column 5, row 1.
  3. Move up 2: row 1 → 3. Beep is at column 5, row 3.
  4. The star is at column 5, row 4. Beep stops one square too low.

Beep stops at column 5, row 3, just below the star. The code has a bug.

Worked example

Fix the bug

Change one step so Beep reaches the star.

  1. Beep needs to go up from row 1 to row 4. That is 3 squares, not 2.
  2. Change ⬆ Move up 2 to ⬆ Move up 3.
  3. Run it again: right 4 → column 5, up 3 → row 4. ⭐

Change Move up 2 to Move up 3. The change made Beep go one square higher, onto the star.

Changing code to change the outcome

When you change one part of code, describe what changed in the outcome. Look at this math code:

START with 10
ADD 5
ADD 5
ADD 5
SAY the number

Outcome: 10 → 15 → 20 → 25. It says 25.

ChangeNew outcomeWhat the change did
START with 20 instead of 1035Every number is 10 more, so the outcome is 10 more.
ADD 10 instead of ADD 5 (all three)40Each jump is bigger: 10, 20, 30, 40.
Remove one ADD 520One less jump of 5, so the outcome is 5 less.
Change the last ADD 5 to SUBTRACT 51510 → 15 → 20 → 15.
Each change gives a different outcome. Describe it with words like more, less and bigger jumps.

Bugs in concurrent code

When two robots run at the same time, a bug can make them crash: both try to be on the same square at the same time.

AB1234561234columnrow
Beep (A) is at column 1, row 2. Boop (B) is at column 4, row 1.
BeatBeep (A)Boop (B)
1➡ Move right 1⬆ Move up 1
2➡ Move right 1⬅ Move left 1
3➡ Move right 1⬆ Move up 1
Concurrent code for Beep and Boop.
Worked example

Find the crash

Run the code beat by beat. Do the robots crash? If they do, fix it.

  1. Beat 1: Beep moves to column 2, row 2. Boop moves up to column 4, row 2.
  2. Beat 2: Beep moves to column 3, row 2. Boop moves left to column 3, row 2.
  3. Both are on column 3, row 2 at the same time. Crash! 💥
  4. Fix: change Boop's beat 2 so it does not move left. For example, Boop's code could be up 1, up 1, left 1.
  5. Run it again: Beat 1: Beep (2,2), Boop (4,2). Beat 2: Beep (3,2), Boop (4,3). Beat 3: Beep (4,2), Boop (3,3). No crash!

They crash on beat 2 at column 3, row 2. Changing the order of Boop's steps to up, up, left fixes it.

Do not give up!

Bugs are normal. Every coder makes them. When your code does not work:

  • Take a breath. Feeling frustrated is okay.
  • Run the code slowly, one step at a time, and stop where it goes wrong.
  • Change one thing, then test again.
  • Ask a friend or grown-up to run it with you.
Just for fun

Funny but true: in 1947, people working on an early computer found a real moth stuck inside it! They taped the moth into their notebook. Coders still call mistakes bugs. 🦋

Your turn

1

Predict the outcome: START with 40 → SUBTRACT 10 → SUBTRACT 10 → SAY the number

2

Change the code above so the outcome is 10. What did you change?

3

Beep is at column 1, row 1. The star is at column 3, row 1. The code says ➡ Move right 3. What is the bug?

4

Beep's code is ⬆ Move up 2, ➡ Move right 1. You change it to ⬆ Move up 2, ⬅ Move left 1. Describe what changes.

5

Morning code: 1. Put on boots. 2. Put on snow pants. 3. Go outside. What is the bug?

6

Predict: START with 0 → ADD 25 → ADD 25 → ADD 25 → SAY the number

Hands-on activity

Bug hunt

You need: a floor grid (tape or tiles), a toy to be Beep, a second toy to be Boop, and paper.

  1. A grown-up writes code to get Beep to a target (a sock or a stuffed animal), but secretly puts one bug in it.
  2. You read the code and predict where Beep will stop.
  3. Run it with the toy. Find the bug and fix it.
  4. Now try two robots: write concurrent code for Beep and Boop. Run it beat by beat. If they crash, change one step and try again.
  5. Switch: you write buggy code for the grown-up to fix!

What to look for: your child changes one step at a time and explains what the change did.

More practice: warm-up

1

What is a bug?

2

What does debug mean?

3

Predict: START with 5 → ADD 5 → SAY the number

4

Change ADD 5 to ADD 10 in the code above. What is the new outcome?

5

Beep is at column 2, row 1. Code: ⬆ Move up 3. Where does Beep stop?

More practice: on your own

1

Code: START with 60 → SUBTRACT 20 → ADD 5 → SAY the number. Predict the outcome. Then change one number so the outcome is 50.

2

This code should count by 10s to 50, but it says 40: START with 10 → ADD 10 → ADD 10 → ADD 10 → SAY. Find the bug.

3

Beep (start column 1, row 1) and Boop (start column 3, row 1) run at the same time. Beat 1: Beep right 1, Boop left 1. What happens?

4

Describe how the outcome changes: START with 30 → ADD 5 becomes START with 30 → ADD 5 → ADD 5 → ADD 5.

5

Snack code: 1. Eat the apple. 2. Wash the apple. 3. Take an apple from the bowl. Fix the order.

Challenge

1

Code: START with 100 → SUBTRACT 25 → SUBTRACT 25 → SAY the number. Change only the START number so the outcome is 25.

2

Beep is at column 1, row 1. The star is at column 4, row 3. Beep's code: ⬆ Move up 3, ➡ Move right 3. Where does Beep end? Fix it with the smallest change.

3

Two snowplows clear a road at the same time. Plow A starts at 0 m and clears 10 m each minute. Plow B starts at 0 m and clears 5 m each minute. The code should make both plows stop at 30 m. Plow A's code runs 3 minutes. How many minutes should Plow B's code run? Explain.

Check your understanding

Quiz

1.

What does debug mean?

2.

Predict: START with 20 → ADD 10 → ADD 10 → SAY

3.

Code: START with 20 → ADD 10. If you change ADD 10 to ADD 20, the outcome is...

4.

Beep should stop at column 4, row 1. Beep starts at column 1, row 1. The code says Move right 2. What is the bug?

5.

Two robots run code at the same time and land on the same square. What should you do?

6.

What is a good first step when your code does not work?

For parents and teachers

Children read code, predict the outcome, change it and describe how the change affected the outcome. They also debug code for two robots running at the same time. This lesson also practises perseverance (Strand A, A1.3): staying calm and trying again.

Common mistakes

  • Rewriting all of the code instead of finding the one wrong step.
  • Describing a change only as it's different. Ask: How much more or less? Which way did Beep go?
  • Running two robots' code one after the other instead of beat by beat.

How to help

  • Hide a bug in code for your child to find. Make some bugs about direction, some about number of squares, and some about order.
  • Model calm debugging: Hmm, that didn't work. Let's go one step at a time.
  • Praise effort and strategies, not just the right answer.