The shape of the lesson
By the end of the lesson, learners can
- Count-controlled: FOR: In the lab, try the Nested FOR with STEP and REPEAT UNTIL programs.
- Pre-condition and post-condition: Use WHILE or REPEAT when you do not know in advance how many passes are needed: the loop stops when a condition changes.
- Totalling and counting: A running total adds each value on; a counter adds 1 each time something happens.
- FOR: known number of passes; the end value is included.
- WHILE: tested first, may run 0 times, continues while TRUE.
- REPEAT: tested last, runs at least once, stops when the UNTIL condition is TRUE.
- Totals and counts: set to 0 before the loop, update inside it, output after it.
One picture every idea returns to
Iteration means repeating a group of statements. A count-controlled loop (FOR ... NEXT) runs a set number of times. A pre-condition loop (WHILE ... DO ... ENDWHILE) tests its condition before each pass, so it may run zero times. A post-condition loop (REPEAT ... UNTIL) tests its condition after each pass, so it always runs at least once. Totalling (Total ← Total + Number) and counting (Count ← Count + 1) are done inside loops.
Forty-five minutes, phase by phase
| Time | Phase | What happens in the room | Grouping |
|---|---|---|---|
| 0 to 5 min | Hook: predict | Open with the question in the title, Do it again.. Computers are good at repeating things. Put the figure on the board: it is the one picture every idea in the lesson returns to. | Whole class, then pairs |
| 5 to 13 min | Teach and check: Count-controlled: FOR | In the lab, try the Nested FOR with STEP and REPEAT UNTIL programs. Project tab 1 of the lab (Loops with a trace table) and let a student drive it while the class predicts. Model the worked example Counting passes on the board, one line at a time. | Whole class, then pairs |
| 13 to 21 min | Teach and check: Pre-condition and post-condition | Use WHILE or REPEAT when you do not know in advance how many passes are needed: the loop stops when a condition changes. Model the worked example Trace a WHILE loop on the board, one line at a time. Your turn: What does the same WHILE program output for the input 9? | Pairs |
| 21 to 30 min | Teach and check: Totalling and counting | A running total adds each value on; a counter adds 1 each time something happens. Model the worked example Trace table on the board, one line at a time. Your turn: How would you output the average mark as well? | Jigsaw |
| 30 to 36 min | Check | The four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong. | Individual, then whole class |
| 36 to 43 min | Plenary | The exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording. | Individual, random call |
| 43 to 45 min | Exit and homework | Set the worksheet below (6 questions, 18 marks). Learners self-assess against the objectives before they leave. | Individual |
Protect the parts that carry the learning
The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.
Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.
Cut first if the build overruns: the last check in Totalling and counting, never the check or the plenary.
In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.
Jigsaw
Split the content into expert pieces. Expert groups master one piece each, then regroup so every home team has one expert per piece, and each expert teaches their piece to the team.
The full structure, with its variations and the other twelve, is on the cooperative-learning page.
What to head off, and how
| Trap learners fall into | Teaching move that pre-empts it |
|---|---|
| A FOR loop in pseudocode includes the end value: FOR Count ← 1 TO 5 runs 5 times. | Python's range(1, 5) would stop at 4, so do not mix the two up. |
| The REPEAT condition says when to stop; the WHILE condition says when to keep going. | Turning one into the other means reversing the condition. |
| Put Total ← 0 before the loop. | If it is inside the loop, the total is reset on every pass and only the last mark is kept. |
Iteration: count-controlled, pre-condition and post-condition loops: worksheet
6 questions, 18 marks. Written in the style of the papers; no past paper question is reproduced.
Describe the three types of loop used in pseudocode.
Complete a trace table for this pseudocode and state the output.
Total ← 0
Number ← 1
WHILE Total < 20 DO
Total ← Total + Number
Number ← Number + 2
ENDWHILE
OUTPUT Total, " ", NumberWrite pseudocode that uses a FOR loop to input 10 numbers, then outputs their total and their average.
Write pseudocode using a REPEAT ... UNTIL loop that makes the user enter a number from 1 to 10 inclusive.
Explain the difference between a pre-condition loop and a post-condition loop.
This loop should output the numbers 1 to 5 but never stops. Explain why and correct it.
Count ← 1
WHILE Count <= 5 DO
OUTPUT Count
ENDWHILEAnswer key and marking notes
- Count-controlled (FOR): repeats a set number of times.
- Pre-condition (WHILE): the condition is tested before each pass, so it may not run at all.
- Post-condition (REPEAT ... UNTIL): the condition is tested after each pass, so it runs at least once.
- Total: 0, 1, 4, 9, 16, 25.
- Number: 1, 3, 5, 7, 9, 11.
- The loop stops when Total is 25 (25 < 20 is FALSE).
- Output: 25 11
Total ← 0before the loopFOR Count ← 1 TO 10...NEXT CountINPUT NumberandTotal ← Total + Numberinside the loop- After the loop:
OUTPUT Total, Total / 10
REPEATINPUT Number(with a prompt)UNTIL Number >= 1 AND Number <= 10
- A pre-condition loop tests the condition before the body, so the body may run zero times.
- A post-condition loop tests the condition after the body, so the body always runs at least once.
- Count is never changed inside the loop, so Count <= 5 is always TRUE.
- Add
Count ← Count + 1inside the loop, after the OUTPUT.
6 multiple-choice questions on this lesson
From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.
Which type of loop always runs its body at least once?
- AA post-condition loop:
REPEAT ... UNTIL - BA pre-condition loop:
WHILE ... DO ... ENDWHILE - CA count-controlled loop:
FOR ... NEXT - DAny loop that changes a counter variable inside its body
How many times does the body of the loop FOR X ← 2 TO 14 STEP 3 run?
- A5
- B4
- C6
- D12
What is output by this pseudocode?
Number ← 50
Count ← 0
WHILE Number > 10 DO
Number ← Number - 12
Count ← Count + 1
ENDWHILE
OUTPUT Count, " ", Number- A4 2
- B3 14
- C5 -10
- D4 14
The inputs are 12, 8 and 5. What is output?
DECLARE Total : INTEGER
DECLARE Mark : INTEGER
DECLARE Index : INTEGER
FOR Index ← 1 TO 3
Total ← 0
INPUT Mark
Total ← Total + Mark
NEXT Index
OUTPUT Total- A5
- B25
- C12
- D0
This loop is rewritten as a WHILE loop with an INPUT Guess just before it and another at the end of its body.
REPEAT
INPUT Guess
UNTIL Guess = 7Which first line should the WHILE loop have?- A
WHILE Guess <> 7 DO - B
WHILE Guess = 7 DO - C
WHILE Guess > 7 DO - D
WHILE Guess < 7 DO
The inputs are 14, 20, 11 and -1. What is output?
Total ← 0
Count ← 0
INPUT Mark
WHILE Mark <> -1 DO
Total ← Total + Mark
Count ← Count + 1
INPUT Mark
ENDWHILE
OUTPUT Total / Count- A15
- B11
- C11.25
- D45
Quiz key
REPEAT ... UNTIL tests its condition after each pass, so the body has always run once before the first test. A WHILE loop tests first and may run zero times. A FOR loop runs a set number of times, which can be zero (for example FOR Count ← 5 TO 1). Changing a counter has nothing to do with when the condition is tested.
X takes the values 2, 5, 8, 11 and 14, and the end value is included, so the body runs 5 times. 4 forgets that 14 itself is reached. 6 would need one more value of X, for example a start at -1. 12 is 14 - 2, which ignores the STEP.
Number goes 50, 38, 26, 14, 2 and Count goes 0, 1, 2, 3, 4. When Number is 2 the test 2 > 10 is FALSE, so the loop stops and 4 2 is output. 3 14 stops one pass early (14 > 10 is still TRUE). 5 -10 does one pass too many. 4 14 gives the right count but the value before the last subtraction.
Total ← 0 is inside the loop, so the total is reset on every pass and only the last mark survives: 0 + 5 = 5. 25 is what the program should output, which needs Total ← 0 before the loop. 12 would be the first mark only. 0 would need the reset to come after the addition.
The REPEAT loop stops when Guess = 7, so the WHILE loop must keep going while Guess is not 7: Guess <> 7, the reverse of the UNTIL condition. Guess = 7 would loop only for the correct answer. Guess > 7 stops on any guess below 7, and Guess < 7 stops on any guess above 7, so both accept wrong guesses.
The test happens before each pass, so -1 ends the loop without being added or counted: Total = 45, Count = 3 and 45 / 3 = 15. 11 would come from adding and counting the -1 (44 / 4). 11.25 counts the -1 but does not add it (45 / 4). 45 is the total, not the average.
What to project
The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.
Slides for this lesson
A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.