# Write a loop invariant proof

Loop Invariants It is hard to keep track of what is happening with loops. Loops which don't terminate or terminate without achieving their goal behavior is a common problem in computer programming. A loop invariant is "just a condition that is true immediately before and after each iteration of a loop. However, usually we are not interested in simply finding a loop invariant just for the sake of finding a loop invariant.

Instead, we are interested in proving a program correct and if we want to prove a program correct then a well-chosen loop invariant turns out to be very useful. For example, the code in question is the inner loop of the bubble sort algorithm and its purpose is to make the array "more sorted".

Hence, to prove total correctness of this code we must prove three things: To prove 1 we need to consider three execution paths and a loop invariant will play a critical role when we consider PATH 2. PATH 1 Consider what happens when execution starts at the beginning of the code and arrives at the top of the loop for the first time.

Since nothing is done to the array on this execution path, the array is a permutation of the array at the beginning of the code. PATH 2 Now consider what happens when execution starts at the top of the loop, goes around the loop, and returns to the top of the loop. However, the array is still a permutation of the array at the beginning of the code since a swap is a type of permutation.

Thus, whenever execution gets to the top of the loop, the array is a permutation of the array at the beginning of the code. Note that the statement "the array is a permutation of the array at the beginning of the code" is the well-chosen loop invariant that we need to help us prove that the code is correct.

PATH 3 Finally, consider what happens when execution starts at the top of the loop but does not enter the loop and instead it goes to the end of the code. These three paths cover all possible ways that execution can go from the beginning of the code to the end of the code and, hence, we have proved 1 the array at the end of the code is a permutation of the array at the beginning of the code.In computer science, a loop invariant is a property of a program loop that is true before (and after) each iteration.

It is a logical assertion, sometimes checked within the code by an assertion call. Knowing its invariant(s) is essential in understanding the effect of a loop. I have that the outcome of the loop is the perfect square following m, but I'm not sure how to write that.

As always, all help is appreciated. algorithm loops post-conditions loop-invariant. It might seem "trivial", but this argument doesn't constitute a formal proof. In contrast, if you use an actual loop invariant, then you can use induction to prove that the loop invariant is maintained throughout the loop, and in particular is satisfied if the loop terminates.

The boolean formula in this rule is known as the loop invariant. 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.

## A brief recap of linear and binary search

In effect, this amounts to a proof by mathematical induction on the number of loop iterations executed. The first step is the base case, which shows that P holds the first time execution reaches the critical location The loop invariant also tells us that a[k-1] is all RED.

loop invariant is: A[j] end, where j is the mid index at any iteration OR A[j] > key for all start –> j, where j is the mid index at any iteration Important points Loop invariants is very useful for proving the correctness of a program.

Writing correct code, part 1: invariants (binary search part 4a) | The Reinvigorated Programmer