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

Reading and Changing Code with Nested Loops

C3.2 A1About 40 minutes
Learning goals
  • I can read code with nested loops and predict the outcome.
  • I can change (alter) code and describe how the change affects the outcome.
  • I can find and fix bugs in nested-loop code.
  • I can explain why moving a line into or out of a loop changes the outcome.

Words to know

alter
Change something in the code.
predict
Say what the code will do before you run it.
bug
A mistake in the code that makes it do the wrong thing.
debug
Find and fix a bug.
outcome
What happens when the code runs.

Remember

In the last lesson you wrote code with a nested loop: a loop inside a loop. Today you will be a code detective. You will read code, predict what it does, change it, and explain how each change affects the outcome.

The starting code

Here is Beep's staircase code from the last lesson. The line numbers help us talk about the code.

WHEN GO is pressed
REPEAT 2 times
REPEAT 3 times
➡ Move right 1
⬆ Move up 1
STOP

Line 2 is the outside loop. Line 3 is the inside loop. Line 4 is inside both loops. Line 5 is inside the outside loop only.

A
The outcome: Beep (A) moves right 3, up 1, right 3, up 1, and reaches the star.

Three kinds of changes

  1. Change the outside number (line 2). The whole group happens more or fewer times.
  2. Change the inside number (line 3). Each group has more or fewer of the inside step.
  3. Move a line into or out of a loop by changing its indent. This changes how many times that line runs.
Worked example

Example 1: Change the outside loop

Change line 2 to REPEAT 3 times. Predict where Beep ends.

  1. The inside loop does not change: right 3 each time.
  2. Now right 3, up 1 happens 3 times instead of 2.
  3. Right: 3 × 3 = 9 squares. Up: 3 × 1 = 3 squares.

Beep ends 9 right and 3 up. The staircase has one more step. (On our 7-square-wide grid, Beep would go off the edge, so this change would need a bigger grid!)

Worked example

Example 2: Change the inside loop

Start again from the original code. Change line 3 to REPEAT 1 time. Describe the new path.

  1. Now the inside loop moves right only 1.
  2. The outside loop still runs 2 times: right 1, up 1, right 1, up 1.

Beep ends 2 right and 2 up. Each step of the staircase is now narrower (1 square wide instead of 3).

Worked example

Example 3: Move a line into the inside loop

Start from the original code. Indent line 5 one more time, so Move up 1 is inside both loops. What happens?

  1. Now the inside loop is: right 1, up 1. It runs 3 times.
  2. The outside loop runs that 2 times.
  3. So right 1, up 1 happens 2 × 3 = 6 times.

Beep moves 6 right and 6 up, in tiny steps like a diagonal staircase. One indent changed the number of up moves from 2 to 6!

A
After the change in Example 3: right 1, up 1, six times. (Beep needs a 7 by 7 grid.)

Changing math code

Here is a counting code. Let's read it, then change it.

SET total = 0
REPEAT 4 times
REPEAT 5 times
ADD 2 to total
SAY total

ADD 2 runs 4 × 5 = 20 times. 20 × 2 = 40. It says 40.

Worked example

Example 4: How do changes affect the outcome?

Predict the new outcome for each change, starting from the code above each time: (a) line 4 becomes ADD 3 (b) line 2 becomes REPEAT 2 (c) line 5 is indented so SAY is inside the outside loop.

  1. (a) ADD 3 still runs 20 times: 20 × 3 = 60. Adding a bigger number makes the outcome bigger.
  2. (b) Now ADD 2 runs 2 × 5 = 10 times: 10 × 2 = 20. Half as many repeats gives half the outcome.
  3. (c) SAY now runs after each time through the outside loop. Each time adds 5 × 2 = 10. It says 10, 20, 30, 40.

(a) 60, (b) 20, (c) 10, 20, 30, 40.

Worked example

Example 5: Debug an array

Ali wants to draw 3 rows of 5 stars. His code:

REPEAT 3 times

    REPEAT 5 times

        DRAW ⭐

        GO to the next row

The outcome is wrong. What happens, and how do you fix it?

  1. Run it: draw ⭐, go to next row, draw ⭐, go to next row ... Every star is on its own row.
  2. GO to the next row is inside the inside loop, so it runs 15 times. It should only run after each row of 5.
  3. Fix: move GO to the next row out of the inside loop (indent it only once).

Fixed code:

REPEAT 3 times

    REPEAT 5 times

        DRAW ⭐

    GO to the next row

Now it draws 3 rows of 5 stars (15 stars).

Code detective steps

  1. Read each line. Point to the indents to see which loop each line is in.
  2. Predict the outcome. Multiply the loop numbers to count how many times a line runs.
  3. Run it step by step (on paper or by acting it out).
  4. If the outcome is wrong, find the bug: a wrong number, a wrong step, or a line in the wrong loop.
  5. Describe how your change affected the outcome.
A funny-but-true fact

Coders call mistakes in code bugs. In 1947, computer scientists found a real moth stuck inside a giant computer, and taped it into their notebook as the "first actual case of bug being found"!

Hands-on activity

Debug the grown-up robot

You need: paper, a pencil, a small toy (the robot), and masking tape or a grid drawn on paper (at least 7 by 7 squares).

  1. Make a grid on the floor with tape, or draw one on paper. Put the toy in the bottom-left square.
  2. Write Beep's staircase code (REPEAT 2, REPEAT 3, right 1, up 1) and run it with the toy.
  3. Now make one change at a time: change the outside number, change the inside number, or move up 1 into the inside loop. Predict first, then run it. Describe how the outcome changed.
  4. Your grown-up writes code with a bug on purpose (for example, a number that makes the toy go too far). Find the bug and fix it.

What to look for: your child predicts before running, and can say, for example, "Moving the line into the inside loop made it run 6 times instead of 2."

Your turn

1

How many claps and stomps?

REPEAT 4 times

    REPEAT 2 times

        CLAP

    STOMP

2

Change the code above so the inside loop is REPEAT 3 times. How does the outcome change?

3

In Beep's staircase code, change line 2 to REPEAT 1 time. Where does Beep end?

4

What does it say?

SET total = 0

REPEAT 2 times

    REPEAT 5 times

        ADD 10 to total

SAY total

5

Change that code so SAY total is indented once (inside the outside loop). What does it say now?

6

Mia wants code that draws 2 rows of 4 dots, but her outside loop says REPEAT 4 and her inside loop says REPEAT 2. What does her code draw?

7

Which change makes a line inside both loops run more times: changing the outside loop from 3 to 4, or changing the inside loop from 5 to 6, if the starting code has outside 3 and inside 5?

More practice: warm-up

1

What does it mean to alter code?

2

How many jumps?

REPEAT 3 times

    REPEAT 3 times

        JUMP

3

Change the outside loop of that code to 4. How many jumps now?

4

What is a bug?

5

If a line is inside the outside loop only and the outside loop says REPEAT 5, how many times does the line run?

More practice: on your own

1

What does it say? Then change ADD 5 to ADD 1. What does it say now?

SET n = 0

REPEAT 3 times

    REPEAT 4 times

        ADD 5 to n

    SAY n

2

Where does Beep end? Alter the code so Beep ends 2 right and 8 up.

REPEAT 2 times

    REPEAT 3 times

        ⬆ Move up 1

    ➡ Move right 1

3

Ben wants to say hip, hip, hooray 3 times. What is the bug in his code?

REPEAT 3 times

    REPEAT 2 times

        SAY hip

        SAY hooray

4

Code makes an array: outside REPEAT 6, inside REPEAT 4. You want 6 rows of 8 instead. What do you change? How many dots now?

5

Beep and Boop are in the same row. Boop starts 6 squares to the right of Beep. They run their code at the same time. Do they meet?

Beep:

REPEAT 2 times

    REPEAT 2 times

        ➡ Move right 1

Boop:

REPEAT 2 times

    REPEAT 1 time

        ⬅ Move left 1

Challenge

1

Predict what this code says. Then alter one number so the outcome is exactly double.

SET total = 0

REPEAT 5 times

    ADD 1 to total

    REPEAT 2 times

        ADD 3 to total

SAY total

2

You want code that says 3, 6, 9, 12, 15, 18. Fix this code.

SET n = 0

REPEAT 3 times

    REPEAT 2 times

        ADD 3 to n

    SAY n

3

Explain why changing the outside loop from 2 to 4 always doubles how many times a line inside both loops runs.

Check your understanding

Quiz

1.

Code: outside REPEAT 3, inside REPEAT 4, CLAP inside both. You change the inside loop to REPEAT 5. How many claps now?

2.

A line is moved from the outside loop into the inside loop. What happens?

3.

Beep's staircase code (outside 2, inside 3 right, then up 1) is changed so the outside loop is 4. Where does Beep end?

4.

What does this code say?

SET t = 0

REPEAT 2 times

    REPEAT 3 times

        ADD 4 to t

SAY t

5.

Code meant to draw 3 rows of 5 stars puts every star on its own row. What is the most likely bug?

6.

Changing ADD 2 to ADD 4 in a counting code (and nothing else) makes the outcome...

For parents and teachers

Big idea: The Ontario curriculum (C3.2) asks Grade 4 students to read and alter existing code, including code with sequential, concurrent, repeating and nested events, and to describe how changes to the code affect the outcomes. The most important idea: the indent decides which loop a line is in, and that decides how many times it runs.

Common difficulties:

  • Not noticing indents. Have your child put a finger on each line and say "inside both loops" or "outside loop only".
  • Changing many things at once. Encourage one change at a time, with a prediction first.
  • Describing only the new answer. Ask "How is it different from before? Bigger, smaller, double, half?"
  • Frustration with bugs. Bugs are normal for every coder. Praise careful checking.

How to help: Make a grid with masking tape and let your child "program" you or a toy. Then make one change and ask them to predict the new outcome before you move (Strand A: making conjectures and reflecting).