A post-tested loop — Pseudocode Aptitude Question
A post-tested loop: A Repeat ... Until loop tests its condition after running the body. What is printed? Answer and worked solution on CodeKairo.
- Topic: Pseudocode
- Section: Programming MCQs
- Difficulty: Medium
- Time target: 75 seconds
Question
A Repeat ... Until loop tests its condition after running the body. What is printed?
Integer x, c
Set x = 3
Set c = 0
Repeat
Set x = x - 7
Set c = c + 1
Until x < 5
Print cOptions
- 0
- 1
- 2
- 3
Show the answer and the worked solution
Correct answer: B. 1
Worked solution
- x starts at 3, so the exit condition x < 5 is already true — but the test comes after the body, not before.
- The body therefore runs once: x = 3 - 7 = -4 and c = 1.
- The test now finds -4 < 5, so the loop ends and 1 is printed.
Answer: 1.
The idea
A post-tested loop executes its body at least once whatever the condition says. A pre-tested While with the same condition would have printed 0, and that contrast is the whole point of the question.
Shortcut. Rule out 0 first, since a post-tested loop cannot print it; one subtraction then takes x to −4, already below 5, so the count stops at 1.
Common trap. 0 treats the loop as pre-tested: x = 3 already satisfies x < 5, but the condition is checked only after the body has run.