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

Reading and Changing Code with Conditionals and Loops

C3.2 A1About 40 minutes
Learning goals
  • I can read code with loops and conditional statements and predict the outcome.
  • I can change a number, a condition or the place of a line, and describe how the outcome changes.
  • I can find and fix a bug in code.
  • I can explain my changes using math.

Words to know

outcome
What happens when the code runs.
alter
Change. Altering code can change the outcome.
bug
A mistake in code that makes it do something you didn't want.
debug
Find and fix a bug.
trace
Run code step by step on paper, keeping track of each variable.

Remember

In the last lesson you wrote code with IF ... THEN ... ELSE, loops and REPEAT UNTIL. Today you will be a code detective: read code someone else wrote, predict what it does, change it, and explain how each change affects the outcome.

The pattern-maker code

This code draws a pattern. The indents show what belongs to the loop and what belongs to the IF and ELSE.

SET n = 1
REPEAT 6 times
IF n is even THEN
DRAW ❄️
ELSE
DRAW 🍁
ADD 1 to n

The pattern-maker code. ADD 1 to n is inside the loop, but not inside the IF or ELSE.

nodd/even?draws1odd🍁2even❄️3odd🍁4even❄️5odd🍁6even❄️
Tracing the pattern-maker code: each time through the loop, n goes up by 1. Odd n draws 🍁 and even n draws ❄️, so the outcome is πŸβ„οΈπŸβ„οΈπŸβ„οΈ.

Trace it: n = 1 is odd β†’ 🍁. n = 2 is even β†’ ❄️. n = 3 β†’ 🍁. n = 4 β†’ ❄️. n = 5 β†’ 🍁. n = 6 β†’ ❄️.

Outcome: πŸβ„οΈπŸβ„οΈπŸβ„οΈ, a repeating pattern with the core πŸβ„οΈ.

Three kinds of changes

  1. Change a number, like how many times the loop repeats.
  2. Change a condition, like n is even to n is a multiple of 3, or > to <.
  3. Move a line into or out of a loop, an IF or an ELSE, by changing its indent.
Worked example

Example 1: Change a number

Change REPEAT 6 times to REPEAT 9 times. What is the new outcome?

  1. The loop now runs for n = 1 to 9.
  2. Odd numbers (1, 3, 5, 7, 9) draw 🍁. Even numbers (2, 4, 6, 8) draw ❄️.

πŸβ„οΈπŸβ„οΈπŸβ„οΈπŸβ„οΈπŸ. The pattern is longer: 9 elements, with 5 🍁 and 4 ❄️. The core stays the same.

Worked example

Example 2: Change the condition

Change IF n is even to IF n is a multiple of 3. Keep REPEAT 6 times. What is the new outcome?

  1. n = 1: not a multiple of 3 β†’ 🍁. n = 2 β†’ 🍁. n = 3 β†’ ❄️.
  2. n = 4 β†’ 🍁. n = 5 β†’ 🍁. n = 6 β†’ ❄️.

πŸπŸβ„οΈπŸπŸβ„οΈ. Changing the condition changed the core: now it is πŸπŸβ„οΈ, with 3 elements.

Worked example

Example 3: Move a line (and make a bug!)

Move ADD 1 to n so it is not indented: it is now after the loop. What happens?

  1. Now n is never changed inside the loop. It stays 1 every time.
  2. 1 is odd, so every time the loop runs, it draws 🍁.
  3. After the loop ends, n becomes 2, but nothing is drawn after that.

🍁🍁🍁🍁🍁🍁. Moving one line changed everything. This is a bug if we wanted the πŸβ„οΈ pattern.

Changing a math code

This code adds up points in a game. Let's predict the outcome, then change it.

SET points = 0
SET n = 1
REPEAT 10 times
IF n > 6 THEN
ADD 5 to points
ELSE
ADD 2 to points
ADD 1 to n
SAY points

The points code.

Worked example

Example 4: Predict, then change the condition

What does the points code SAY? Then change n > 6 to n > 8. What does it SAY now?

  1. n goes from 1 to 10. For n = 1 to 6, the condition n > 6 is false, so it adds 2 each time: 6 Γ— 2 = 12.
  2. For n = 7 to 10 (4 numbers), the condition is true, so it adds 5 each time: 4 Γ— 5 = 20.
  3. Total: 12 + 20 = 32.
  4. With n > 8: n = 1 to 8 add 2 (8 Γ— 2 = 16), and n = 9 and 10 add 5 (2 Γ— 5 = 10). Total: 26.

The original says 32. After the change it says 26. Making the condition harder to reach means fewer turns get 5 points, so the total goes down by 6: two turns now get 2 points instead of 5, and 2 Γ— 3 = 6.

01234512345678910npointsn > 6 is false: +2 each6 Γ— 2 = 12n > 6 is true: +5 each4 Γ— 5 = 20Total: 12 + 20 = 32 points
Points added on each turn of the loop. For n = 1 to 6 the condition n > 6 is false (+2 each). For n = 7 to 10 it is true (+5 each). Altogether the code says 32.
Worked example

Example 5: Debug the code

This code should say "pass" for a score of 50 or more out of 100, and "try again" otherwise. Test it with a score of 50. Find and fix the bug.

IF score > 50 THEN SAY "pass" ELSE SAY "try again"

  1. Test score = 50: Is 50 > 50? False. It says "try again". But 50 should pass! That's a bug.
  2. Fix: the condition must be true for 50. For whole-number scores, score > 49 works.
  3. Re-test: score = 50: 50 > 49 βœ” "pass". score = 49: 49 > 49 ✘ "try again". βœ”

Fixed code: IF score > 49 THEN SAY "pass" ELSE SAY "try again". Always test the boundary number!

Code detective steps

  1. Read each line. Use the indents to see what's inside the loop, the IF and the ELSE.
  2. Predict the outcome. Make a tracking table for each variable.
  3. Change one thing at a time.
  4. Predict again, then trace to check.
  5. Describe the effect: "Changing 6 to 9 made the pattern 3 elements longer."

Test the boundary values (like 50 when the condition is about 50). Bugs love to hide there!

Just for fun

Funny but true: in 1947, a team working on an early computer found a real moth stuck inside it, causing a problem. They taped the moth into their logbook as the "first actual case of bug being found." Your bugs are much easier to fix: just move a line!

Hands-on activity

Change the code, change the pattern

You need: paper, a pencil, and some small objects in two kinds (for example 10 forks and 10 spoons, or 10 red and 10 blue blocks, or 10 coins and 10 buttons).

  1. Your grown-up is the computer. You read the pattern-maker code out loud. Instead of drawing, they lay out objects: a spoon for 🍁 and a fork for ❄️.
  2. Before they start, predict the outcome. Were you right?
  3. Change one thing in the code (the REPEAT number, the condition or where ADD 1 to n goes). Write your new code. Predict the new outcome, then have your grown-up run it.
  4. Describe the effect in a sentence, like "The core changed from 2 elements to 3."
  5. Swap: your grown-up makes a change and you run it.

Ideas for conditions to try: n is a multiple of 4, n > 3, n < 5.

What to look for: your child predicts before running and explains how the change caused the new outcome.

Grown-up note: use forks and spoons only (no knives), and keep small objects away from babies and toddlers.

Your turn

1

In the pattern-maker code, change REPEAT 6 times to REPEAT 4 times. What is the outcome?

2

In the pattern-maker code, swap ❄️ and 🍁 (DRAW 🍁 under THEN and DRAW ❄️ under ELSE). What is the outcome?

3

In the pattern-maker code, change the condition to IF n > 4. What is the outcome?

4

In the points code, change ADD 5 to points to ADD 10 to points. What does it SAY?

5

In the points code, change REPEAT 10 times to REPEAT 12 times. What does it SAY?

6

This code should say every number from 1 to 5. Find the bug.

SET n = 1

REPEAT 5 times

Β Β Β Β SAY n

ADD 1 to n

More practice: warm-up

1

What is a bug?

2

Pattern-maker code with REPEAT 2 times. What is the outcome?

3

In code, how can you tell which lines are inside a loop?

4

If you change IF n > 5 to IF n > 2, will the THEN part run more or fewer times (for n = 1 to 10)?

5

Why should you change only one thing at a time when testing code?

More practice: on your own

1

In the points code, change n > 6 to n < 6. What does it SAY?

2

In the pattern-maker code, change the condition to n is a multiple of 4 and REPEAT to 8 times. Write the outcome and the core.

3

This code should say "cold" when the temperature is 10 degrees or less. Test it at 10 and fix the bug.

IF temp < 10 THEN SAY "cold" ELSE SAY "warm"

4

What does this SAY? Then change ADD 4 to ADD 6 and say what happens.

SET total = 0

REPEAT UNTIL total > 25

Β Β Β Β ADD 4 to total

SAY total

5

Change the pattern-maker code so the outcome is πŸπŸβ„οΈπŸπŸβ„οΈπŸπŸβ„οΈ. What two things must you change?

Challenge

1

What is the outcome?

REPEAT 3 times

Β Β Β Β SET n = 1

Β Β Β Β REPEAT 4 times

Β Β Β Β Β Β Β Β IF n is odd THEN DRAW ⬛ ELSE DRAW ⬜

Β Β Β Β Β Β Β Β ADD 1 to n

Β Β Β Β GO to the next row

2

In the challenge code above, move SET n = 1 to the very top, before REPEAT 3 times (not indented). What changes?

3

Change the points code so that it SAYs exactly 40. Change only the number in IF n > 6. What number works?

Check your understanding

Quiz

1.

Pattern-maker code with REPEAT 5 times. What is the outcome?

2.

In the pattern-maker code, what happens if ADD 1 to n is moved outside the loop?

3.

The points code says 32. If n > 6 changes to n > 8, what does it say?

4.

Code should say "pass" for 50 or more, but it uses IF score > 50. Which score shows the bug?

5.

What is the best first step when reading new code?

6.

Changing IF n is even to IF n is a multiple of 3 (n = 1 to 6) changes the core from πŸβ„οΈ to:

For parents and teachers

Big idea: Grade 5 students read and alter existing code, including code with conditional statements and other control structures, and describe how the changes affect the outcomes (C3.2). The focus is on predicting, testing one change at a time, and explaining the effect, which is mathematical reasoning.

Common difficulties:

  • Ignoring indents. Have your child draw a bracket beside the lines inside each loop and each IF.
  • Changing several things at once and not knowing which change mattered.
  • Missing boundary bugs like > 50 when 50 should count. Always test the exact boundary number.

How to help: Act as the "computer" and follow your child's code literally. When something goes wrong, ask "Which line made that happen?" Social-emotional learning (A1): bugs are normal. Professional programmers spend lots of time debugging. Praise your child for finding a bug, not only for code that works the first time.