Learning Objectives
  • Write and trace REPEAT UNTIL loops.
  • Explain why the body executes at least once.
  • Distinguish the UNTIL condition from a WHILE condition.
  • Use post-condition iteration for validation and menus.
Key Terms
Post-condition loop
A loop that tests its condition after executing the body.
REPEAT UNTIL
A loop that repeats its body until a Boolean condition becomes TRUE.
Exit condition
The condition that must become true for a REPEAT loop to stop.
Guaranteed first iteration
The body executes once before any test.
Summary diagram
Summary Of The Main Ideas In This Lesson
How REPEAT UNTIL Works

A REPEAT UNTIL loop executes the body first and tests the condition afterwards. Therefore the body always executes at least once.

The loop stops when the UNTIL condition becomes true. This is the opposite wording from WHILE, which continues while its condition is true.

REPEAT is useful when one execution is always required, such as displaying a menu once or asking for input before it can be validated.

Cambridge Syntax

There is no separate end keyword because UNTIL closes the structure. The condition describes the state that ends the loop.

REPEAT
  Statements
UNTIL Condition
Validation Example

The body asks for an age and then tests validity. If the age is invalid, the condition is false and the loop repeats. If valid, the condition is true and the loop ends.

Notice that the valid condition uses AND. In a WHILE validation loop, the repeated condition would usually be the invalid condition Age < 0 OR Age > 120.

REPEAT
  OUTPUT "Enter age from 0 to 120"
  INPUT Age
UNTIL Age >= 0 AND Age <= 120
Menu Example

A menu often needs to be displayed at least once and repeated until the user chooses Exit. Place the display, input and action selection inside REPEAT, and use UNTIL Choice = ExitChoice.

If an action should occur for every non-exit choice, ensure the exit value is handled correctly and not treated as an ordinary action.

WHILE And REPEAT Compared
Feature WHILE REPEAT UNTIL
Test position Before body After body
Minimum executions Zero One
Condition meaning Continue while true Stop when true
Typical use May not need to repeat Must perform body once
Termination

The body must change data involved in the exit condition. If Choice is never input again, UNTIL Choice = 0 may never become true.

A REPEAT loop can also be infinite when the exit condition is impossible. Trace the values and check that at least one path can make the condition true.

Worked Examples
Positive Number

Question: Input repeatedly until Number is greater than zero.

  1. Place prompt and input inside REPEAT.
  2. Use the valid exit condition Number > 0.

Answer: REPEAT OUTPUT “Enter positive number”; INPUT Number; UNTIL Number > 0.

Converting WHILE To REPEAT

Question: A WHILE repeats while Choice <> 4. What is the equivalent REPEAT exit condition?

  1. WHILE uses a continuation condition.
  2. REPEAT uses the state that stops repetition.
  3. Reverse Choice <> 4 to Choice = 4.

Answer: UNTIL Choice = 4.

Examination Guidance
  • State that the body executes at least once.
  • Write the condition that ends the loop, not the condition that continues it.
  • Use a valid condition after input validation.
  • Update values used by the UNTIL condition.
  • Compare with WHILE when asked to justify loop choice.
Common Mistakes
  • Writing UNTIL with the invalid condition in a validation loop.
  • Adding ENDREPEAT when it is not part of the Cambridge form.
  • Assuming the body can be skipped.
  • Forgetting to input the control value in the body.
  • Using a condition that can never be true.
Knowledge Check

1. When is the REPEAT condition tested?

Answer: After the body.

2. What is the minimum number of executions?

Answer: One.

3. When does the loop stop?

Answer: When the UNTIL condition is true.

4. Which loop is preferable when input must be requested at least once?

Answer: REPEAT UNTIL.

5. What valid condition ends age validation from 0 to 120?

Answer: Age >= 0 AND Age <= 120.