Question 1
2 marksLook at this pseudocode:
Count ← 0
FOR Row ← 1 TO 5
FOR Column ← 1 TO Row
Count ← Count + 1
NEXT Column
NEXT Row
OUTPUT CountState the output, and explain why it is not 25.
Model answer
15. The inner loop runs Row times, so it runs once, then twice, then 3, 4 and 5 times: 1 + 2 + 3 + 4 + 5 = 15.
Explanation
An inner loop can use the outer loop's counter in its own start or end value. Here the inner loop grows by one pass each time round the outer loop, so the total is a sum, not 5 × 5.
Mark scheme
-
1 mark
15
-
1 mark
The end value of the inner loop is Row, so the inner loop runs a different number of times on each pass of the outer loop (1 + 2 + 3 + 4 + 5), not 5 times every pass