Loop invariant

Hi, I'm a computer science student and i'm trying to understand loop invariant. I just know that is a proof of correctness for an algorithm and it is very similar to mathematical induction. Maybe an example will help me to understand or maybe some of you know an intuitive way to understand this subject. Thanks in advance!
2 Replies
mtreit
mtreit4d ago
An invariant is just a fancy way of saying something that holds true throughout the course of execution of the code.
VoidPointer
VoidPointer3d ago
Like if you have for int x = 0; x < 10; x++, then x < 10 would be your invariant, because it's true for the entire lifetime of the loop. In other words, while the loop is running, x is always less than 10, and that is why it is called invariant.

Did you find this page helpful?