fossterer.github.io

Invariants in computer science (and Loop Invariants)

While following this debate that brought up RAII, I ended up reading about Loop Invariants

So in essence, in the cases,

while (i <= 10) {
        doSomething();
}

and

for(i = 0; i <= n; i++) {
    do_something();
}

the condition we say

and the very condition that we say

in formal terms is a loop invariant

Extending this idea, if you see some Java logic with Assert() statements, the implementation can be said to be confirming invariance