Loop invariants
The most common approach to this is to find a loop invariant (I), a predicate that
- Is true each time the loop head is reached
- And helps us prove the properties we need
(It approximates the fixed point of the loop)
If we have {P} while B do S {Q} then find I such that
- P ? I -Invariant is correct on entry
- {B ? I} S {I} –It’s invariant
- {?B ? I} ? Q –Loop termination proves Q