Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.
Count the values that end in 7 while looping from 100 to 200. A number ends in 7 when it leaves remainder 7 after dividing by 10.
DECLARE ThisInt, Count : INTEGER
Count ← 0
FOR ThisInt ← 100 TO 200
IF ThisInt MOD 10 = 7 THEN
OUTPUT ThisInt
Count ← Count + 1
ENDIF
NEXT ThisInt
OUTPUT CountDeclare the loop variable and the counter as INTEGER, and set the counter to 0 [1]
A loop from 100 to 200 with no step [1]
Test each value inside the loop (MOD 10 = 7) [1]
Output the chosen value and add 1 to the counter inside the loop [1]
Output the counter after the loop [1]
Another correct way: loop ThisInt from 107 TO 197 STEP 10 and output and count each value.
Use nested IF statements, one test for each case, and ELSE for OTHERWISE.
IF MySwitch = 1 THEN
ThisChar ← 'a'
ELSE
IF MySwitch = 2 THEN
ThisChar ← 'y'
ELSE
IF MySwitch = 3 THEN
ThisChar ← '7'
ELSE
ThisChar ← '*'
ENDIF
ENDIF
ENDIFA test of MySwitch against 1, 2 or 3 [1]
All three comparisons with their matching assignments [1]
The default '*' given by the last ELSE (or set before the tests) [1]
Completely correct IF ... THEN ... ELSE ... ENDIF syntax [1]
▶ Whole paper
Video solution of the whole paper · by CS BY AQIB KHAN. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.