Some programming languages, such as C#, have a DO ... WHILE iteration structure. DO ... WHILE is not part of AQA pseudo-code, but it has been added to the pseudo-code in the algorithm below.
secret ← 7
DO
guess ← USERINPUT
WHILE guess ≠ secret
OUTPUT 'Correct'
Give one similarity and one difference between a DO ... WHILE loop and a REPEAT ... UNTIL loop, and rewrite the algorithm using REPEAT ... UNTIL so that it works in the same way.
Model answer
Similarity: both check their condition at the end, so the body always runs at least once.
Difference: DO ... WHILE keeps repeating while its condition is True, whereas REPEAT ... UNTIL keeps repeating until its condition becomes True, so the two conditions are opposites.
secret ← 7
REPEAT
guess ← USERINPUT
UNTIL guess = secret
OUTPUT 'Correct'
Explanation
Both structures are post-condition loops: the user is always asked for at least one guess before any test is made. DO ... WHILE continues while guess ≠ secret is True, so the equivalent REPEAT ... UNTIL must stop when the opposite, guess = secret, becomes True. Keeping ≠ in the UNTIL condition is the common mistake: that loop would stop after the first wrong guess.