Cost And Management Accounting Formats (AS Level): Cost–Volume–Profit Decision Table Format (Copy)
REPEAT UNTIL Loops: Exit-Controlled Iteration
What A REPEAT UNTIL Loop Is (Exit-Controlled Loop)
- A REPEAT UNTIL loop runs the loop body at least once
- The condition is checked after the loop body executes
- The loop stops when the condition becomes TRUE
- Best used when:
- You must run the code at least once
- Input must be taken before validation
- Menu systems must display at least one time
Standard REPEAT UNTIL Structure (Pseudocode)
REPEAT
<statements>
UNTIL <condition>
- Execution order
- Execute loop body
- Check condition
- If condition is FALSE → repeat
- If condition is TRUE → exit loop
- Key difference from WHILE
- REPEAT UNTIL guarantees minimum one execution
REPEAT UNTIL Vs WHILE (Quick Comparison)
| Feature | REPEAT UNTIL | WHILE |
|---|---|---|
| Condition checked | After loop | Before loop |
| Minimum executions | 1 | 0 |
| Input-first logic | Ideal | Risky |
| Input validation | Good | Good |
| Risk of skipping loop | No | Yes |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Structure Components (Exam-Focused)
Loop Body
- Executes before condition is checked
- Usually contains:
- INPUT
- Processing
- Update logic
UNTIL Condition
- Must be a Boolean expression
- Loop exits when condition becomes TRUE
- Common patterns
choice = "X"num >= 0attempts = 3
Critical Logic Rule
- Condition must be written as:
- “Stop when this becomes TRUE”
- Common mistake
- Writing the condition as if it were a WHILE loop
Core REPEAT UNTIL Patterns (High-Frequency Exam Use)
Pattern 1: Input Validation (Guaranteed One Input)
- Scenario
- User must enter a positive number
REPEAT
INPUT num
UNTIL num > 0
- Why REPEAT UNTIL is perfect here
- Input must occur at least once
- Pitfall
- Writing
UNTIL num < 0(logic reversed)
- Writing
Pattern 2: Menu System (Exit Option)
- Scenario
- Display menu until user chooses “Q”
REPEAT
OUTPUT "A. Add"
OUTPUT "B. Delete"
OUTPUT "Q. Quit"
INPUT choice
UNTIL choice = "Q"
- Examiner focus
- Correct exit condition
- Menu displayed at least once
Pattern 3: Sentinel-Controlled Input
- Scenario
- Input numbers until user enters
-1
- Input numbers until user enters
REPEAT
INPUT num
IF num <> -1 THEN
total ← total + num
ENDIF
UNTIL num = -1
OUTPUT total
- Why this structure works
- Sentinel not added to total
- Pitfall
- Adding sentinel before checking
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Controlled Repetition Using Counters
Pattern 4: Limited Attempts (Password Example)
- Scenario
- Allow maximum 3 attempts
attempts ← 0
REPEAT
INPUT password
attempts ← attempts + 1
UNTIL password = "ABC123" OR attempts = 3
- Examiner marking points
- Counter initialised
- Counter updated inside loop
- Compound condition used correctly
Pattern 5: Fixed Repetition With REPEAT (Less Common)
- Scenario
- Output numbers 1 to 5
i ← 1
REPEAT
OUTPUT i
i ← i + 1
UNTIL i > 5
- Why FOR is better here
- Fixed repetitions are clearer with FOR
- Why REPEAT still works
- Condition stops loop correctly
Using REPEAT UNTIL With Arrays
Pattern 6: Search Until Found
- Scenario
- Search for value in array until found
index ← 1
REPEAT
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
UNTIL found = TRUE OR index > n
- Strength of REPEAT UNTIL
- Guarantees at least one check
- Pitfall
- Accessing
arr[index]afterindex > n
- Accessing
Pattern 7: Input Validation With Range Check
- Scenario
- Input number between 1 and 10 inclusive
REPEAT
INPUT num
UNTIL num >= 1 AND num <= 10
- Common mistake
- Using OR instead of AND
- Correct logic
- Both bounds must be satisfied
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Tracing A REPEAT UNTIL Loop (Dry Run Method)
Tracing Checklist
- Assume loop runs once minimum
- Track:
- Variable values
- Condition result after each iteration
- Stop when condition becomes TRUE
Worked Trace Example
i ← 1
REPEAT
OUTPUT i
i ← i + 1
UNTIL i = 4
| Iteration | i Before | Output | i After | Condition (i = 4) |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | FALSE |
| 2 | 2 | 2 | 3 | FALSE |
| 3 | 3 | 3 | 4 | TRUE |
- Output:
1 2 3
Common Examiner Pitfalls (And Fixes)
Pitfall 1: Reversed Condition Logic
- Mistake
- Writing the condition as “keep going while”
- Example (wrong)
UNTIL num < 0
- Correct thinking
- “Stop when input is valid”
UNTIL num >= 0
Pitfall 2: Infinite Loop
- Cause
- Condition never becomes TRUE
- Example
REPEAT
OUTPUT "Hello"
UNTIL 1 = 2
- Fix
- Ensure condition depends on changing variable
Pitfall 3: Misplaced Processing Logic
- Example
REPEAT
total ← total + num
INPUT num
UNTIL num = -1
- Error
- Adds previous invalid value
- Fix
- Input first, then process conditionally
Pitfall 4: Wrong Use Of OR / AND
- Example (wrong)
UNTIL num >= 1 OR num <= 10
- Why wrong
- Condition always TRUE
- Correct
UNTIL num >= 1 AND num <= 10
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Entry-Controlled Vs Exit-Controlled Loops (Decision Table)
| Requirement | Best Choice |
|---|---|
| Must run at least once | REPEAT UNTIL |
| Validate after input | REPEAT UNTIL |
| Unknown repetitions | WHILE |
| Fixed repetitions | FOR |
| Early termination needed | WHILE / FOR with flag |
Copy-Paste REPEAT UNTIL Templates (Exam-Safe)
Template A: Input Validation
REPEAT
INPUT value
UNTIL value > 0
Template B: Sentinel Loop
REPEAT
INPUT num
UNTIL num = -1
Template C: Limited Attempts
attempts ← 0
REPEAT
INPUT data
attempts ← attempts + 1
UNTIL attempts = limit
Template D: Menu Exit
REPEAT
<display menu>
INPUT choice
UNTIL choice = "X"
“Spot The Bug” Mini Practice
Bug 1
REPEAT
INPUT num
UNTIL num < 0
- Error
- Stops only on invalid input
- Fix
- Reverse condition to valid exit
Bug 2
REPEAT
OUTPUT "Menu"
UNTIL choice = "Q"
- Error
choicenever changes
- Fix
- Add input inside loop
Bug 3
REPEAT
INPUT num
total ← total + num
UNTIL num = -1
- Error
- Adds sentinel
- Fix
- Add conditional check before summation
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
