Introduction to Loop Invariants

Loop Invariants

Loop invariants are formalized statements that are true before and after each loop iteration. Understanding loop invariants can sometimes lead to a bettter understanding of a loop's function (i.e. what it does, and what it does not do). One common misunderstanding students have about loop invariants is that they only need to be true before and after each iteration of the loop. Loop invariants do not have to be true during each loop iteration. This is a somewhat theortical topic which may come up during a software interview or during the debugging process of a large program.