Skip to main content
AP CSP course

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).

  1. After iterations 1 and 2,total is 3 then 6.
  2. After iterations 3 and 4,total is 9 then 12.
  3. 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
Open this example on its own

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

Ready to explore: Counted loops, accumulator and output placement
  1. Repetitions (0–8)3Waiting
  2. Increment (0–10)2Waiting
  3. 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

Ready to experiment
  1. Input 110Waiting
  2. Input 23Waiting
  3. 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

  1. Question 1 of 2Starting x 1, repeat x←x*2 four times. What is final x?

    Select one answer.

  2. Question 2 of 2Which TWO statements are correct?

    Select exactly two answers (0 of 2 selected).

Your practice so far

Check an answer to see its rationale.

Teacher guidance and scoring