CIE IGCSE | 8.1.4C.3 Post-Condition Loops: REPEAT UNTIL

Lesson objective

Trace post-condition repetition, distinguish true-to-stop from true-to-continue, and use REPEAT UNTIL for validated input and guarded sentinel processing.

Learn

8.1.4C.3 | POST-CONDITION LOOPS: REPEAT UNTIL

01 | SOMETIMES THE FIRST ACTION MUST HAPPEN

A program needs a score from the user. Before it can decide whether the score is acceptable, it must receive one. If the first response is invalid, it asks again.

A REPEAT UNTIL loop puts the repeated work first and the test afterwards. This is post-condition iteration: the condition is checked after the body has executed.

The body runs at least once whenever execution reaches REPEAT. That makes it useful for tasks such as asking for input before checking whether to stop asking.

The most important contrast with WHILE is the meaning of true: WHILE continues when its condition is true, while UNTIL stops when its condition is true.

02 | READ THE REPEAT UNTIL FORMAT

Cambridge pseudocode uses REPEAT to start the body and UNTIL followed by its stopping condition to close it.

DECLARE Score : INTEGER
REPEAT
    OUTPUT "Enter a whole-number score from 0 to 10"
    INPUT Score
UNTIL Score >= 0 AND Score <= 10
OUTPUT "Accepted score: ", Score

Assume every supplied input is a whole number. The program prompts and receives Score, then checks whether it lies inside the inclusive range.

If the condition is true, the loop ends. If it is false, the body runs again and receives another score.

There is no separate ENDREPEAT in this notation
PartRole
REPEATBegin the repeated body
OUTPUT and INPUTAsk for and receive the current value
Score >= 0 AND Score <= 10Boolean stopping condition
UNTILClose the body and test for completion
Final OUTPUTCommunicate the accepted score after the loop

Unlike WHILE, this pattern does not need a first Score input before the loop. The body supplies Score before its first test.

03 | TRUE MEANS STOP, FALSE MEANS REPEAT

Read UNTIL Score >= 0 AND Score <= 10 as “repeat until this score is valid”. The condition describes completion.

Inputs are twelve, minus one and eight in that order
Input attemptStopping conditionWhat happens
12FALSERepeat prompt and input
-1FALSERepeat prompt and input
8TRUELeave the loop and output accepted score

Both bounds must hold, so the valid-range condition uses AND. Twelve violates the upper bound and minus one violates the lower bound.

The final accepted output occurs once, after the successful check. Invalid attempts are not reported as accepted.

Do not mentally apply the WHILE rule to UNTIL. A true UNTIL is the reason to stop, not the reason to run another iteration.

04 | AT LEAST ONCE DOES NOT MEAN TWICE

If the first supplied score is five, the body runs once, then the condition is true and repetition ends. There is one prompt, one input and one stopping-condition check.

The phrase “at least once” describes the minimum, not a compulsory extra attempt. A successful first response does not get requested again.

If the first response is invalid, more iterations occur. The number of attempts is determined by the responses, so a fixed FOR count is not a natural replacement for this retry behaviour.

Always consider both a valid first response and an invalid-then-valid sequence when testing input retries.

05 | TRACE A COUNTER VERSION

A counter example makes the position of the check easy to see.

DECLARE Count : INTEGER
Count ← 1
REPEAT
    OUTPUT Count
    Count ← Count + 1
UNTIL Count > 3
OUTPUT "Finished"
Final Count is four; value outputs are one, two and three
IterationCount outputCount after updateCount > 3?
112FALSE
223FALSE
334TRUE

The condition uses the updated Count. The body outputs three, increases Count to four and only then tests the stopping condition.

There are three iterations and three UNTIL checks. The output Finished appears afterwards.

Compare the earlier WHILE example: its three body iterations required four checks because it tested once more before deciding not to enter the body.

06 | AN ALREADY-SATISFIED STOP CONDITION STILL FOLLOWS THE BODY

Start the same algorithm with Count equal to four. Count > 3 is already true mathematically, but REPEAT does not test it before entering the body.

The body first outputs four and increases Count to five. Then UNTIL Count > 3 is tested and the loop stops. There is one body iteration, not zero.

Both versions output then increment within their bodies
Start CountWHILE Count <= 3 versionREPEAT ... UNTIL Count > 3 version
1Outputs 1, 2, 3; ends with Count 4Outputs 1, 2, 3; ends with Count 4
4No body output; Count stays 4Outputs 4 once; Count becomes 5

Two loops can behave alike for one starting state and differently for another. Do not claim they are interchangeable after testing only the normal case.

07 | STEP THROUGH BODY THEN CHECK

Choose a starting Count and predict the next event. Try one, three and four to see multiple, single and initially beyond-limit starts.

:

CURRENT COUNT

1

BODY EXECUTIONS

0

Ready: the body executes before the first check.

    The trace begins with a body output, not a condition check. It then shows the update and the UNTIL result, followed by common code when the loop stops.

    08 | CONVERT THE CONDITION, NOT JUST THE KEYWORD

    A WHILE range retry continues while invalid. The REPEAT version stops when valid.

    Whole-number scores are permitted from zero to ten inclusive
    StructureRange conditionMeaning
    WHILEScore < 0 OR Score > 10Continue while outside the range
    REPEAT UNTILScore >= 0 AND Score <= 10Stop when inside the range

    Changing WHILE to UNTIL while keeping the invalidity condition would make the retry stop for invalid input. A score of twelve could immediately terminate the wrong REPEAT loop.

    The structure also changes the placement of the first input. WHILE needs a value before its first condition; REPEAT can obtain the value inside the first body execution.

    To translate correctly, identify the repeated job, its start state, its continuation or stopping rule, and the location of input and processing.

    09 | A POST-CONDITION BODY STILL NEEDS PROGRESS

    A REPEAT loop can be infinite if its stopping condition never becomes true.

    Count ← 1
    REPEAT
        OUTPUT Count
    UNTIL Count > 3

    Count remains one. The body outputs one, then checks a false stopping condition, and repeats forever under these instructions.

    Add the relevant update inside the body: Count ← Count + 1. With the correct update, later checks can eventually see Count greater than three.

    For a retry loop, the next INPUT provides new state. If you ask again but forget to receive a new value, the old invalid input remains and the loop cannot complete.

    A post-condition check does not magically ensure termination. Explain what changes and why the stopping condition can become true.

    10 | SENTINEL PROCESSING NEEDS A GUARD

    In the WHILE sentinel example, a test before the body excluded minus one from processing. In REPEAT, input and processing happen before UNTIL, so you must guard processing inside the body.

    DECLARE Amount : INTEGER
    DECLARE Total : INTEGER
    Total ← 0
    REPEAT
        OUTPUT "Enter an amount, or -1 to stop"
        INPUT Amount
        IF Amount <> -1
          THEN
            Total ← Total + Amount
        ENDIF
    UNTIL Amount = -1
    OUTPUT "Total: ", Total

    Assume each supplied value is a non-negative whole number or the sentinel minus one. INPUT receives the value, IF decides whether it is an amount to add, and UNTIL decides whether another iteration is needed.

    The stop value reaches the body, but the guard prevents it contributing to Total. Simply placing UNTIL after an unconditional addition would be too late to exclude it.

    11 | DISTINGUISH BODY ITERATIONS FROM ITEMS PROCESSED

    Four body iterations receive three amounts plus the stop value
    Input receivedAdded?Total after guardAmount = -1?
    4Yes4FALSE
    0Yes4FALSE
    7Yes11FALSE
    -1No11TRUE

    The REPEAT body runs four times because each pass receives an input, including the sentinel pass. Only three amounts are processed. That differs from the primed WHILE pattern, whose sentinel is tested before another body entry.

    If minus one is the first input, the REPEAT body still executes once, but it processes zero amounts and outputs a total of zero.

    A valid zero amount changes no sum but is still an item. If the task also counts entries, increment the count inside the same non-sentinel guard as the addition.

    12 | SELECT THE RIGHT WORK TO KEEP INSIDE

    In validation, receive attempts inside the loop and use the accepted value afterwards. For example, add an accepted class attendance to a total after its range retry has finished, not on every invalid attempt.

    In a sentinel-controlled REPEAT, receive the current value inside the body, process it only if it is not the sentinel, then test the stop condition.

    The keyword alone does not determine correctness. The order and placement of input, checks and processing determine which values affect the result.

    Common final results normally belong after UNTIL so they display once after completion, rather than after every attempt.

    13 | TEST THE FIRST ATTEMPT AND THE STOPPING BOUNDARY

    For a zero-to-ten score retry, test zero and ten as accepted endpoints. Test minus one followed by zero, and eleven followed by ten, to check rejection and replacement.

    For the counter version, starts one, three and four reveal the body-before-check behaviour. Start three outputs three once, updates to four and stops. Start four also runs once but outputs four.

    For sentinel processing, test several values followed by the sentinel and the sentinel as the first response. Check both the number of received inputs and the items actually added or counted.

    These tests can expose a wrong condition sense, a missing update or an unconditional addition of the sentinel.

    14 | COMMON REPEAT UNTIL MISTAKES

    True treated as continue: UNTIL stops when true.

    Initial test imagined: the first body runs before any UNTIL evaluation.

    WHILE condition copied unchanged: a continuation condition usually needs a corresponding stopping condition and adjusted structure.

    Sentinel added before its test: use a guard to exclude it from processing.

    Attempts mistaken for accepted items: a body can receive a rejected or sentinel value without processing an ordinary item.

    Input or progress missing: the state may never change to satisfy completion.

    Extra closing keyword: UNTIL closes the Cambridge REPEAT structure; do not add ENDREPEAT.

    15 | EXPLAIN BODY, CHECK AND CONSEQUENCE

    For range retry: “The body receives a score before testing it. If the valid-range condition is false, another input is requested; when it is true, the loop ends and the accepted score is output.”

    For sentinel processing: “The IF guard excludes minus one from the sum because the body receives that value before the stopping check. UNTIL then detects the sentinel and prevents another iteration.”

    Explain the condition’s sense as well as its position. Both are needed to distinguish pre-condition and post-condition repetition.

    The next lesson brings FOR, WHILE and REPEAT UNTIL together so you can choose and compare them for different scenarios.

    MATCH THE REPEAT COMPONENT TO ITS ROLE

    Terminology

    Terminology

    REPEAT UNTIL

    A loop executing its body before testing whether to stop.

    Post-condition

    A test evaluated after the body has executed.

    Stopping condition

    The Boolean condition whose true result ends the repetition.

    REPEAT

    The Cambridge keyword introducing the repeated body.

    UNTIL

    Closes REPEAT and states its stopping condition.

    Loop body

    Instructions executed before the stopping test.

    Iteration

    One execution of the repeated body.

    Minimum one iteration

    The body executes at least once when control reaches REPEAT.

    Validation retry

    Receiving replacements until input meets the specified rules.

    Accepted input

    A supplied value satisfying the applied checks.

    Progress update

    A state change that affects a later condition test.

    Infinite loop

    Repetition that cannot terminate under the stated instructions.

    Sentinel

    A special value marking the end of data rather than an ordinary item.

    Processing guard

    A condition controlling whether a received value is processed.

    Termination

    Leaving the loop when its stopping condition becomes true.

    Pre-condition

    A test before body execution, as in WHILE.

    Continuation condition

    A true-to-continue condition, such as a WHILE condition.

    Body versus item count

    The distinction between executed loop passes and ordinary items actually processed.

    Questions

    Questions

    CHECK YOUR UNDERSTANDING

    Select all correct choices. Each exact set earns one point.

    1. Which statements describe REPEAT UNTIL?
    2. The first score is valid in a range retry. How many input-body iterations are needed?
    3. Which condition stops a zero-to-ten whole-number retry on valid input?
    4. Count starts at one; body outputs Count then increments it; UNTIL Count > 3. Which are correct?
    5. The same REPEAT starts at Count four. What happens?
    6. Why guard Amount <> -1 before adding inside a sentinel REPEAT?
    7. Inputs four, zero, seven, minus one are received inside the guarded sentinel REPEAT. Which are correct?
    8. Which statements about termination are correct?

    EXPLAIN AND COMPARE

    Write your answers before revealing each sample.

    1. Define post-condition iteration and state when REPEAT UNTIL stops.

    2. Explain why the first valid score causes exactly one input-body iteration.

    3. Trace Count initially one with body OUTPUT Count, Count ← Count + 1, UNTIL Count > 3.

    4. Compare that REPEAT with WHILE Count <= 3 when both start at four and their bodies output then increment.

    5. Explain why keeping an invalidity condition when changing WHILE to UNTIL can be wrong.

    6. Explain why a REPEAT sentinel loop needs a guard before accumulation.

    7. Calculate total, body iterations and ordinary item count for inputs four, zero, seven, minus one in the guarded REPEAT.

    8. Suggest tests exposing first-iteration and boundary behaviour for a score retry from zero to ten.

    BUILD IT | ACCEPT A SCORE, THEN CLASSIFY

    Receive a whole-number score from zero to twenty inclusive, asking again until it is valid. Then output Pass if it is at least ten, otherwise Not yet passed. Assume whole-number inputs. Write a complete REPEAT UNTIL algorithm and trace twenty-one followed by ten.

    Reveal one possible solution
    DECLARE Score : INTEGER
    REPEAT
        OUTPUT "Enter a whole-number score from 0 to 20"
        INPUT Score
    UNTIL Score >= 0 AND Score <= 20
    IF Score >= 10
      THEN
        OUTPUT "Pass"
      ELSE
        OUTPUT "Not yet passed"
    ENDIF

    Twenty-one makes the stopping condition false and is retried. Ten makes it true, then the classification condition is also true and Pass is output.

    Test: zero and twenty should be accepted on the first input. Nine should classify as Not yet passed; ten should classify as Pass. There are two separate boundaries: the acceptance range and the later pass threshold.

    Flashcards

    Flashcards

    Click to flip. Select the ideas you need to revisit.

    0 cards selected for revision.

      Selections are kept while this page is open.

      Workbook

      Workbook

      COMING SOON

      The workbook for 8.1.4C.3 Post-Condition Loops: REPEAT UNTIL is coming soon. Complete the matching task and written questions in your exercise book.