Science Fair Projects Ideas - Loop invariant

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
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice