Back to Topics/Loops & Iteration/REPEAT...UNTIL
Page 10.xLoops & Iteration

REPEAT...UNTIL Loop

Master the post-condition loop that runs at least once. Learn the CIE syntax, trace tables, when to choose REPEAT over WHILE, and apply it to input validation, menus, and sentinel-controlled sums.

6
Sections
30
KC Items
12
Questions
Loops
Category

10.1 What is REPEAT...UNTIL?

REPEAT...UNTIL is a post-condition loop — the body of the loop runs first, and the condition is checked afterwards. This means the loop body is always executed at least once, even if the condition is already TRUE when the program reaches the loop.

  • 1
    Post-condition: The condition is tested after each pass through the body, not before.
  • 2
    Runs at least once: Because the test happens at the end, the body is guaranteed to execute one or more times.
  • 3
    Exit condition: The loop stops when the condition becomes TRUE (opposite of WHILE, which stops when FALSE).
  • 4
    Manual update required: You must update the loop control variable inside the body — otherwise the loop runs forever (infinite loop).

Key Idea

Think of REPEAT...UNTIL as "do this task, then check if you can stop." The checking happens at the end of each iteration.

Common Use

REPEAT...UNTIL is perfect for input validation and menu systems — situations where the user must be prompted at least once before you can check their response.

10.2 Syntax

The CIE pseudocode syntax for REPEAT...UNTIL is straightforward. The body sits between the REPEAT keyword and the UNTIL keyword, with the condition appearing on the same line as UNTIL.

General Form
REPEAT
  <statements to repeat>
UNTIL <condition>
Example: Count from 1 to 5
DECLARE Counter : INTEGER
Counter <- 1
REPEAT
  OUTPUT Counter
  Counter <- Counter + 1
UNTIL Counter > 5

REPEAT Keyword

Marks the start of the loop body. No condition is written here.

UNTIL Keyword

Marks the end of the loop body. The exit condition follows on the same line.

Watch Out

Unlike WHILE...DO...ENDWHILE, REPEAT...UNTIL has no closing ENDREPEAT keyword — the UNTIL line itself closes the loop. Forgetting this is a common exam mistake.

The condition can be any expression that produces a BOOLEAN result (TRUE or FALSE), such as Counter > 5, Password = "open", or (Age >= 18) AND (HasID = TRUE).

10.3 Tracing

A trace table is the most reliable way to follow a loop's behaviour. Each row records the state of the variables after one complete iteration of the body, plus the result of evaluating the UNTIL condition.

Example to Trace
DECLARE Counter : INTEGER
DECLARE Total : INTEGER
Counter <- 1
Total <- 0
REPEAT
  Total <- Total + Counter
  Counter <- Counter + 1
UNTIL Counter > 4
OUTPUT Total

Trace table:

IterationCounter (before)Total (after)Counter (after)Counter > 4?
1112FALSE — loop again
2233FALSE — loop again
3364FALSE — loop again
44105TRUE — exit loop

The loop exits when Counter becomes 5 (which is > 4). The final value of Total is 10 (1 + 2 + 3 + 4), which is what the OUTPUT prints.

Tracing Rule

In REPEAT...UNTIL, the condition is tested after the body. So even if the condition would be TRUE at the start, the body still runs once before any check.

Subtle Case — Body Runs Once Even When Condition Starts TRUE
DECLARE X : INTEGER
X <- 100
REPEAT
  OUTPUT "X is ", X
  X <- X + 1
UNTIL X > 50
OUTPUT "Done"

Here X starts at 100, which is already > 50. But because REPEAT runs the body first, "X is 100" is still printed once before the loop exits. The program then prints "Done".

10.4 REPEAT...UNTIL vs WHILE

Both REPEAT...UNTIL and WHILE are indefinite loops — the number of iterations is not known in advance. But they differ in two crucial ways: whenthe condition is checked, and what value ends the loop.

FeatureREPEAT...UNTILWHILE...DO...ENDWHILE
Condition checkedAfter body (post-test)Before body (pre-test)
Body runs at least onceYes — alwaysNo — may be skipped entirely
Loop continues while condition isFALSETRUE
Loop exits when condition isTRUEFALSE
Best forInput validation, menusSentinel-controlled sums, reading data
Closing keywordUNTIL (with condition)ENDWHILE
Same Logic — Two Different Loops
// REPEAT...UNTIL version (body always runs once)
DECLARE Age : INTEGER
REPEAT
  OUTPUT "Enter age (18+): "
  INPUT Age
UNTIL Age >= 18

// WHILE version (must initialise Age first, body may not run)
DECLARE Age : INTEGER
Age <- 0
WHILE Age < 18 DO
  OUTPUT "Enter age (18+): "
  INPUT Age
ENDWHILE

Key Distinction

The UNTIL condition is the EXIT condition (loop stops when TRUE). The WHILE condition is the CONTINUE condition (loop stops when FALSE). Getting this backwards is the #1 student error.

When to Choose REPEAT

Pick REPEAT...UNTIL whenever you must ask the user (or read a value) at least once before you can evaluate the condition — for example password prompts, "press Y to continue" menus, or "enter a positive number" validation.

10.5 Practical Examples

REPEAT...UNTIL shines in three classic scenarios: input validation, menu systems, and sentinel-controlled accumulation. Let's look at a worked example of each.

Example 1 — Input Validation

Force the user to enter a positive number
DECLARE Num : INTEGER
REPEAT
  OUTPUT "Enter a positive number: "
  INPUT Num
UNTIL Num > 0
OUTPUT "Thanks! You entered ", Num

The prompt is shown at least once. If the user enters 0 or a negative value, the condition Num > 0 is FALSE so the loop repeats — asking again.

Example 2 — Sentinel-Controlled Sum

Add numbers until the user enters -1
DECLARE Value : INTEGER
DECLARE Total : INTEGER
Total <- 0
REPEAT
  OUTPUT "Enter a value (-1 to stop): "
  INPUT Value
  IF Value <> -1 THEN
    Total <- Total + Value
  ENDIF
UNTIL Value = -1
OUTPUT "Total is ", Total

The sentinel value -1 is not added to the total — the IF inside the loop guards against that. The loop exits when Value = -1 becomes TRUE.

Example 3 — Simple Menu

Menu that keeps showing until user picks Quit
DECLARE Choice : INTEGER
REPEAT
  OUTPUT "1. Play"
  OUTPUT "2. Settings"
  OUTPUT "3. Quit"
  INPUT Choice
  CASE OF Choice
    1 : OUTPUT "Starting game..."
    2 : OUTPUT "Opening settings..."
    3 : OUTPUT "Goodbye!"
    OTHERWISE OUTPUT "Invalid choice"
  ENDCASE
UNTIL Choice = 3

The menu always displays at least once. It only stops when the user picks option 3 (Quit), which sets Choice = 3 to TRUE.

Update Inside

In every example above, the loop control variable (Num, Value, Choice) is updated inside the body via INPUT. If you forget to update it, the loop runs forever — an infinite loop.

10.6 Key Points Summary

Here is a quick recap of everything we've covered on REPEAT...UNTIL:

  • Post-condition loopThe condition is checked AFTER the body executes.
  • Runs at least onceThe body is always executed one or more times.
  • Exit when TRUEThe loop stops when the UNTIL condition becomes TRUE.
  • No ENDREPEATUNTIL <condition> closes the loop — there is no ENDREPEAT keyword.
  • Manual updateYou must update the loop control variable inside the body.
  • Best for validationIdeal for input validation and menus — anywhere a prompt must appear at least once.
  • Opposite of WHILEWHILE continues on TRUE; REPEAT continues on FALSE. Pick the loop that matches your logic.

Exam Tip

When a CIE question says "the user must be prompted at least once" or "validate the input", reach for REPEAT...UNTIL. When it says "while the value is not equal to X, keep reading", reach for WHILE. Match the keyword to the English phrasing in the question.

Question Bank

Test your understanding — 12 questions on REPEAT...UNTIL

Q1Multiple Choice

When is the condition evaluated in a REPEAT...UNTIL loop?

Q2Multiple Choice

What is the minimum number of times a REPEAT...UNTIL loop body will execute?

Q3True / False

A REPEAT...UNTIL loop stops when its condition becomes TRUE.

Q4Multiple Choice

Look at this code. What is the final OUTPUT? DECLARE C : INTEGER C <- 0 REPEAT C <- C + 3 UNTIL C >= 10 OUTPUT C

Q5True / False

REPEAT...UNTIL and WHILE...DO...ENDWHILE both check the condition at the start of each iteration.

Q6Multiple Choice

Which is the correct CIE pseudocode syntax for a REPEAT...UNTIL loop?

Q7Multiple Choice

You need to write a password prompt that must be shown at least once. Which loop is MOST suitable?

Q8True / False

If the loop control variable is never updated inside the body, REPEAT...UNTIL becomes an infinite loop.

Q9Multiple Choice

Trace this loop. How many times does the body execute? DECLARE X : INTEGER X <- 5 REPEAT X <- X - 1 UNTIL X <= 0

Q10True / False

REPEAT...UNTIL is more appropriate than WHILE when you know in advance how many times to iterate.

Q11Multiple Choice

Look at this code. What is OUTPUT? DECLARE N : INTEGER N <- 10 REPEAT OUTPUT N N <- N - 3 UNTIL N < 0

Q12Multiple Choice

Which loop will NOT execute its body at all if the condition is initially FALSE?

Answered 0 of 12 questions