Unable to open this exercise
Common exam pitfall
Students often think REPEAT UNTIL loops can execute zero times if the condition is already met, confusing them with WHILE loops.
In computer programming, when an action must occur before a condition can be tested (such as prompting a user for input), developers use a Post-Condition Loop (REPEAT ... UNTIL).
1. Syntax of REPEAT ... UNTIL
REPEAT
// Statements executed at least once
UNTIL <TerminationCondition>
The Two Critical Behaviors:
- Guaranteed Execution: Because the condition is located at the bottom of the structure, the loop body is guaranteed to run at least once.
- Termination Logic: The loop continues executing as long as the condition is
FALSE. The moment the condition evaluates toTRUE, the loop terminates immediately.
2. Crucial Comparison: REPEAT ... UNTIL vs WHILE ... DO
Feature |
|
|
|---|---|---|
Condition Location | Evaluated at the bottom after loop body. | Evaluated at the top before loop body. |
Minimum Iterations | 1 (Always runs at least once). | 0 (May never run if condition is initially false). |
Exit Condition | Loops while | Loops while |
Typical Use Case | User input validation, menu prompts, retry limits. | Searching arrays, reading file data until EOF. |
AI Socratic Tutor
Need guidance? Ask a question or use the hint below.
AI Socratic Tutor is an All-Access Benefit
Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.
You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.
Exam-Style Check: Guaranteed Minimum Iteration in Post-Condition Loops
• Multiple ChoiceHow many times is the body of a REPEAT ... UNTIL loop guaranteed to execute under all possible circumstances?
Interactive exercises for Module 2 and beyond require an active plan.