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

Reading and Altering Code: Counts, Sub-programs and Efficiency

C3.2 A1About 50 minutes
Learning goals
  • I can read existing code with counters and sub-programs and predict what it will do.
  • I can alter code to change its outcome, and explain how the outcome changes.
  • I can find and fix bugs, such as a loop that never stops.
  • I can describe how a change affects the efficiency of the code.

Words to know

outcome
What the code does or produces when it runs: what it says, draws or calculates.
alter
Change existing code on purpose.
bug
A mistake in code that makes it do something you didn't intend.
debug
Find and fix bugs.
infinite loop
A loop that never stops, because its stopping condition never becomes true.
efficiency
How few lines the code needs and how few steps it takes to run.

Remember from Grade 6 and the last lesson

In Grade 6 you read and changed code and described how each change affected the outcome and the efficiency. In the last lesson you wrote code with counters and sub-programs.

Today you're a code detective. For every change, ask two questions:

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

Let's start with some winter lights

A family programs their window lights for a winter festival. Read the code and predict: how many blinks in all?

DEFINE BLINK(times)
REPEAT times
LIGHT ON
LIGHT OFF
END DEFINE
SET count = 0
REPEAT 6 times
ADD 1 to count
IF count is a multiple of 2 THEN
BLINK(3)
ELSE
BLINK(1)

The winter lights code. BLINK is a sub-program with an input.

Reading the code

The counter goes 1, 2, 3, 4, 5, 6.

  • Odd counts (1, 3, 5): not multiples of 2, so BLINK(1): 1 blink each, 3 blinks.
  • Even counts (2, 4, 6): BLINK(3): 3 blinks each, 9 blinks.

Total: 3 + 9 = 12 blinks. The pattern is: 1, 3, 1, 3, 1, 3.

count 1BLINK(1)count 2BLINK(3)count 3BLINK(1)count 4BLINK(3)count 5BLINK(1)count 6BLINK(3)1 + 3 + 1 + 3 + 1 + 3 = 12 blinks
Odd counts call BLINK(1) and even counts call BLINK(3). Each bulb is one blink: 12 blinks in all.
AlterationNew outcomeEffect on efficiency
REPEAT 6 times → REPEAT 9 timesodd counts 1, 3, 5, 7, 9 (5 blinks) + even 2, 4, 6, 8 (12 blinks) = 17 blinksSame number of lines. More steps (9 loops instead of 6).
multiple of 2 → multiple of 3counts 3 and 6 blink 3 times (6), the other four blink once (4) = 10 blinksNo change in lines or loops.
BLINK(3) → BLINK(2)3 × 1 + 3 × 2 = 9 blinksFewer light steps; same number of lines.
Remove the sub-program and copy LIGHT ON / LIGHT OFF lines insteadSame 12 blinks if done carefullyMany more lines, harder to read and change. Less efficient.
Each change to the winter lights code, and what it does.

Predict, then trace

Before you run altered code, predict what it will do. Then trace it with a tracking table. If your prediction was wrong, that's great information: figure out why.

Worked example

Example 1: A bug that never stops

This code should stop when x reaches 25. What happens? Fix it.

SET x = 0

REPEAT UNTIL x = 25

ADD 10 to x

SAY x

  1. Trace: x = 10, 20, 30, 40, ... x jumps over 25, so x = 25 is never true.
  2. The loop never stops: it's an infinite loop. The outcome is that nothing is ever said.
  3. Fix: change the condition to REPEAT UNTIL x ≥ 25. Now x goes 10, 20, 30 and stops. It says 30.

Change x = 25 to x ≥ 25. Safer conditions use ≥ or ≤ instead of = when the count jumps by more than 1.

05101520253035404550+10+10+10+10+10REPEAT UNTIL x = 25x is never exactly 25, so the loop never stops!…and on
Adding 10 each time makes x jump right over 25. Changing the condition to x ≥ 25 stops the loop at 30.
Worked example

Example 2: The line order matters

In the hockey code from the last lesson, Kai moves ADD 1 to lap to the end of the loop. What happens?

SET lap = 0

REPEAT 12 times

IF lap is a multiple of 4 THEN SAY "Water break!"

ADD 1 to lap

  1. Now the IF checks lap before adding. The values checked are 0, 1, 2, ..., 11.
  2. 0 is a multiple of 4 (0 = 4 × 0), so the code says "Water break!" before the first lap!
  3. Then breaks at 4 and 8. At the end, lap is 12, but the loop is over, so there's no break after lap 12.

Outcome changes: breaks happen at the start and after laps 4 and 8, not after 4, 8 and 12. Efficiency is the same. Line order can change the outcome without changing the length.

Worked example

Example 3: Same outcome, fewer steps

Both codes say 25, 50, 75, 100. Which is more efficient?

Code A: SET n = 0, REPEAT 100 times: ADD 1 to n, IF n is a multiple of 25 THEN SAY n

Code B: SET n = 25, REPEAT 4 times: SAY n, ADD 25 to n

  1. Code A runs its loop 100 times and checks the IF 100 times.
  2. Code B runs its loop only 4 times and doesn't need an IF.
  3. Both have about the same number of lines, but B takes far fewer steps.

Code B is more efficient. It skips the numbers it doesn't need.

Worked example

Example 4: Make a sub-program more useful

Alter SQUARE(size) so it can draw any regular polygon.

  1. A square turns 90° four times: 4 × 90° = 360°. Any regular polygon turns a total of 360°, so each turn is 360° ÷ sides.
  2. New sub-program: DEFINE POLYGON(sides, size), REPEAT sides times: MOVE size, TURN RIGHT 360° ÷ sides.
  3. POLYGON(4, 3) draws a square with sides of 3. POLYGON(6, 2) draws a hexagon with sides of 2 (turns of 60°). POLYGON(3, 5) draws a triangle with sides of 5 (turns of 120°).

One sub-program now draws many shapes. That is more efficient than writing a separate sub-program for each shape.

Worked example

Example 5: Fix a savings bug

This code should say the first week Maya has at least $160. What does it really say? Fix it.

SET savings = 35

SET week = 0

REPEAT 52 times

ADD 12.50 to savings

ADD 1 to week

IF savings ≥ 160 THEN SAY week

  1. Savings reach $160 at week 10, so it says 10. But at week 11 savings are still ≥ 160, so it says 11, then 12, and so on to 52.
  2. Outcome: it says 43 numbers instead of one. Efficiency: it runs 52 loops when 10 would do.
  3. Fix: use REPEAT UNTIL savings ≥ 160 (with the two ADD lines inside), then SAY week after the loop.

The fixed code says only 10, and it stops after 10 loops. Better outcome and better efficiency.

Just for fun

Why did the programmer get dizzy?

They were stuck in an infinite loop... going around and around and around and around...

Hands-on activity

Code swap

You need: paper, a pencil and a partner (a grown-up or sibling).

  1. Each of you writes a short program that uses a counter and a sub-program. Use actions you can do at home: CLAP, STOMP, JUMP, or drawing on grid paper. For example: DEFINE CHEER(n): REPEAT n times: CLAP. Main: SET c = 0, REPEAT 5 times: ADD 1 to c, CHEER(c).
  2. Swap papers. Read your partner's code and predict the outcome. (For the example: 1 + 2 + 3 + 4 + 5 = 15 claps.)
  3. Run it together to check.
  4. Make one alteration to your partner's code. Predict the new outcome, then run it.
  5. Explain how your change affected the outcome and the efficiency.

Your turn

1

In the winter lights code, change BLINK(1) to BLINK(2). How many blinks now?

2

SET n = 0, REPEAT 5 times: ADD 3 to n, SAY n. What does it say? What if you change ADD 3 to ADD 1.5?

3

In the same code (with ADD 3), swap the two lines inside the loop so SAY n comes first. What does it say now?

4

Find the bug: SET x = 1, REPEAT UNTIL x = 10: ADD 2 to x. Then fix it.

5

Alter the hockey code (12 laps, a break every 4 laps) to give a break every 3 laps. How many breaks now?

6

Code A uses REPEAT 50 times and an IF to say the multiples of 10 up to 50. Code B starts at 10, says n and adds 10, 5 times. Same outcome? Which is more efficient?

More practice: warm-up

1

SET c = 0, REPEAT 4 times: ADD 1 to c. What is c at the end? What if you change 4 to 9?

2

What is an infinite loop?

3

REPEAT 3 times: SAY "Go team!". How many times does it say it? Change 3 to 5.

4

Which is more efficient: copying 4 lines 10 times, or a loop that repeats them 10 times?

5

Calling BLINK(4) makes how many blinks?

More practice: on your own

1

DEFINE ROW(n): REPEAT n times: DRAW ⭐, then NEW LINE. Main: SET size = 1, REPEAT 4 times: ROW(size), ADD 1 to size. How many stars? What if you change ADD 1 to ADD 2?

2

SET t = 3, SET n = 1, REPEAT UNTIL n = 6: SAY t, ADD 4 to t, ADD 1 to n. What does it say? What if you change the condition to REPEAT UNTIL n > 6?

3

Look at Example 5 again. Explain in your own words why REPEAT UNTIL is better than REPEAT 52 times.

4

Code A draws 3 squares by writing MOVE and TURN 4 times for each square (24 lines). Code B uses the SQUARE sub-program (5 lines to define) and calls it 3 times. How many lines does Code B need (not counting moves between squares)? Which is more efficient?

5

Change POLYGON(6, 2) to POLYGON(8, 2). What shape is drawn, and what is each turn?

Challenge

1

In the winter lights code (REPEAT 6 times), what condition would make the lights do exactly two long BLINK(3)s? How many blinks in all?

2

Code A adds 1 + 2 + 3 + ... + 100 with a loop that runs 100 times. Code B says 100 × 101 ÷ 2. Do they give the same outcome? Compare efficiency.

3

Kai wants the hockey code to say "Water break!" after laps 4, 8 and 12 and "Last lap!" at the start of lap 12. Alter the code.

Check your understanding

Quiz

1.

SET k = 0, REPEAT UNTIL k = 15: ADD 4 to k. What happens?

2.

In the winter lights code, you change REPEAT 6 times to REPEAT 4 times. How many blinks?

3.

SET n = 2, REPEAT 3 times: SAY n, ADD 2 to n. You swap the two lines inside the loop. What does it say now?

4.

Two codes have the same outcome. Code A runs 60 loops. Code B runs 6. What can you say?

5.

Which alteration makes POLYGON(sides, size) draw a triangle with sides of 7?

6.

Why is changing REPEAT 52 times to REPEAT UNTIL savings ≥ 160 a good alteration?

For parents and teachers

Big idea: Grade 7 students read and alter existing code, including code with events influenced by a defined count and/or sub-programs and other control structures, and describe how changes affect the outcomes and the efficiency of the code (C3.2). Reading code carefully is as important as writing it.

Common difficulties:

  • Conditions with = when the counter skips that number (infinite loops). Suggest ≥ or ≤.
  • Line order: adding to a counter before or after a check changes the outcome.
  • Confusing shorter with more efficient. Efficiency is about both lines and steps; two programs can be the same length but take very different numbers of steps.

How to help: Play the code swap game. When your child alters code, ask "What do you predict?" first, then "Why was your prediction right or wrong?" Treat bugs as puzzles, not failures. This lesson builds A1 skills: managing frustration while debugging, persevering and explaining reasoning.