AQA GCSE Computer Science June 2023 Paper 1, Question 2: nested WHILE loops and integer division
AQA 8525 June 2023 Paper 1, Question 2: a WHILE loop inside a WHILE loop that uses integer division. The three multiple choice answers worked through, with the algorithm as a Python program you can run.
Question 2 of the AQA GCSE Computer Science Paper 1 sat on 19 May 2023 (8525/1B, the Python paper) shows a pseudo-code algorithm with one WHILE loop inside another. Three multiple choice parts follow, one mark each. They look easy and they are easy to get wrong, because the question asks what is output, not what you expect to be output.
We do not copy the exam paper here. Open it beside this page: AQA June 2023 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.
The question in short
The algorithm keeps asking the user for a number, a.
- If
ais greater than 0, it setscounterto 0. Then, whileais still greater than 0, it replacesawitha DIV 3and adds 1 tocounter. - If
ais 0 or less, it sets a flag so that the outer loop will stop. - Either way, it then outputs
a.
DIV is integer division: divide and throw the remainder away. 14 DIV 5 is 2. In Python it is written //.
The three parts ask: where iteration is first used, what is output when the user enters 10, and the largest value counter reaches when the user enters 36.
Part 1: where is iteration first used?
Iteration means repeating, so look for the first loop. The outer WHILE on line 2 comes before the inner WHILE on line 6. The answer is line 2.
The trap is line 4. IF is selection, not iteration. It chooses, it does not repeat.
Part 2: what is output when the input is 10?
Trace the inner loop. Each pass divides a by 3 and drops the remainder.
| a | counter |
|---|---|
| 10 | 0 |
| 3 | 1 |
| 1 | 2 |
| 0 | 3 |
a is now 0, so the inner loop stops. The algorithm outputs a, which is 0.
The trap: the inner loop only ever stops when a reaches 0. So for any positive input, the output is 0. If you answered 1 or 2 you stopped dividing too soon. If you answered 3 you gave the value of counter, and the algorithm outputs a.
Part 3: the largest value of counter when the input is 36
| a | counter |
|---|---|
| 36 | 0 |
| 12 | 1 |
| 4 | 2 |
| 1 | 3 |
| 0 | 4 |
The answer is 4. Do not stop at a = 1. One is still greater than 0, so the loop runs once more: 1 DIV 3 is 0, and counter goes up to 4.
Run it
This is the same algorithm in Python. It prints a and counter on every pass of the inner loop, so you can check your trace. Type a number when it asks. Enter 0 to finish.
The program
from bugbot import *
connect()
again = True
while again == True:
a = int(input("Enter a number (0 to stop): "))
if a > 0:
counter = 0
while a > 0:
a = a // 3
counter = counter + 1
print(" a =", a, " counter =", counter)
else:
again = False
print("OUTPUT", a)
What is it really counting?
counter ends up as the number of times you can divide by 3 before nothing is left. That is the number of digits the number has in base 3. Try 26 and 27: 26 gives 3, and 27 gives 4, because 27 is the first number that needs four base 3 digits.
Questions
What are the answers to AQA GCSE Computer Science 2023 Paper 1 Question 2?
Part 1 is A, line number 2. Part 2 is A, the output is 0. Part 3 is C, the largest value of counter is 4.
What does DIV mean in AQA pseudo-code?
DIV is integer division. It gives the whole number of times one number goes into another and ignores the remainder, so 25 DIV 3 is 8. In Python the same operator is written //.
What is the difference between iteration and selection?
Selection chooses between paths once, with IF. Iteration repeats a block of code, with WHILE, FOR or REPEAT. A question that asks where iteration is first used wants the first loop, not the first IF.
More from this paper
- Question 4: Trace a Python function with three sets of inputs, then make it robust 4 marks
- Question 5: Trace a flowchart with a loop 3 marks
- Question 6: Fill the gaps in a login routine 4 marks
- Question 7: A ticket price with a group discount 6 marks
- Question 10: Trace nested FOR loops over a list of scores and find the error 6 marks
Every AQA 8525 question we have worked
Learn it step by step
- F2.6 Condition-controlled loops: while Decisions and loops
- F1.9 Arithmetic operators Programming basics
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.