Algorithms - Loop Invariants

Loop invariants can be used to:

document your code
check them by making explicit assert calls (available in most languages C, Java, Python, Pascal...)
prove correctness

In computer science, a loop invariant is a property of a program loop that is true before (and/or after) each iteration.

...

A loop invariant is a ________ __________, sometimes checked within the code by an assertion call.

A loop invariant is a logical assertion, sometimes checked within the code by an assertion call.

Loop Invariant to prove correctness requires three steps:

Initialization (basis): it is true prior to the first iteration
Maintenance (step): If it is true before an iteration of the loop, it remains true before the next iteration.
Termination:When the loop terminates, the invariant gives us a useful property th

Initialization (basis)

Initialization (basis): it is true prior to the first iteration

Maintenance (step)

Maintenance (step): If it is true before an iteration of the loop, it remains true before the next iteration.

Termination

Termination:When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct

Select the loop invariant as _______ as possible to the property you are seeking from your final ____________ of your algorithm.

Select the loop invariant as close as possible to the property you are seeking from your final OUTPUTS of your algorithm.