Post-Condition Loops (REPEAT ... UNTIL)

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:

  1. Guaranteed Execution: Because the condition is located at the bottom of the structure, the loop body is guaranteed to run at least once.
  2. Termination Logic: The loop continues executing as long as the condition is FALSE. The moment the condition evaluates to TRUE, the loop terminates immediately.

2. Crucial Comparison: REPEAT ... UNTIL vs WHILE ... DO

Feature

REPEAT ... UNTIL (Post-Condition)

WHILE ... DO (Pre-Condition)

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 FALSE; exits when TRUE.

Loops while TRUE; exits when FALSE.

Typical Use Case

User input validation, menu prompts, retry limits.

Searching arrays, reading file data until EOF.

AI Socratic Tutor

All-Access
Sign in or create an account. AI Tutor is an All-Access benefit.
Sign Up

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.

Independent Practice: Limited attempts

+30 XP

Allow at most three integer PIN attempts. The correct PIN is 2468. Stop immediately on success and output ACCESS; otherwise output DENIED after three wrong attempts.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.

solution.pseudo
All-Access Required

Interactive exercises for Module 2 and beyond require an active plan.

○ Immediate success
○ Third attempt
○ Three failures