loopinvariants
Loop invariants are conditions that must hold before and after every iteration of a loop. They are a central tool in reasoning about program correctness, linking the state at the start of the loop to the state after it finishes.
To prove a loop correct, one typically shows three properties. Initialization: the invariant holds before the
Common examples include maintaining partial results such as sums or maxima, or preserving data-structure properties. Example:
Loop invariants are used in formal verification methods such as Hoare logic and weakest preconditions, and