🎯 Syllabus & Goals 3 min
Cambridge 8.1.4 · Iteration Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Write a count-controlled
FOR … TO … NEXTloop (with and withoutSTEP) and its Pythonfor … in range()version. - Write a pre-condition
WHILE … ENDWHILEloop and a post-conditionREPEAT … UNTILloop, and the Python equivalents. - Choose the right loop for a task, and trace a loop with a trace table.
Textbook: Chapter 8, §8.1.4(c) (pp. 312–314)
Recap / Warm-Up 5 min
Last lesson: selection chooses between paths using a condition. Loops use conditions too — to decide whether to go round again.
Quick starter
You need to print a class register of exactly 30 names. Do you know in advance how many times the printing step repeats?
Reveal the answer
Yes — exactly 30. When the number of repetitions is known, a count-controlled loop is the right choice.
🧠 Key Concept 14 min
There are three loop structures. Each suits a different situation:
| Loop | Pseudocode | When the test happens | Runs at least once? |
|---|---|---|---|
| Count-controlled | FOR … TO … NEXT | Fixed number of iterations | Set in advance |
| Pre-condition | WHILE … ENDWHILE | Before each iteration | No — may run zero times |
| Post-condition | REPEAT … UNTIL | After each iteration | Yes — always at least once |
1 · Count-controlled: FOR … NEXT
Cambridge pseudocode
FOR Counter ← 1 TO 10 STEP 2 OUTPUT Counter NEXT Counter
The same in Python
for counter in range(1, 11, 2): # start 1, stop before 11, step 2 print(counter)
1 3 5 7 9
2 · Pre-condition: WHILE … ENDWHILE
Use this when you do not know how many repetitions are needed, and it is possible that none are needed at all.
WHILE TotalWeight < 100 DO INPUT Weight TotalWeight ← TotalWeight + Weight ENDWHILE
while total_weight < 100: weight = float(input("Weight: ")) total_weight = total_weight + weight
3 · Post-condition: REPEAT … UNTIL
Use this when the body must run at least once — for example, asking for input and repeating until it is valid.
REPEAT OUTPUT "Enter a PIN: " INPUT Pin UNTIL Pin = CorrectPin
# Python only has pre-condition loops, # so a post-condition loop is built like this: while True: pin = input("Enter a PIN: ") if pin == correct_pin: break
Worked Example 12 min
Example 1 · FOR loop — a times table
Output the first five rows of the 7 times table.
- The number of rows (5) is known in advance.so a count-controlled loop is the right choice.
- The loop counter is also used in the calculation.it changes by 1 each time, giving 1×7, 2×7, …
- In Python, the stop value is 6.
rangestops one before its second argument.
Cambridge pseudocode
DECLARE Counter, Answer : INTEGER FOR Counter ← 1 TO 5 Answer ← Counter * 7 OUTPUT Counter, " x 7 = ", Answer NEXT Counter
The same in Python
# The 7 times table, rows 1 to 5 for counter in range(1, 6): # 1, 2, 3, 4, 5 - stops BEFORE 6 answer = counter * 7 print(counter, "x 7 =", answer)
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
| Counter | Answer | OUTPUT |
|---|---|---|
| 1 | 7 | 1 x 7 = 7 |
| 2 | 14 | 2 x 7 = 14 |
| 3 | 21 | 3 x 7 = 21 |
| 4 | 28 | 4 x 7 = 28 |
| 5 | 35 | 5 x 7 = 35 |
Example 2 · WHILE and REPEAT — savings goal and a valid rating
(a) Sam saves $15 each week. How many weeks until the savings reach at least $60?
CONSTANT Goal ← 60 CONSTANT WeeklySaving ← 15 DECLARE Savings, Weeks : INTEGER Savings ← 0 Weeks ← 0 WHILE Savings < Goal DO Savings ← Savings + WeeklySaving Weeks ← Weeks + 1 ENDWHILE OUTPUT "Goal reached after ", Weeks, " weeks with $", Savings
# Pre-condition loop: save $15 a week until the goal is reached GOAL = 60 WEEKLY_SAVING = 15 savings = 0 weeks = 0 while savings < GOAL: savings = savings + WEEKLY_SAVING weeks = weeks + 1 print("Goal reached after", weeks, "weeks with $", savings)
Goal reached after 4 weeks with $ 60
| Savings | Weeks | Savings < 60? | OUTPUT |
|---|---|---|---|
| 0 | 0 | TRUE | |
| 15 | 1 | TRUE | |
| 30 | 2 | TRUE | |
| 45 | 3 | TRUE | |
| 60 | 4 | FALSE | Goal reached after 4 weeks with $60 |
Why WHILE? If Sam already had $80, the test 80 < 60 is FALSE at the start and the body is skipped: 0 weeks. A pre-condition loop handles "nothing to do" correctly.
(b) Ask for a film rating from 1 to 5, and keep asking until the rating is valid.
DECLARE Rating : INTEGER REPEAT OUTPUT "Rate the film from 1 to 5: " INPUT Rating UNTIL Rating >= 1 AND Rating <= 5 OUTPUT "Thank you for rating it ", Rating
# Post-condition loop in Python: always ask at least once while True: rating = int(input("Rate the film from 1 to 5: ")) if rating >= 1 and rating <= 5: break # the UNTIL condition is met print("Thank you for rating it", rating)
Rate the film from 1 to 5: 7 Rate the film from 1 to 5: 0 Rate the film from 1 to 5: 4 Thank you for rating it 4
- The question must be asked at least once.there is nothing to test until the user has typed something — so REPEAT.
- 7 fails
Rating <= 5, 0 failsRating >= 1: the loop repeats both times.the UNTIL condition is FALSE, so go round again. - 4 makes the condition TRUE, so the loop ends.this is a range check (Unit 7 validation) built from a loop.
Try It Yourself 12 min
Goal: Use a FOR loop to output a countdown from 10 to 1, then "Lift off!". (Hint: STEP -1; in Python range(10, 0, -1).)
Goal: Write two programs that each output 0, 3, 6, … for ten values — one with a count-controlled loop and one with a condition-controlled loop.
Goal: Rewrite this REPEAT loop as a WHILE loop that behaves the same way for every possible input.
REPEAT INPUT Password UNTIL LENGTH(Password) >= 8
Hint
A WHILE loop might run zero times, so input once before the loop as well as inside it. Then reverse the condition: keep looping while the length is under 8.
📝 Exam Practice 10 min
Describe the purpose of each statement in this algorithm.
FOR Index ← 1 TO 200 INPUT Score[Index] NEXT Index
Mark scheme
FOR … NEXTrepeats the input 200 times / is a count-controlled loop with Index from 1 to 200 (1).INPUT Score[Index]stores each value in the next position of the array Score (1).
Explain the difference between a pre-condition loop and a post-condition loop. Give the pseudocode keywords for each.
Mark scheme
- A pre-condition loop tests the condition before the loop body runs (1)…
- …so the body may not be executed at all (1).
- A post-condition loop tests the condition after the body runs, so the body is always executed at least once (1).
- Pre-condition:
WHILE … ENDWHILE; post-condition:REPEAT … UNTIL(1).
Write pseudocode to input a number between 0 and 100 inclusive. The algorithm must prompt for the input and output an error message and ask again if the number is outside this range.
Mark scheme
- Loop that repeats until the value is in range, e.g.
REPEAT … UNTIL Number >= 0 AND Number <= 100(1). - Prompt and input inside the loop (1).
- Error message output only when the number is out of range, e.g.
IF Number < 0 OR Number > 100 THEN OUTPUT "Error"(1).
Identify, using pseudocode, a different loop structure that could replace the FOR loop in the first question.
Mark scheme
WHILE … ENDWHILEorREPEAT … UNTILwith a counter that is set and incremented manually (1).
🗝️ Recap & Key Terms 3 min
Known number of repeats → FOR … NEXT. Unknown, and maybe none → WHILE … ENDWHILE. Unknown, but at least one → REPEAT … UNTIL. Python has for and while; it builds a post-condition loop with while True and break.
- Iteration
- A section of programming code that can be repeated under certain conditions.
- Count-controlled loop
- A loop that repeats a set number of times, e.g.
FOR … NEXT. - Pre-condition loop
- A condition-controlled loop that tests before each iteration, so it may not run at all, e.g.
WHILE … ENDWHILE. - Post-condition loop
- A condition-controlled loop that tests after each iteration, so it always runs at least once, e.g.
REPEAT … UNTIL.
Homework 1 min
Task (≤ 15 min): Complete a trace table for this algorithm and state the final output. [4]
X ← 1 Y ← 0 WHILE X < 20 DO Y ← Y + 1 X ← X * 3 ENDWHILE OUTPUT Y, " ", X
Model answer
| X | Y | OUTPUT |
|---|---|---|
| 1 | 0 | |
| 3 | 1 | |
| 9 | 2 | |
| 27 | 3 | 3 27 |
Marks: initial values (1); X column 3, 9, 27 (1); Y column 1, 2, 3 (1); output 3 27 (1).