What is a loop invariant?

Viewed 201561

I'm reading "Introduction to Algorithm" by CLRS. In chapter 2, the authors mention "loop invariants". What is a loop invariant?

16 Answers

Definition by How to Think About Algorithms, by Jeff Edmonds

A loop invariant is an assertion that is placed at the top of a loop and that must hold true every time the computation returns to the top of the loop.

In simple words, it is a LOOP condition that is true in every loop iteration:

for(int i=0; i<10; i++)
{ }

In this we can say state of i is i<10 and i>=0

A loop invariant is an assertion that is true before and after loop execution.

Related