Testing Progress in a While Loop

A condition-driven loop needs a state update that moves it toward a stopping condition within the allowed domain.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Find a decreasing quantity
  2. Avoid a skipped target
  3. Write down what must get closer to finishing
  4. Handle interruptions without hiding an infinite retry

Find a decreasing quantity

With six tasks left, processing two per step gives remaining counts four, two, and zero. If the interruption branch skips this update and still repeats, the loop can stall. A termination measure formalizes that concern under a stated ordering.

Avoid a skipped target

With five tasks left and steps of two, checking only equality with zero yields five, three, one, and minus one. Stop when no positive remainder exists or process the smaller last batch. A state trace should include an odd-sized input.[1]

Write down what must get closer to finishing

A worker with seven records remaining can process at most three per step. The remaining sizes become four, one, and zero when the last batch uses its available count. If progress requires at least one consumed record on each successful pass, that nonnegative remainder strictly decreases. This makes a useful termination argument. Remaining work as a measure describes how a decreasing count relates to a proof rather than just one observed trace.

Handle interruptions without hiding an infinite retry

If a read fails and the continue branch leaves remaining unchanged, an ordinary attempt might never move toward zero. Either reject an unrecoverable failure or give retries a separate bounded counter under the caller's contract. The state would then record both remaining work and available attempts.

Test one batch not divisible by the batch size and a failure on the final record. Check that the exact target cannot be skipped by subtraction. A condition testing equality with zero can keep looping after a negative result unless the budget or update prevents that state. Fixed-size batches gives concrete edge fixtures.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩