Quarry School

Explain when a repetition ends — quick review

In plain words

A cashier counting a drawer of coins repeats one small action: take a coin and add it to the count.

Rule

A loop repeats its steps until its stopping condition is met. If the stopping condition can never be met, the loop never ends. To explain why a loop ends, name its stopping condition and the step in each pass that moves toward it.

Step

  1. Find the steps that repeat, and mark the step that sends the reader back.

  2. Find the stopping condition, which is the test that lets the reader leave the loop.

  3. Find the step inside the loop that changes what the stopping condition tests. In the largest-card procedure, taking a card changes how many cards are left.

  4. Check the direction of that change. In the largest-card procedure every pass leaves one card fewer in the row, so the row runs out.

  5. Trace a small input and count the passes. For a row of three cards, step 3 is carried out twice.

  6. Write one sentence that names the stopping condition and the step that moves toward it.

Trap

A loop can look finished on paper because the tracer stops when the answer seems clear. Trace until the stopping condition is met, and note the step at which the reader leaves the loop.

The serious slip is a stopping condition that the passes cannot reach. Take a procedure invented for this paragraph. It writes 1, then repeats two steps: add 2 to the number, and stop if the number is 10. The number goes 3, 5, 7, 9, 11 and on, and never equals 10. The ending condition never becomes true, so the loop does not end. Compare the wording of the stopping condition with what each pass changes.