Recap
- Iteration repeats a block zero or more times until a stopping condition is met.
REPEAT n TIMES { ... }runs the block exactlyntimes.REPEAT UNTIL(condition) { ... }checks the condition before each pass, runs the block while it is false, and stops when it becomes true.- If the condition is true at the start, the block runs zero times.
- A condition that can never become true makes an infinite loop.
- A counter adds a fixed amount each pass; an accumulator builds a running total.
Key relationships
| Pattern | Code | Result |
|---|---|---|
| Fixed repeat | REPEAT 5 TIMES { x ← x + 2 } |
x increases by 10 |
| Counter | i ← i + 1 |
i counts the passes |
| Accumulator | total ← total + n |
total builds a sum |
| Loop until goal | REPEAT UNTIL(balance ≥ 150) |
Stops once the goal is reached |
| Zero passes | Condition already true | Block never runs |
| Infinite loop | REPEAT UNTIL(n = 0) with n stepping by 3 from 10 |
Never ends |
Assumptions
- The condition is evaluated before every pass.
- Variables change only through assignments in the block.
DISPLAYoutputs are listed in the order they happen.
Method for tracing a loop
- Draw columns for every variable and for the condition.
- Write the starting values in the first row.
- Check the condition (REPEAT UNTIL) or count the pass (REPEAT n TIMES).
- Run the block statements one by one, updating the row.
- Repeat until the stop; note how many times the block ran.
Mistakes to avoid
- Reading REPEAT UNTIL as “while true”.
- Forgetting that the block can run zero times.
- Using
=as a stopping test where the variable can jump past the value. - Putting the counter update inside an IF that may not be true.
- Off-by-one slips:
n ≥ 150versusn > 150.
Worked mini-example
n ← 1
count ← 0
REPEAT UNTIL(n ≥ 100)
{
n ← n * 2
count ← count + 1
}
DISPLAY(count)
DISPLAY(n)
Output: 7 128
n goes 2, 4, 8, 16, 32, 64, 128. After the 7th pass n is 128, which is at least 100, so the loop stops. count is 7.
Quick self-check
- How many times does
REPEAT UNTIL(x > 5)run its block whenxstarts at 8? - What does
xhold afterx ← 1andREPEAT 3 TIMES { x ← x * 3 }? - Why is
REPEAT UNTIL(n = 0)risky whenndecreases by 3 from 10?
Answers
- Zero times: the condition is already true.
- 27 (1 × 3 × 3 × 3).
nsteps over 0 (10, 7, 4, 1, −2, …), so the condition never becomes true and the loop never ends.
Where marks are won and lost
Typical items give a loop with a counter and ask for the final value or the number of passes. Trace it row by row and watch the exact stopping condition (≥ versus >). Another item asks which segment never ends, or which condition replaces a broken one. Robot questions ask for the final square or which loop reaches a goal. In the Create task your program must contain iteration, and you must explain what the loop does and why it ends.
Back to the full study guide for worked examples and explanations.