C. Algebra · Patterns, Variables, Coding and ModellingReading, 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
- Read the code, one step at a time.
- Predict the outcome. Say it out loud.
- Run it (with your finger, toys or in your head) to check.
- Change one thing and describe how the outcome changed.
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 exampleRead and predict
Read Beep's code. Predict where Beep stops. Does Beep reach the star?
- Start at column 1, row 1.
Move right 4: column 1 → 5. Beep is at column 5, row 1.Move up 2: row 1 → 3. Beep is at column 5, row 3.- 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 exampleFix the bug
Change one step so Beep reaches the star.
- Beep needs to go up from row 1 to row 4. That is 3 squares, not 2.
- Change
⬆ Move up 2 to ⬆ Move up 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.
| Change | New outcome | What the change did |
|---|
| START with 20 instead of 10 | 35 | Every number is 10 more, so the outcome is 10 more. |
| ADD 10 instead of ADD 5 (all three) | 40 | Each jump is bigger: 10, 20, 30, 40. |
| Remove one ADD 5 | 20 | One less jump of 5, so the outcome is 5 less. |
| Change the last ADD 5 to SUBTRACT 5 | 15 | 10 → 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.
Beep (A) is at column 1, row 2. Boop (B) is at column 4, row 1.| Beat | Beep (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 exampleFind the crash
Run the code beat by beat. Do the robots crash? If they do, fix it.
- Beat 1: Beep moves to column 2, row 2. Boop moves up to column 4, row 2.
- Beat 2: Beep moves to column 3, row 2. Boop moves left to column 3, row 2.
- Both are on column 3, row 2 at the same time. Crash! 💥
- 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.
- 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 funFunny 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
1Predict the outcome: START with 40 → SUBTRACT 10 → SUBTRACT 10 → SAY the number
2Change the code above so the outcome is 10. What did you change?
Add one more SUBTRACT 10: 40 → 30 → 20 → 10. (Or start with 30.)
3Beep is at column 1, row 1. The star is at column 3, row 1. The code says ➡ Move right 3. What is the bug?
Beep moves one square too far (to column 4). Change it to ➡ Move right 2.
4Beep's code is ⬆ Move up 2, ➡ Move right 1. You change it to ⬆ Move up 2, ⬅ Move left 1. Describe what changes.
Beep still goes up 2, but then goes left instead of right, so it ends 2 squares to the left of where it ended before.
5Morning code: 1. Put on boots. 2. Put on snow pants. 3. Go outside. What is the bug?
The order is wrong. Snow pants should go on before boots. Swap steps 1 and 2.
6Predict: START with 0 → ADD 25 → ADD 25 → ADD 25 → SAY the number
75. 25, 50, 75 (like counting quarters).
Hands-on activityBug hunt
You need: a floor grid (tape or tiles), a toy to be Beep, a second toy to be Boop, and paper.
- A grown-up writes code to get Beep to a target (a sock or a stuffed animal), but secretly puts one bug in it.
- You read the code and predict where Beep will stop.
- Run it with the toy. Find the bug and fix it.
- 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.
- 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
To find and fix a mistake in code.
3Predict: START with 5 → ADD 5 → SAY the number
4Change ADD 5 to ADD 10 in the code above. What is the new outcome?
5Beep is at column 2, row 1. Code: ⬆ Move up 3. Where does Beep stop?
More practice: on your own
1Code: START with 60 → SUBTRACT 20 → ADD 5 → SAY the number. Predict the outcome. Then change one number so the outcome is 50.
Outcome: 60 − 20 = 40, 40 + 5 = 45. One fix: change ADD 5 to ADD 10: 40 + 10 = 50. (Or change SUBTRACT 20 to SUBTRACT 15.)
2This code should count by 10s to 50, but it says 40: START with 10 → ADD 10 → ADD 10 → ADD 10 → SAY. Find the bug.
It needs one more ADD 10. 10 → 20 → 30 → 40, then one more makes 50.
3Beep (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?
They crash! Beep moves to column 2 and Boop moves to column 2, both on row 1. One fix: change Boop's step to up 1.
4Describe how the outcome changes: START with 30 → ADD 5 becomes START with 30 → ADD 5 → ADD 5 → ADD 5.
The outcome goes from 35 to 45. Two more jumps of 5 make it 10 more.
5Snack code: 1. Eat the apple. 2. Wash the apple. 3. Take an apple from the bowl. Fix the order.
3, 2, 1: Take an apple from the bowl, wash the apple, eat the apple. You must have the apple before you can wash it, and wash it before you eat it.
Challenge
1Code: START with 100 → SUBTRACT 25 → SUBTRACT 25 → SAY the number. Change only the START number so the outcome is 25.
START with 75. 75 − 25 = 50, 50 − 25 = 25. The outcome went down by 25 because the start went down by 25.
2Beep 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.
Beep ends at column 4, row 4, one square above the star. Change ⬆ Move up 3 to ⬆ Move up 2: up to row 3, right to column 4. ⭐
3Two 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.
6 minutes. Plow B: 5, 10, 15, 20, 25, 30. Plow A only needs 3 minutes (10, 20, 30) because it clears twice as much each minute.
Check your understandingQuiz
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 teachersChildren 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.