Loop invariant
A statement that holds before the loop and is preserved by every iteration. Combined with the exit condition, it should imply the desired postcondition.
REFERENCE SHEET
A correct output and a guaranteed halt are separate obligations.
Before this Proof techniques
A statement that holds before the loop and is preserved by every iteration. Combined with the exit condition, it should imply the desired postcondition.
Partial correctness asserts the postcondition whenever the program terminates. Total correctness additionally asserts termination on every permitted input.
A problem expressed through smaller instances, with base cases. A well-founded measure must decrease along recursive calls to justify termination.
Establish initialization, preservation, and that invariant plus loop exit implies the postcondition. These prove partial correctness.
If a nonnegative integer measure strictly decreases on every iteration while the loop continues, the loop terminates.
For nonnegative integers a,b, not both zero, repeated replacement (a,b)←(b,a mod b) while b≠0 terminates with a=gcd(a₀,b₀). The gcd is invariant and the second component strictly decreases.
A binary-search invariant specifies exactly which interval can still contain the answer.
State preconditions explicitly to catch overflow, invalid indices, and missing base cases.
01 / Trace
At each step compute the remainder.
(48,18)→(18,12)→(12,6)→(6,0). The gcd is 6.
02 / Prove
Relate s to the numbers processed so far.
Invariant: 0≤i≤n and s=i(i+1)/2. Initialization holds; adding i+1 establishes the formula for the next i. The nonnegative variant n−i decreases by 1. At exit s=n(n+1)/2.
These sheets use this topic as a prerequisite.
Further reading and full-length treatments: