AQA GCSE Computer Science June 2022 Paper 1, Question 10: the REPEAT UNTIL trace table
AQA 8525 June 2022 Paper 1, Question 10: trace the subroutine call calculate(50) through a REPEAT UNTIL loop with DIV, then compare REPEAT UNTIL with WHILE. The full table, the marks explained, and both loops to run side by side.
Question 10 of the AQA GCSE Computer Science June 2022 Paper 1 (8525/1B, the Python paper) is built round a small subroutine with a REPEAT ... UNTIL loop. You trace it (4 marks), say what it outputs for a tricky input (1 mark), suggest a better variable name (1 mark), and compare it with a WHILE version (2 marks).
We do not copy the exam paper here. Open it beside this page: AQA June 2022 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.
The question in short
The subroutine calculate(n) copies n into a and sets b to 0. Then it repeats two steps: halve a with integer division (a DIV 2), and add 1 to b. It carries on until a is 1 or less, then outputs b.
Part 1: trace calculate(50)
Halve, count, then check. Remember that DIV throws away the remainder: 25 DIV 2 is 12, not 12.5.
| n | a | b | OUTPUT |
|---|---|---|---|
| 50 | 50 | 0 | |
| 25 | 1 | ||
| 12 | 2 | ||
| 6 | 3 | ||
| 3 | 4 | ||
| 1 | 5 | 5 |
After the fifth pass a is 1. The condition a ≤ 1 is now true, so the loop ends and the output is 5.
The four marks are for: the first values of a and b (50 and 0); the a column halving correctly down to 1 and no further; six values in the b column going up in ones, matching the a column; and the output being the last value of b, with nothing else in the n and OUTPUT columns.
Part 2: what does calculate(1) output?
It is tempting to say 0, because a is already 1 or less. But a REPEAT ... UNTIL loop checks its condition at the end. So the body runs once before anything is checked: a becomes 1 DIV 2 = 0, and b becomes 1. Then the check passes and the output is 1.
Part 3: a better name for b
b counts how many times the loop has run. count says so. counter, total and times get the mark too. Any name that describes the job does.
Part 4: REPEAT UNTIL against WHILE
The paper shows the same subroutine written with WHILE a > 1, and gives you one difference to start you off. Two more, for a mark each:
REPEAT ... UNTILtests its condition at the end of the loop.WHILEtests at the start.- So
REPEAT ... UNTILalways runs its body at least once. AWHILEloop may not run at all. - You can also say what that means here: when
nis 1 or less, theREPEATversion changesaandb, and theWHILEversion does not. They output 1 and 0.
Where the marks are lost
- 12.5 in the
acolumn.DIVis integer division. - Carrying on to 0. The loop stops as soon as
ais 1. A row witha= 0 loses a mark. - Writing 50 all the way down the
ncolumn.nnever changes, so it is written once. - Repeating the paper's own difference in Part 4. "One repeats until true and the other until false" is given to you in the question, and the mark scheme rejects it.
Run it
Python has no REPEAT ... UNTIL. The usual way to write one is while True with a break at the bottom. This program runs both versions for the same N. The robot drives forward 5 cm on every pass of the first loop, so you can count the passes.
The program
from bugbot import *
connect()
# change N and press Run
N = 50
# REPEAT ... UNTIL: the check is at the bottom
a = N
b = 0
print("a b")
print(a, b)
while True:
a = a // 2
b = b + 1
print(a, b)
forward(60, distance=5)
if a <= 1:
break
print("REPEAT version outputs", b)
# WHILE: the check is at the top
a = N
b = 0
while a > 1:
a = a // 2
b = b + 1
print("WHILE version outputs", b)
What is it for?
b ends up as the number of times you can halve n before you reach 1. That is the number of steps a binary search needs for a list of n items. For a million items the answer is only 19.
Questions
What is the answer to AQA GCSE Computer Science 2022 Paper 1 Question 10.1?
The a column is 50, 25, 12, 6, 3, 1. The b column is 0, 1, 2, 3, 4, 5. The output is 5.
What does calculate(1) output in Question 10.2?
- A REPEAT UNTIL loop always runs once before its condition is checked, so b goes up to 1 before the loop ends.
What is the difference between REPEAT UNTIL and WHILE?
REPEAT UNTIL checks its condition at the end, so the loop body always runs at least once, and it stops when the condition becomes true. WHILE checks at the start, so the body may never run, and it stops when the condition becomes false.
How do you write REPEAT UNTIL in Python?
Python has no REPEAT UNTIL. Use while True, put the loop body inside, and end it with an if that tests the UNTIL condition and breaks.
More from this paper
Every AQA 8525 question we have worked · Guide: Trace tables explained
Learn it step by step
- F13.2 Trace tables Exam preparation
- F2.6 Condition-controlled loops: while Decisions and loops
- F5.4 Trace tables Algorithms
This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.