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

Reading and Altering Code: Outcomes and Efficiency

C3.2 A1About 40 minutes
Learning goals
  • I can read code with loops and conditional statements and predict its outcome.
  • I can alter code by changing a number, a condition or the order of lines.
  • I can describe how a change affects the outcome of the code.
  • I can describe how a change affects the efficiency of the code, and find and fix bugs.

Words to know

outcome
What the code produces when it runs: what it says, draws or moves.
alter
Change part of the code.
trace
Run code by hand, one line at a time, keeping track of every variable.
bug
A mistake in code that makes it do something you didn't want.
debug
Find and fix a bug.
infinite loop
A loop that never stops, because its stopping condition never becomes true.

Remember from Grade 5 and the last lesson

In Grade 5 you read and changed code with conditionals and loops and described how the outcome changed. In the last lesson you wrote efficient code and used ELSE IF.

Today you'll be a code detective again. This time, for each change, ask two questions:

  1. How does it change the outcome?
  2. How does it change the efficiency (the number of lines or steps)?

The winter pattern code

Read this code carefully. The indents show what is inside the loop and what is inside each part of the IF.

SET n = 1
REPEAT 12 times
IF n is a multiple of 4 THEN
DRAW ⛄
ELSE IF n is even THEN
DRAW ❄️
ELSE
DRAW 🌲
ADD 1 to n

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

Trace it: the code checks the conditions in order.

  • n = 1: not a multiple of 4, not even → 🌲
  • n = 2: not a multiple of 4, even → ❄️
  • n = 3 → 🌲
  • n = 4: a multiple of 4 → ⛄ (the computer stops checking here)
  • n = 5 to 8 → 🌲 ❄️ 🌲 ⛄, and n = 9 to 12 → 🌲 ❄️ 🌲 ⛄

Outcome: 🌲❄️🌲⛄🌲❄️🌲⛄🌲❄️🌲⛄. It's a repeating pattern with the core 🌲❄️🌲⛄.

n a multiple of 4?yessnowmannon even?yessnowflakenotreechecked firstchecked second123456789101112Outcome for n = 1 to 12:
The winter pattern code as a flowchart. Because the multiple-of-4 check comes first, 4, 8 and 12 become snowmen before the even check is ever asked.
Kind of changeExampleWhat to ask
Change a numberREPEAT 12 times → REPEAT 6 timesHow long is the outcome now?
Change a conditionmultiple of 4 → multiple of 3, or > → ≥Which values take a different path?
Change the orderswap the IF and the ELSE IF, or move SAY after ADDDoes a line now run too early, too late, or never?
Change the methodcheck every number → count by the jumpIs the outcome the same? Are there fewer steps?
Four kinds of changes a code detective looks at.
Worked example

Example 1: Change a number

In the winter pattern code, change REPEAT 12 times to REPEAT 6 times. What is the new outcome?

  1. The loop now runs for n = 1 to 6 only.
  2. n = 1 to 4 gives 🌲❄️🌲⛄. n = 5 gives 🌲 and n = 6 gives ❄️.

🌲❄️🌲⛄🌲❄️. The pattern is shorter. The core is the same. The code runs half as many steps.

Worked example

Example 2: Swap the conditions (and make a bug)

Swap the two conditions: IF n is even THEN DRAW ❄️, then ELSE IF n is a multiple of 4 THEN DRAW ⛄. What happens?

  1. n = 4: Is 4 even? Yes, so it draws ❄️ and stops checking. The ⛄ check never happens.
  2. Every multiple of 4 is also even, so ⛄ can never be drawn.
  3. Outcome: 🌲❄️🌲❄️🌲❄️🌲❄️🌲❄️🌲❄️.

The ⛄ disappears. With ELSE IF, order matters: put the more specific condition (multiple of 4) first.

Worked example

Example 3: The same outcome, fewer steps

Both codes say the multiples of 6 up to 60. Compare their efficiency.

Version A: SET n = 1

REPEAT 60 times

IF n is a multiple of 6 THEN SAY n

ADD 1 to n

Version B: SET n = 6

REPEAT 10 times

SAY n

ADD 6 to n

  1. Both say 6, 12, 18, 24, 30, 36, 42, 48, 54, 60.
  2. Version A's loop runs 60 times, and it checks a condition every time. 50 of those checks find nothing to say.
  3. Version B's loop runs only 10 times, with no checks at all, because it counts by 6.

Version B is more efficient: the same outcome with 50 fewer trips through the loop.

0102030405060Version A60 tripsVersion B10 tripstrips through the loop (same outcome: 6, 12, ..., 60)
Both versions say the same ten numbers, but Version B makes 50 fewer trips through the loop because it counts by 6.
Worked example

Example 4: Move a line

In Version B, swap the last two lines so the loop does ADD 6 to n first and SAY n second. What is the outcome now?

  1. n starts at 6. First time: ADD 6 makes n = 12, then SAY 12.
  2. Next: 18, then 24, ... The tenth time: n = 66, SAY 66.

12, 18, 24, ..., 66. The 6 is skipped and 66 is added. The order of lines changes the outcome. To fix it, swap them back, or start with SET n = 0.

Worked example

Example 5: Change ≥ to >

Run this savings code. Then change savings ≥ 48 to savings > 48. How does the outcome change?

SET savings = 0

SET weeks = 0

REPEAT UNTIL savings ≥ 48

ADD 8 to savings

ADD 1 to weeks

SAY weeks

  1. With ≥ 48: savings go 8, 16, 24, 32, 40, 48. 48 ≥ 48 is true, so the loop stops. weeks = 6.
  2. With > 48: 48 > 48 is false, so the loop runs once more: savings = 56, weeks = 7.

≥ 48 says 6. > 48 says 7. One small symbol changes the answer. Read conditions very carefully at the boundary.

Worked example

Example 6: Debug an infinite loop

This code was meant to stop when n reaches 20. What goes wrong? Fix it.

SET n = 1

REPEAT UNTIL n = 20

ADD 3 to n

SAY n

  1. Trace: n = 1, 4, 7, 10, 13, 16, 19, 22, 25, ...
  2. n jumps from 19 to 22. It is never exactly 20, so the loop never stops: an infinite loop.
  3. Fix: change the condition to REPEAT UNTIL n ≥ 20. Now it stops at 22.

Bug: n skips over 20. Fix: use n ≥ 20. The fixed code says 22.

Code detective steps

  1. Read the whole code first. Use the indents to see what is inside what.
  2. Predict the outcome before you trace.
  3. Trace with a tracking table.
  4. Make one change at a time, then trace again and compare.
  5. Describe the change: what happened to the outcome, and what happened to the number of steps?
  6. When you find a bug, say what it does wrong and how your fix solves it. Bugs aren't failures; they're clues.
Just for fun

Joke: Why did the coder spend all day in the shower?

The shampoo bottle said: Lather. Rinse. Repeat. There was no REPEAT UNTIL!

Hands-on activity

Change the code, change the pattern

You need: three kinds of small objects (for example, 12 buttons, 12 coins and 12 dried pasta pieces), or paper and three coloured pencils, and the winter pattern code.

  1. Let 🌲 be one kind of object, ❄️ another, and ⛄ the third. Be the computer: run the winter pattern code and lay out the objects in a row.
  2. Predict, then run each change. Lay out a new row each time:
  • Change REPEAT 12 times to REPEAT 8 times.
  • Change multiple of 4 to multiple of 3.
  • Swap the IF and the ELSE IF conditions.
  1. After each change, describe what happened to the pattern and its core.
  2. Make up your own change and ask your grown-up to predict the outcome.

What to look for: REPEAT 8 → 🌲❄️🌲⛄🌲❄️🌲⛄. Multiple of 3 → 🌲❄️⛄❄️🌲⛄🌲❄️⛄❄️🌲⛄ (core of 6). Swapped → no ⛄ at all.

Grown-up note: keep buttons, coins and pasta away from babies and toddlers.

Your turn

1

Change the winter pattern code to REPEAT 8 times. What is the outcome?

2

Keep REPEAT 12 times, but change multiple of 4 to multiple of 3. What is the outcome? What is the new core?

3

In Example 3, how many times does each loop run to say the multiples of 6 up to 60?

4

In the savings code (Example 5, with ≥ 48), change ADD 8 to ADD 12. What does it say now?

5

Will this loop ever stop? If not, fix it.

SET n = 0

REPEAT UNTIL n = 15

ADD 4 to n

6

This code checks every number from 1 to 50 and says the multiples of 5. Rewrite it to be more efficient.

SET n = 1

REPEAT 50 times

IF n is a multiple of 5 THEN SAY n

ADD 1 to n

7

What does this code say?

SET n = 6

REPEAT 3 times

ADD 6 to n

SAY n

More practice: warm-up

1

A code says REPEAT 3 times: SAY "go". Change the 3 to 5. What is the new outcome?

2

SET x = 10. ADD 2 to x. SAY x. Change ADD 2 to ADD 7. What does it say now?

3

IF n > 5 THEN SAY "big" ELSE SAY "small". Change > to <. What does it say for n = 8?

4

How many times does REPEAT 4 times run the lines inside it?

5

What is the outcome of a code?

More practice: on your own

1

What does this code say? Then change REPEAT 4 to REPEAT 5. What does it say now?

SET total = 0

SET n = 2

REPEAT 4 times

ADD n to total

ADD 2 to n

SAY total

2

In the same code (REPEAT 4 times), indent SAY total so it is the last line inside the loop. What is the outcome now?

3

In the ticket code (IF age < 6 → free, ELSE IF age < 13 → $5, ELSE → $9), change age < 13 to age < 18. Who pays a different price now?

4

This code finds the first n where 5n + 2 > 60:

SET n = 0

REPEAT UNTIL 5 × n + 2 > 60

ADD 1 to n

SAY n

It says 12. Lee changes ADD 1 to ADD 5 to make it faster. What does it say now? Is it still correct?

5

This code was meant to say 10, 20, 30, 40, 50. Find the bug and fix it.

SET n = 10

REPEAT 5 times

ADD 10 to n

SAY n

Challenge

1

Alter the winter pattern code so the outcome is 🌲🌲⛄🌲🌲⛄🌲🌲⛄.

2

A code adds 1 + 2 + 3 + ... + 100 by looping 100 times. Mira notices you can pair the numbers: 1 + 100, 2 + 99, 3 + 98, ... How could the code use her idea? How does it change the efficiency?

3

Run this code. Then change ADD 15 to ADD 20 and run it again.

SET savings = 20

SET weeks = 0

REPEAT UNTIL savings ≥ 100

ADD 15 to savings

ADD 1 to weeks

SAY weeks

Check your understanding

Quiz

1.

REPEAT 3 times: DRAW ⭐. If you change 3 to 7, how many stars are drawn?

2.

IF n > 5 THEN SAY "big" ELSE SAY "small" is changed to IF n ≥ 5 .... What does it say for n = 5 now?

3.

Version A loops 60 times and Version B loops 10 times. Both have the same outcome. Which is more efficient?

4.

SET n = 1. REPEAT UNTIL n = 10: ADD 2 to n. What happens?

5.

SET n = 5. REPEAT 3 times: ADD 5 to n, then SAY n. What is the outcome?

6.

SET savings = 0 and SET weeks = 0. REPEAT UNTIL savings ≥ 40: ADD 8 to savings and ADD 1 to weeks. What is weeks at the end?

For parents and teachers

Big idea: Grade 6 students read and alter existing code with conditional statements and other control structures, and describe how changes affect both the outcome and the efficiency (C3.2). Efficiency is new: two codes can give the same outcome, but one may take far fewer steps.

Common difficulties:

  • Order of ELSE IF conditions. If a general condition (even) comes before a specific one (multiple of 4), the specific one never runs.
  • Boundary conditions. ≥ 48 and > 48 can give different answers; "= 20" can create an infinite loop if the variable skips 20.
  • Making code "faster" but wrong. Fewer steps only count if the outcome is still correct.

How to help: Ask your child to predict before tracing, then change just one thing at a time. Use objects to act out pattern codes. Ask "How many times did the loop run? Could it run fewer times?" Social-emotional learning (A1): finding a bug is a success, not a failure; model calm, step-by-step checking.