C31 · Topic 3.8 · 40 minutes · AAP-2.J, AAP-2.K
Counted repetition
By the end you can:
- Trace a fixed number of iterations.
- Place initialization and accumulation correctly.
- Predict the zero-iteration case.
Before this lesson: topics 3.1, 3.6.
- Iteration
- Repeating a sequence of instructions.
- Loop invariant
- A property maintained across the loop steps under stated assumptions.
Understand
A counted loop repeats its body a specified number of times. Record state after each repetition and keep initialization separate from repeated work. An accumulator such as total or count usually starts before the loop so each iteration builds on the previous state rather than discarding it.
The position of output and return statements matters. Displaying inside a loop produces a sequence of intermediate outputs; displaying after it produces a final result. A zero repetition count does no body work, so a defined initial value can still provide a useful result if the contract allows that input.
Worked example
total←0; REPEAT 4 TIMES{total←total+3}; DISPLAY(total).
- After iterations 1 and 2,total is 3 then 6.
- After iterations 3 and 4,total is 9 then 12.
- DISPLAY after the loop shows 12 once; resetting total inside would produce a different result.
Explore each concept
Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.
- REPEAT n TIMES
- Initialisation placement
- Display inside versus after
- Early return and zero iterations
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Repetitions (0–8)3Waiting
- Increment (0–10)2Waiting
- PlacementnormalWaiting
Ready to explore: Counted loops, accumulator and output placement
Why: Change an input, predict the first effect, then advance one step.
Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.
Step through and experiment
Change it · step through · explain
Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.
Ready · Make a prediction
- Input 110Waiting
- Input 23Waiting
- Input 3at-mostWaiting
Ready to experiment
Why: Predict the first change, then advance one step.
Try this: Compare equal and at-most with start 10 and decrement 3, then try start 0.
Check a misconception
Claim: Initialization inside the loop is equivalent to initialization before it.
Resetting an accumulator discards earlier iterations’ contributions.
Guided practice
Move total←0 inside the body and predict the final result after four iterations.
Work with a partner. Make a prediction, compare your reasoning, then identify the rule that supports your answer.
Independent transfer
Predict the original programme with 0 repetitions.
Exit check
What output changes if DISPLAY(total) moves inside the original body?
Write your reasoning before comparing with a classmate. If your teacher assigned this task, submit it through My classes to save your work and receive feedback.
Check yourself
Your practice so far
Check an answer to see its rationale.