🔄 What is a Loop Invariant?

A visual introduction to proving loops correct

📚 Analogy: Reading a Book

A loop invariant is like a bookmark that always tells you the truth about what you've read so far.

Chapter 0: Intro
Chapter 1: Basics
Chapter 2: Advanced
Chapter 3: Expert

🎯 Connection to Induction

Like dominoes: if the first falls (Init) and each pushes the next (Maintenance), all will fall!

0
1
2
3
...
n
🚀
Initialization
True BEFORE the first loop iteration (base case)
🔁
Maintenance
If true before iteration i, still true before iteration i+1
🏁
Termination
When loop ends, invariant proves correctness
INIT
iter 0
iter 1
iter 2
iter 3
END
Click "Next Step" to see how a loop invariant works through each phase.

💡 Key Insight

A good loop invariant captures "what progress have we made so far". At each iteration, it describes the partial result computed up to that point. When the loop finishes, this partial result becomes the complete answer!