# All Science Fair Projects

## Science Fair Project Encyclopedia for Schools!

 Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary

# Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

# Loop invariant

In computer science, a loop invariant is an invariant used to prove properties of loops.

Specifically in Floyd-Hoare logic, the partial correctness of a while loop is governed by the following rule of inference:

$\frac{\{C\land I\}\;\mathrm{body}\;\{I\}} {\{I\}\;\mathbf{while}\ (C)\ \mathrm{body}\;\{\lnot C\land I\}}$

This rule is a deductive step that has as its premise the Hoare triple $\{C\land I\}\;\mathrm{body}\;\{I\}$. This triple is actually a relation on machine states. It holds whenever starting from a state in which the boolean expression $C\land I$ is true and successfully executing some program called body, the machine ends up in a state in which I is true. If this relation can be proven, the rule then allows us to conclude that successful execution of the program while (C) body will lead from a state in which I is true to a state in which $\lnot C\land I$ holds. The boolean formula I in this rule is known as the loop invariant.

The following example illustrates how this rule works. Consider the program

while (x<10) x:= x+1;


One can then prove the following Hoare triple:

$\{x\leq10\}\; \mathbf{while}\ (x<10)\ x := x+1\;\{x=10\}$

The condition C of the while loop is x < 10. A useful loop invariant I is $x\leq10$. Under these assumptions it is possible to prove the following Hoare triple:

$\{x<10 \land x\leq10\}\; x := x+1 \;\{x\leq10\}$

While this triple can be derived formally from the rules of Floyd-Hoare logic governing assignment, it is also intuitively justified: Computation starts in a state where $x<10 \land x\leq10$ is true, which means simply that x < 10 is true. The computation adds 1 to x, which means that $x\leq10$ is still true.

Under this premise, the rule for while loops permits the following conclusion:

$\{x\leq10\}\; \mathbf{while}\ (x<10)\ x := x+1 \;\{\lnot(x<10) \land x\leq10\}$

However, the post-condition $\lnot(x<10)\land x\leq10$ (x is less than or equal to 10, but it is not less than 10) is logically equivalent to x = 10, which is what we wanted to show.

The loop invariant plays an important role in the intuitive argument for soundness of the Floyd-Hoare rule for while loops. The loop invariant has to be true before each iteration of the loop body, and also after each iteration of the loop body. Since a while loop is precisely the repeated iteration of the loop body, it follows that if the invariant is true before entering the loop, it must also be true after exiting the loop.

Because of the fundamental similarity of loops and recursive programs, proving partial correctness of loops with invariants is very similar to proving correctness of recursive programs via induction. In fact, the loop invariant is often the inductive property one has to prove of a recursive program that is equivalent to a given loop.

03-10-2013 05:06:04