A decreasing measure used to prove algorithm termination. A nonnegative integer loop variant must strictly decrease after every completed body whose condition was true; the body itself must terminate.[ref]
Loop invariants. CS 2112 Fall 2019; Loop invariants, Using loop invariants to show code is correct: Termination and the decrementing function
The prefix maximum correctness example uses . It counts unread entries and is independent of whether the new entry changes the maximum. Ties therefore cannot stall progress: increases even when stays the same. An integer bounded below cannot strictly decrease indefinitely, which supplies the missing algorithm termination argument.
Control flow and four proof obligations for the prefix maximum correctness example
. Initialization, preservation, and exit establish partial correctness; the loop variant establishes algorithm termination and hence total correctness. The loop invariant is restored after both variable assignments. The input array stays unchanged throughout. Details. Articles by others on the same topic
A **loop variant** is a concept used in computer science, particularly in the context of program verification and formal methods. It is a condition that helps ensure that a loop terminates successfully and does not run indefinitely. A loop variant is typically a scalar value (it could be an integer or another comparable type) associated with a loop that satisfies two main properties: 1. **Initialization**: The loop variant must be initialized before the loop starts executing.
