🎯 Syllabus & Goals 3 min
Cambridge 7.2 · Design methods — pseudocode Cambridge 7.3 · Explain the purpose of an algorithm Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Write the three loops —
FOR…TO…NEXT,REPEAT…UNTILandWHILE…DO…ENDWHILE— and choose the right one. - Use
INPUTandOUTPUTcorrectly, including lists of values. - Explain the purpose of a given algorithm and list the processes in it.
Textbook: Chapter 7, §7.2.3 Pseudocode — iteration, input and output (pp. 268–270) and §7.3 (p. 271)
Recap / Warm-Up 5 min
Last lesson covered assignment and selection. The third building block of every algorithm is iteration — repeating steps.
Quick starter
After A ← 5, B ← A + 2, A ← B * 2, what are A and B?
Reveal the answer
B = 7, then A = 14. A's old value (5) is replaced.
🧠 Key Concept 14 min
1 · Iteration: three kinds of loop
Repeating actions is called iteration. Pseudocode has three loop structures. All three can do the same job, but each suits a different situation.
| Loop | When to use it | Condition tested | Runs at least once? |
|---|---|---|---|
FOR … TO … NEXT | the number of repeats is known | counter managed automatically | yes, if start ≤ end |
REPEAT … UNTIL | number of repeats not known | at the end (post-condition) | always |
WHILE … DO … ENDWHILE | number of repeats not known | at the start (pre-condition) | not necessarily — may run zero times |
REPEAT tests after the body, so the body always runs once. WHILE tests before, so the body may never run.FOR — five repeats, known in advance
FOR Counter ← 1 TO 5 OUTPUT "Lap ", Counter NEXT Counter
REPEAT — ask until valid
REPEAT OUTPUT "PIN? " INPUT Pin UNTIL Pin = 4096
WHILE — keep adding until the rogue value −1
Total ← 0 INPUT Mark WHILE Mark <> -1 DO Total ← Total + Mark INPUT Mark ENDWHILE
2 · Input and output
INPUT Name— data is entered and stored in the variableName.OUTPUT "Your name is ", Name— a list of values separated by commas is displayed.READmay replaceINPUTwhen reading a file;PRINTmay replaceOUTPUTfor a hard copy.
3 · Explaining the purpose of an algorithm
An exam may give you an algorithm with no explanation — often with single-letter names — and ask for its purpose. Answer in two parts: what it achieves, and the processes it uses to get there.
Worked Example 12 min
Worked example 1 · Summing with a rogue value (WHILE)
Trace the WHILE loop above with the inputs 12, 8, 5, −1.
Total ← 0 INPUT Mark WHILE Mark <> -1 DO Total ← Total + Mark INPUT Mark ENDWHILE OUTPUT "Total: ", Total
total = 0 mark = int(input()) while mark != -1: total = total + mark mark = int(input()) print("Total:", total)
| Mark | Total | Mark <> -1 ? | OUTPUT |
|---|---|---|---|
| 0 | |||
| 12 | TRUE | ||
| 12 | |||
| 8 | TRUE | ||
| 20 | |||
| 5 | TRUE | ||
| 25 | |||
| -1 | FALSE | Total: 25 |
- The first INPUT is before the loop. So the condition has a value to test.WHILE tests at the start; without a first input, Mark would have no value.
- Each pass adds then inputs again. 0 + 12 = 12; 12 + 8 = 20; 20 + 5 = 25.
- −1 is read, the test is FALSE, the loop ends. −1 is never added.that is why the second INPUT is the last line inside the loop.
- If −1 were the very first input, the loop body would run zero times and output Total: 0.
Worked example 2 · What is the purpose of this algorithm?
INPUT A B ← 1 FOR C ← 1 TO A B ← B * 2 NEXT C OUTPUT B
- Trace it with easy data, A = 4.
A B C OUTPUT 4 1 2 1 4 2 8 3 16 4 16 - Spot the pattern. B doubles A times: 1 → 2 → 4 → 8 → 16 = 24.one easy trace usually shows the pattern; a second (A = 3 → 8) confirms it.
- State the purpose. It inputs a whole number and outputs 2 raised to the power of that number.
- List the processes. Input a number; set B to 1; multiply B by 2 once per loop, A times; output the result.
a = int(input()) b = 1 for c in range(1, a + 1): b = b * 2 print(b)
4 16
Try It Yourself 12 min
Goal: Write a FOR loop that outputs the 7 times table from 7 × 1 to 7 × 12.
Goal: Rewrite the WHILE rogue-value algorithm as a REPEAT … UNTIL loop that gives the same total. Trace it with 12, 8, 5, −1 to prove it.
Goal: State the purpose of this algorithm and list its processes.
INPUT N F ← 1 WHILE N > 1 DO F ← F * N N ← N - 1 ENDWHILE OUTPUT F
Hint
Trace it with N = 4: F becomes 4, then 12, then 24. What is 4 × 3 × 2 × 1?
📝 Exam Practice 10 min
Describe the difference between a REPEAT … UNTIL loop and a WHILE … DO … ENDWHILE loop.
Mark scheme
- REPEAT tests its condition at the end / is post-condition, so it always runs at least once (1).
- WHILE tests its condition at the start / is pre-condition, so it may not run at all (1).
Rewrite this algorithm using a WHILE … DO … ENDWHILE loop instead of FOR. It must give the same output.
FOR Count ← 1 TO 10 OUTPUT Count * 3 NEXT Count
Mark scheme
Count ← 1before the loop (1)WHILE Count <= 10 DO(or< 11) (1)OUTPUT Count * 3inside the loop (1)Count ← Count + 1inside the loop, thenENDWHILE(1)
Explain the purpose of this algorithm and state its output when the inputs are 3, 9, 4, 0.
X ← 0
REPEAT
INPUT Y
IF Y > X
THEN
X ← Y
ENDIF
UNTIL Y = 0
OUTPUT XMark scheme
- Inputs numbers until 0 is entered (1).
- Finds / outputs the largest number entered (1).
- Output: 9 (1).
A program asks a user to enter a password until it is correct. Explain which loop structure is most suitable.
Mark scheme
- REPEAT … UNTIL (1)…
- …because the number of attempts is not known and the password must be entered at least once (1).
🗝️ Recap & Key Terms 3 min
Use FOR when the number of repeats is known, REPEAT when the body must run at least once, and WHILE when it might not run at all. To explain an algorithm, trace it, spot the pattern, then state its purpose and processes.
- Iteration
- Repeating a set of actions using a loop structure.
- Count-controlled loop
FOR … TO … NEXT: repeats a set number of times; the counter is managed automatically.- Post-condition loop
REPEAT … UNTIL: the condition is tested at the end, so it runs at least once.- Pre-condition loop
WHILE … DO … ENDWHILE: the condition is tested at the start, so it may never run.- Rogue value
- A value that is not real data, used to end a loop (e.g. −1).
- Purpose of an algorithm
- What the algorithm achieves, together with the processes it uses to achieve it.
Homework 1 min
Task (≤ 15 min): Write pseudocode that inputs positive numbers until the user enters 0, then outputs how many numbers were entered and their total. Explain why you chose your loop. [5]
Model answer
Count ← 0 Total ← 0 INPUT Number WHILE Number <> 0 DO Count ← Count + 1 Total ← Total + Number INPUT Number ENDWHILE OUTPUT "How many: ", Count OUTPUT "Total: ", Total
Marks: both variables initialised (1); input before and at the end of the loop (1); correct WHILE condition (1); count and total updated (1). Reason: WHILE, because the number of inputs is unknown and 0 might be the first input, so the body may run zero times (1).