Skip to main content
AP CSP course

C32 · Topic 3.8 · 40 minutes · AAP-2.J, AAP-2.K

Conditional repetition and termination

By the end you can:

  • Trace a stopping condition and update.
  • Identify a reachable progress measure.
  • Distinguish bounded observation from proof.

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 conditional loop repeats until a stated condition is satisfied. Check whether it is initially satisfied, how the body changes relevant state and whether the update can reach a stopping state. An equality target can be skipped even when the value appears to move past it.

A termination argument needs a stated domain and progress, not merely a fast example. For a nonnegative counter and positive decrement, stopping at counter≤0 can be justified because each iteration reduces its distance to that boundary. A tool’s step cap is only an observation limit for what it ran.

Worked example

Counter 10 decreases by 3. Compare stop at=0 with stop at≤0.

  1. Values after updates are 7,4,1,-2.
  2. The≤0 version stops at-2 after 4 updates.
  3. The=0 version skips the target; in this exact integer sequence 10−3 k never equals 0 for an integer k.

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: Moving toward a target guarantees equality will eventually be reached.

The update can skip an equality target; reason about reachability and the allowed values.

Guided practice

Trace starting 9 with step 3 and equality stopping.

Work with a partner. Make a prediction, compare your reasoning, then identify the rule that supports your answer.

Independent transfer

Why is step 0 a problem for the≤0 version starting 10?

Exit check

What is justified if a bounded observer stops after 50 updates without success?

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 2Start 10,step 3,stop≤0. How many updates occur?

    Select one answer.

  2. Question 2 of 2Which precondition supports this decrementing-loop termination argument?

    Select one answer.

Your practice so far

Check an answer to see its rationale.

Teacher guidance and scoring