Worked solution from the Algorithm design and problem-solving Paper 2 Algorithms, Programming and Logic booklet, Part 4 of 8.
Pick one check for each rule. Write the check name and what it does.
Length check [1]
It makes sure the product code entered is 6 characters long. [1]
Format check [1]
It makes sure the first two characters of the product code are "PD". [1]
Range check [1]
It makes sure the number made by the last four characters is between 1000 and 9999. [1]
Use the LENGTH function and keep asking for input until the length is 6.
REPEAT
INPUT Product
UNTIL LENGTH(Product) = 6Correct use of LENGTH on Product [1]
Correct test that the result equals 6, so the loop stops only for a valid code [1]
Other correct loops (for example WHILE with an error message) also score.
Use SUBSTRING to take the first two characters, starting at position 1, and compare them with "PD".
REPEAT
INPUT Product
UNTIL SUBSTRING(Product, 1, 2) = "PD"Correct use of SUBSTRING (start 1, length 2) [1]
Correct test that it equals "PD" [1]
▶ Topic lesson
A lesson on this topic · by Mr Bulmer's Learning Zone. 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.