Study Guide Instructor view

Iteration

Teaching notes for Lesson 5.5, with controls for presenting it.

Keep this page on your own screen. Share only the lesson window. The lesson window holds no teaching notes, so sharing it cannot show them.

Presenting

  1. Open the lesson in its own window and share that window, not this one.
  2. Use Next and Previous here, or the lesson’s own, to move through the lesson. Next reveals one thing at a time: the next idea, or the answer to the question on screen, then the next step. Previous takes the last reveal back. The line below says what is on screen and what Next will show.
  3. Reveal hints and answers when the class is ready, and reset a demonstration before using it again.

The controls need JavaScript and a browser that supports BroadcastChannel. Without them, open the lesson in presentation mode and use its own controls.

The two windows talk to each other only inside this browser, on this device. Nothing is sent to a server, and no account is needed. A second device (a phone or another tablet) cannot follow the lesson window yet.

Lesson plan

Level
Intermediate
Time
About 75 minutes; the steps below add up to 75

Pupils start from the pain of writing a thousand OUTPUT lines and ask for a way to repeat. They then meet FOR with STEP, WHILE (test first, may run zero times), REPEAT … UNTIL (test last, the condition says when to stop), how to choose between the three, and nested loops where the passes multiply. Tracing is the main tool: almost every question can be settled by writing the loop variable's values in a column on the board.

Objectives

  • Explain what iteration is and why programs need it
  • Write and trace count-controlled FOR … NEXT loops, with and without STEP, and say how many times they run
  • Write and trace pre-condition WHILE … DO … ENDWHILE loops and post-condition REPEAT … UNTIL loops
  • Choose the right kind of loop for a problem and justify the choice
  • Trace and write nested loops
  • Spot and correct a loop that never ends or runs the wrong number of times

Before the lesson

  • Mini whiteboards, one per pupil, for predictions and trace rows.
  • Write the FOR, WHILE, REPEAT and nested programs on the board before the lesson so that you can trace with the pen.
  • Optional: three props to stand for the loops, such as a counting clicker (FOR), a door that is checked before anyone goes in (WHILE) and a door that is checked on the way out (REPEAT).
  • Prepare a column on the board for each of Count, Coins and Table so that pupils can see variables change.

Steps

  1. Introduction Opening

    On screen: the lesson’s title, module and number; Next adds its objectives and key terms, as the lesson page lists them; Next again begins the first step.

    Objectives on screen

    • Explain what iteration is and why programs need it
    • Write and trace count-controlled FOR … NEXT loops, with and without STEP, and say how many times they run
    • Write and trace pre-condition WHILE … DO … ENDWHILE loops and post-condition REPEAT … UNTIL loops
    • Choose the right kind of loop for a problem and justify the choice
    • Trace and write nested loops
    • Spot and correct a loop that never ends or runs the wrong number of times

    Purpose: Pupils start from the pain of writing a thousand OUTPUT lines and ask for a way to repeat. They then meet FOR with STEP, WHILE (test first, may run zero times), REPEAT … UNTIL (test last, the condition says when to stop), how to choose between the three, and nested loops where the passes multiply. Tracing is the main tool: almost every question can be settled by writing the loop variable's values in a column on the board.

  2. 1. A countdown Think 4 min

    Purpose: Pupils feel the cost of repetition without loops (a thousand lines) and say for themselves what the language should let them do.

    Ask

    • What is wrong with one OUTPUT line per number for a countdown from 1000?
    • What would you want to be able to say instead?

    Responses to expect

    • It is too long and tiring, and a different countdown means rewriting it
    • Repeat this once for each number

    Misconceptions to challenge

    • A learner may believe: “A long program is fine as long as it works.” Respond: Ask what happens when the designers want 1500 instead of 1000, or when one line has a typing mistake among a thousand.

    Checking understanding: Pupils write the instruction they want on a mini whiteboard; any form of repeat for each is the target.

    Depending on the answers

    • If pupils only complain about length: ask what the program would need to say instead, and write repeat ... for each on the board before moving on.

    English as an additional language

    • Check countdown and blast off; count down aloud from 10 together.

    Discussion pause: Thirty seconds of silent thinking, then pairs say the instruction they wish they could give the computer.

    In the lesson window

    • Think it through: The designers now want a countdown from 1000. What is wrong with writing one OUTPUT line for each number, and what would you want a programming language to let you say instead?
  3. 2. Repeating a set number of times Predict 12 min

    Purpose: Pupils predict a FOR loop's output, trace it, count passes (inclusive ends) and meet STEP and the zero-pass case.

    Ask

    • What does FOR Count ← 1 TO 5 output, and what does Count hold each time?
    • How many times does the body of FOR Count ← 4 TO 9 run?
    • What does FOR Number ← 10 TO 0 STEP -5 output?

    Responses to expect

    • 2, 4, 6, 8, 10
    • 6, not 5
    • 10, 5 and 0

    Misconceptions to challenge

    • A learner may believe: “FOR Count ← 4 TO 9 runs 5 times because 9 - 4 = 5.” Respond: List the values: 4, 5, 6, 7, 8, 9. Both ends are included, so count them.
    • A learner may believe: “Count ← Count + 1 belongs inside the FOR loop.” Respond: NEXT Count does the adding; changing Count in the body is a mistake.
    • A learner may believe: “STEP -5 from 10 to 0 stops before 0.” Respond: The end value is included when the steps land on it: 10, 5, 0.
    • A learner may believe: “A FOR loop starting at 4 runs 9 times because it goes up to 9.” Respond: It starts at 4, not 1. Count how many values there are from 4 to 9.

    On the board or with the pen

    • Write Pass, Count and OUTPUT columns and fill them in row by row: 1, 2, 3, 4, 5 with output 2, 4, 6, 8, 10. Beside it write 4 5 6 7 8 9 and count the values.

    What learners should notice

    • The loop variable changes by itself, and its last value is the end value.

    Checking understanding: The passes-four-to-nine question: the 5 option is the off-by-one subtraction slip and the 9 option mistakes the last value for the number of passes. The STEP question checks that pupils start at the first value written.

    Depending on the answers

    • If pupils answer 5 passes: write the values on the board and number them one to six.
    • If pupils choose 0, 5 and 10: ask which value Number takes first and which direction STEP -5 moves it.

    Support

    • Give the trace table with the Count column partly filled.

    Stretch

    • How many times does FOR Count ← 20 TO 2 STEP -3 run? What is the last value?

    English as an additional language

    • Check pass, body and loop variable; draw a circle going round with a counter in the middle.

    In the lesson window

    • Think it through: Predict what this program outputs, in order. How many times does the OUTPUT line run, and what does the variable Count hold each time?
    • Check your understanding: How many times does the body of FOR Count ← 4 TO 9 run?
    • Check your understanding: What does the FOR Number ← 10 TO 0 STEP -5 loop output?
  4. 3. Repeating while something is true Discover 14 min

    Purpose: Pupils work out the drinks machine by hand without knowing in advance how many repeats, then meet WHILE, trace it, find that it can run zero times and diagnose an infinite loop.

    Ask

    • How many drinks does the pupil get and how many coins are left? Did you know in advance how many repeats?
    • The pupil starts with 1 coin: how many times does the body run?
    • Why does a loop with no Coins change in the body never finish?

    Responses to expect

    • 3 drinks and 1 coin left, found by taking away 2 each time
    • Zero times
    • The condition can never become false

    Misconceptions to challenge

    • A learner may believe: “Every loop runs at least once.” Respond: A WHILE loop tests first; with 1 coin the test is false before any pass.
    • A learner may believe: “A WHILE loop stops when the variable reaches 0.” Respond: It stops when its condition is false: Coins >= 2 is false for 1.
    • A learner may believe: “The loop stops because the body has run enough times.” Respond: Only the condition stops a WHILE loop; if nothing in the body changes Coins, it can never become false.
    • A learner may believe: “Missing declarations make a loop infinite.” Respond: That is a different fault; look at which variable the condition tests and whether the body changes it.

    On the board or with the pen

    • Fill the drinks table row by row (before pass 1, after pass 1, 2, 3, and after the loop) with columns Coins, Drinks and Coins >= 2? and mark where the test is made.

    What learners should notice

    • The condition is tested before each pass, including the first.

    Checking understanding: The zero-passes and infinite-loop questions: each wrong option names a plausible belief (always at least once, WHILE repeats forever, declarations); the drinks trace shows whether pupils separate the loop's test from its body.

    Depending on the answers

    • If pupils say once for 1 coin: ask them to evaluate Coins >= 2 for 1 before the first pass.
    • If pupils choose the declaration option for the infinite loop: ask which variable the WHILE test reads and whether any line in the body changes it.
    • If pupils are secure: ask what Coins and Drinks hold if the machine starts with 10 coins and sells drinks for 3.

    Support

    • Provide the trace table with the Coins column filled in and ask pupils to complete only the condition column.

    Stretch

    • Rewrite the drinks machine so that the price is a CONSTANT and the number of coins is typed in.

    English as an additional language

    • Check pre-condition and infinite as before and never ends; mime a hamster wheel for an infinite loop.

    In the lesson window

    • Think it through: How many drinks does the pupil get, and how many coins are left? Describe how you worked it out, and say whether you knew in advance how many times you would repeat the step.
    • Check your understanding: The pupil starts with 1 coin instead of 7. How many times does the body of the WHILE loop run?
    • Apply it: A programmer writes the loop below and the program never finishes. Why? WHILE Coins >= 2 DO Drinks ← Drinks + 1 ENDWHILE
  5. 4. Repeating until something is true Discover 12 min

    Purpose: Pupils see why a WHILE loop is awkward for an ask-at-least-once task, meet REPEAT … UNTIL, trace the password program, and meet the reversed condition error.

    Ask

    • What would a WHILE loop's test have to look at before the first password has been typed?
    • In the password trace, what is output during each pass and after the loop?
    • Why does UNTIL Password <> “Secret” stop on a wrong password?

    Responses to expect

    • Nothing has been typed yet
    • Enter the password twice, then Welcome once
    • UNTIL stops when its condition is true

    Misconceptions to challenge

    • A learner may believe: “UNTIL works like WHILE: it repeats while the condition is true.” Respond: UNTIL says when to stop. Check the trace: the loop stopped when Password = “Secret” was true.
    • A learner may believe: “A REPEAT loop always makes only one pass.” Respond: It stops only when the UNTIL condition is true; in the trace it made two passes.
    • A learner may believe: “OUTPUT after UNTIL runs on every pass.” Respond: It is after the loop, so it runs once, when the loop has stopped.

    On the board or with the pen

    • Draw WHILE and REPEAT side by side with the test marked at the top of the first and the bottom of the second; write the password trace under the REPEAT diagram.

    Checking understanding: The until-meaning question: the first option reverses the direction of the condition, the third claims REPEAT always stops; both are answered by the trace rows.

    Depending on the answers

    • If pupils choose the repeat-while option: ask them to trace UNTIL Password <> “Secret” with abc as the first password and say when it stops.
    • If pupils turn the condition correctly: ask them to write the matching UNTIL condition for WHILE Coins >= 2.

    Support

    • Provide the trace table with the first row completed.

    Stretch

    • Convert the REPEAT password loop into a WHILE loop with one extra INPUT before it. Where does the extra line go?

    English as an additional language

    • Check until and stop; use a simple example such as keep walking until you reach the door.

    In the lesson window

    • Think it through: Could a WHILE loop do this without any extra work? Think about what the test would have to look at before the first password has been typed.
    • Check your understanding: A programmer writes UNTIL Password <> “Secret” and finds the loop stops as soon as a wrong password is typed. Why?
  6. 5. Choosing the right loop Apply 8 min

    Purpose: Pupils classify four tasks by loop type and then choose and justify a loop and its condition for an age-checking sign-up page.

    Ask

    • Is the number of repeats known before the loop starts?
    • Must the body run at least once?
    • Which loop and condition for asking an age until it is from 1 to 120?

    Responses to expect

    • FOR for 100 numbers and for 20 stars
    • REPEAT for the PIN
    • WHILE for subtracting while at least 5
    • REPEAT … UNTIL Age >= 1 AND Age <= 120

    Misconceptions to challenge

    • A learner may believe: “FOR is best because there are 120 possible ages.” Respond: The page does not know how many tries the user will need; FOR is for a count known in advance.
    • A learner may believe: “WHILE with the valid condition keeps asking until valid.” Respond: That keeps going while the age is valid, which is backwards, and nothing has been typed at the first test.

    On the board or with the pen

    • Write the three questions as a decision list on the board: count known? zero passes possible? ask at least once? and choose a loop for each.

    Checking understanding: The loop-choice table and the valid-age question: look for pupils who justify the choice by the three tests, not by habit.

    Depending on the answers

    • If pupils choose WHILE for the valid age: ask them to say when the loop should stop and what has been typed before its first test.
    • If pupils are quick: ask them to write the UNTIL condition for a PIN of 1234.

    Support

    • Give the three tests on a card: known count, may run zero times, must run once.

    English as an additional language

    • Check best fits and count known; point to the loop that tests first and the one that tests last.

    In the lesson window

    • Apply it: A fictional sign-up page keeps asking for an age until the user types a number from 1 to 120. Which loop is best, and what is its condition?
  7. 6. Loops inside loops Predict 10 min

    Purpose: Pupils predict the total output of a nested times-table program, trace one pass of the outer loop, and apply the multiplication rule to a new grid.

    Ask

    • How many numbers does the times-table program output, and what are the first eight?
    • Which loop changes faster?
    • A Row loop of 5 contains a Column loop of 6: how many times does the inner OUTPUT run?

    Responses to expect

    • 12 numbers: 1, 2, 3, 4, 2, 4, 6, 8
    • The inner loop
    • 30

    Misconceptions to challenge

    • A learner may believe: “Two loops give 5 + 6 passes.” Respond: That is for loops one after the other. When one is inside the other, the inner runs 6 times for each of the 5 outer passes.
    • A learner may believe: “The inner loop runs once overall.” Respond: It starts again for every outer pass.
    • A learner may believe: “The inner variable keeps its last value on the next outer pass.” Respond: It starts again from its first value, as the trace for Table 3 shows.

    On the board or with the pen

    • Draw a grid with Table 1 to 3 down the side and Number 1 to 4 across, then write the product in each cell and number the order cells are output in (row by row).

    Follow-up questions

    • How many OUTPUT runs for three nested loops of 2 passes each?

    Checking understanding: The how-many-outputs check separates adding from multiplying; the trace for Table 3 shows whether pupils see the inner loop restart.

    Depending on the answers

    • If pupils answer 11: draw the 5 by 6 grid and count the cells.
    • If pupils are secure: ask what changes in the output if the loops are swapped so that Number is the outer loop.

    Support

    • Provide the grid on paper so that pupils fill in numbers before tracing.

    Stretch

    • Predict the output for the loops in the opposite order and say which numbers come first.

    English as an additional language

    • Check nested and inner; show the idea with a hand inside a hand.

    In the lesson window

    • Think it through: Predict how many numbers the program outputs in total, and write out the first eight. Explain which loop changes faster.
    • Check your understanding: A program has FOR Row ← 1 TO 5 containing FOR Column ← 1 TO 6, and the inner body has one OUTPUT. How many times does that OUTPUT run?
  8. 7. Write loops of your own Apply 9 min

    Purpose: Pupils write a times-table loop, justify the loop type, and correct Sam's countdown.

    Ask

    • Write the 12 times table up to 12 x 12 = 144, and say which loop you chose and why.
    • What does Sam's FOR Count ← 5 TO 1 output, and how do you fix it?

    Responses to expect

    • A FOR loop from 1 to 12 with OUTPUT Number, " x 12 = ", Number * 12
    • Nothing, because 5 is already past 1; add STEP -1

    Misconceptions to challenge

    • A learner may believe: “A FOR loop counts down on its own if the start is bigger.” Respond: Without STEP it counts up by 1; the body never runs.
    • A learner may believe: “The loop would output 5 once.” Respond: The test is made before the first pass; 5 is already past 1, so there are zero passes.
    • A learner may believe: “Spaces are added automatically between OUTPUT items.” Respond: The pieces are joined with nothing between them; the spaces are inside the quotation marks.

    On the board or with the pen

    • Write Sam's program and ask a pupil to trace Count with the pen: it starts at 5, which is already past 1; then add STEP -1 and trace again.

    Checking understanding: The written program: check the loop header, the OUTPUT format and the use of the loop variable. Sam's question separates a correct trace from a wished-for output.

    Depending on the answers

    • If pupils choose 5, 4, 3, 2, 1: ask them to find the default step in the notes and trace the loop variable.
    • If pupils write WHILE for the times table: ask whether the count is known; accept WHILE only if they can say why FOR is neater.

    Support

    • Provide the first and last lines of the times-table program.

    Stretch

    • Output all twelve tables, one under another, using nested loops.

    In the lesson window

    • Think it through: Write pseudocode that outputs the 12 times table in the form “1 x 12 = 12”, “2 x 12 = 24” and so on up to “12 x 12 = 144”. Say which kind of loop you chose and why.
    • Apply it: Sam writes a countdown from 5 to 1 as: FOR Count ← 5 TO 1, then OUTPUT Count, then NEXT Count. What does it output, and how should it be fixed?
  9. 8. Show what you know Consolidate 6 min

    Purpose: Pupils explain the three loops with examples of their own and choose a loop for a first-value-may-be-zero quiz, before the summary.

    Ask

    • What is the difference between FOR, WHILE and REPEAT … UNTIL, with your own example for each?
    • Which of them can run zero times?
    • A quiz input ends at 0 and might begin with 0: which loop, and where does the INPUT go?

    Responses to expect

    • FOR known count, WHILE test first, REPEAT at least once
    • Only WHILE
    • INPUT before the loop, WHILE Score <> 0, next INPUT at the end of the body

    Misconceptions to challenge

    • A learner may believe: “REPEAT … UNTIL Score = 0 handles an immediate 0.” Respond: The body runs before the test, so the first 0 would be recorded. Read before the loop.
    • A learner may believe: “FOR works because the quiz has a limit.” Respond: The user decides when to finish, so the count is not known.

    Checking understanding: Collect the explanations: the best give a distinct example for each loop and name WHILE as the only one that can run zero times.

    Depending on the answers

    • If pupils choose REPEAT: ask what REPEAT does with a first value of 0 before the test.
    • If several are unsure about the extra INPUT: start the next lesson by tracing the two-INPUT pattern with scores 5, 3, 0.

    In the lesson window

    • Think it through: Explain the difference between FOR, WHILE and REPEAT … UNTIL to a friend, using a different example of your own for each, and say which of them can run zero times.
    • Apply it: A fictional quiz game asks for scores one at a time and outputs “Recorded” for each one. The user types 0 to finish, and might type 0 straight away, in which case nothing should be recorded. Which description of the loop is correct?
  10. Lesson recap Closing

    On screen: the lesson’s objectives again; Next adds the closing question, which asks the class to explain each one in their own words without looking back. It says nothing about what anyone has learnt: use the answers to decide what to revisit.

    Points to listen for

    • Iteration repeats instructions: FOR for a known count (both ends included, STEP can be negative), WHILE tests first and can run zero times, REPEAT tests last and runs at least once.
    • A WHILE body must change something in its condition or the loop never ends.
    • UNTIL says when to stop; WHILE says when to carry on.
    • Nested loops multiply: the inner loop restarts for every outer pass.
    • Trace the loop variable to settle how many passes there are.

Consolidation

  • Iteration repeats instructions: FOR for a known count (both ends included, STEP can be negative), WHILE tests first and can run zero times, REPEAT tests last and runs at least once.
  • A WHILE body must change something in its condition or the loop never ends.
  • UNTIL says when to stop; WHILE says when to carry on.
  • Nested loops multiply: the inner loop restarts for every outer pass.
  • Trace the loop variable to settle how many passes there are.