C. Algebra · Patterns, Equations, Coding and ModellingReading, Changing and Debugging Code with LoopsC3.2 A1About 40 minutesLearning goalsI 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 knowpredictSay what you think will happen before it happens.alterTo change something.outcomeWhat happens at the end when the code is run.bugA mistake in code that makes it do the wrong thing.debugTo find and fix a bug.Remember from Grade 2In 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, checkRead the code line by line.Predict the outcome. Write it down.Run the code (act it out or trace it on paper).Check: was your prediction right? If the outcome is wrong, look for the bug.Change 1: The number of repeatsThis code makes Beep climb the staircase from the last lesson.WHEN GO is pressedREPEAT 3 times ➡ Move right 1 ⬆ Move up 1STOPOutcome: Beep ends 3 right and 3 up.Worked exampleExample 1: Alter the loopChange line 2 to REPEAT 5 times. How does the outcome change?The steps inside the loop stay the same: right 1, up 1.Now they run 5 times instead of 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 loopSTART with number = 0REPEAT 5 times ADD 25 to numberSAY numberSTOPOutcome: it says 125.Worked exampleExample 2: Alter a stepChange line 3 to ADD 50 to number. What is the new outcome? Describe the change.The loop still runs 5 times.Now it adds 50 each time: 50, 100, 150, 200, 250.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 loopSTART with number = 0START with number = 0REPEAT 4 timesREPEAT 4 times ADD 10 ADD 10 SAY numberSAY numberSays: 10, 20, 30, 40Says: 40The same lines, but in Code A, SAY is inside the loop. In Code B, SAY is after the loop.Change 4: The order of stepsThere is a wall (the dark square) next to Beep. Robots cannot go through walls.ABeep (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 pressedREPEAT 2 times ➡ Move right 1 ⬆ Move up 1STOPBeep's code. Is there a bug?Worked exampleExample 3: Debug the orderRun the code. What goes wrong? Fix it.Line 3 says Move right 1. But the square to the right of Beep is the wall! Bump! That is the bug.Idea: switch the order of the two steps inside the loop. Go up first, then right.New code: REPEAT 2 times: Move up 1, Move right 1.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.AThe fixed path: up, right, up, right. Beep goes around the wall to the star.Worked exampleExample 4: Debug a shapeAva 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.Run it: forward 4, turn; forward 4, turn; forward 4, turn. Then the loop stops.Only 3 sides are drawn. The square is open on one side.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 funFunny 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 questionsDoes 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 turn1Predict: START with number = 0, REPEAT 6 times: ADD 5, SAY number.Show answer30. 5, 10, 15, 20, 25, 30.2Change that code to REPEAT 3 times. What is the new outcome? Describe the change.Show answer15. The loop runs half as many times, so the outcome is half as big.3Beep's code is REPEAT 4 times: Move up 1. The star is 6 squares up. Fix the bug.Show answerChange it to REPEAT 6 times: Move up 1.4Code A: REPEAT 3 times: ADD 2, SAY number (SAY inside). Starting at 0, what does it say?Show answer2, 4, 6. SAY is inside the loop, so it runs each time.5Code B: REPEAT 3 times: ADD 2, then SAY number (SAY after the loop). Starting at 0, what does it say?Show answer6 only. SAY runs once, after the loop.6A pen robot runs REPEAT 2 times: Move forward 3, Turn right (quarter turn). What does it draw?Show answerTwo sides of a square that meet at a square corner. It needs 4 repeats to make a whole square.Hands-on activityBug detectiveYou need: paper, a pencil, and a grown-up.The grown-up hides a small toy somewhere in a room.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.You read the code and predict where it will take you.Act it out, step by step. Did you reach the toy?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."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-up1What is a bug in code?Show answerA mistake that makes the code do the wrong thing.2What does debug mean?Show answerTo find and fix a bug.3REPEAT 4 times: Move right 1. If you change 4 to 6, how many more squares does the robot move?Show answer2 more squares (6 in all).4Predict: START with number = 10, REPEAT 2 times: ADD 10, SAY number.Show answer30. 10 → 20 → 30.5True or false: The lines inside a loop run only once.Show answerFalse. They run every time the loop repeats.More practice: on your own1Code: START with number = 0, REPEAT 4 times: ADD 3, SAY number. Change it so the outcome is 24. Describe two different ways.Show answerWay 1: change ADD 3 to ADD 6 (4 × 6 = 24). Way 2: change REPEAT 4 to REPEAT 8 (8 × 3 = 24).2Leo 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.Show answerMove SAY number inside the loop, so it runs each time: 100, 200, 300.3Beep 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?Show answerBeep ends 3 right and 3 up, 2 squares above the star. The bug: Move up 1 should not be inside the loop. Fix: REPEAT 3 times: Move right 1, then Move up 1 once after the loop.4Beep 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.Show answerYes. Beep starts at 0 and Boop at 5. After beat 2: Beep at 2, Boop at 3. On beat 3, Beep moves to 3 while Boop moves to 2, so they try to pass through each other. Bump! Fix: change both loops to 2 times. Then Beep stops at 2 and Boop stops at 3, side by side.5Code: START with number = 50, REPEAT 5 times: SUBTRACT 10, SAY number. Predict the outcome. Then change one number so the outcome is 30.Show answerOutcome: 0 (50, 40, 30, 20, 10, 0). To get 30, change REPEAT 5 to REPEAT 2: 50 → 40 → 30.Challenge1Code: START with number = 1, REPEAT 3 times: DOUBLE number, SAY number. Predict. Then predict what happens if you change 3 to 4.Show answerWith 3: 1 → 2 → 4 → 8. With 4: one more double, 16. Each extra repeat doubles the outcome.2Mia'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?Show answerIt says 5, 10, 15, 20, 25, 30, 35, 40, 45, 50. It says the number before adding, so it says 10 numbers: 5 to 50. No bug! (At the very end, number has become 55, but it is never said.)3A 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?Show answerChange Move forward 2 to Move forward 4. The loop still repeats 4 times. Each side is twice as long, so the distance around the square (the perimeter) is also twice as long: 16 steps instead of 8.Check your understandingQuiz1. Code: REPEAT 5 times: Move up 1. You change 5 to 2. What happens?Nothing changesThe robot moves downThe robot moves up 3 fewer squares2. Predict: START with number = 0, REPEAT 4 times: ADD 20, SAY number.24100803. A line that is inside a loop runs…every time the loop repeatsoncenever4. Code to draw a square: REPEAT 3 times: Move forward 2, Turn right (quarter turn). What is the bug?It should turn leftIt should be REPEAT 4 timesMove forward should be 35. Code: START with number = 0, REPEAT 3 times: ADD 10. Which change makes the outcome 60?Change REPEAT 3 to REPEAT 4Change ADD 10 to ADD 20Change ADD 10 to ADD 306. What should you do before running code?Delete the loopPredict the outcomeAdd more linesMark lesson completeFor parents and teachersBig 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?"PreviousCoding with Loops: Repeating EventsNextMath Models: Planning with Math in Real Life