### Designing loop: the loop invariant

• Property of loops

• An important property of loops

 In each iteration of a loop statement, the entire loop body is executed

Implication:

 Because the loop body is executed many times, to ensure correctness, the same condition must hold at the start of each iteration !!!

• The loop invariant:

• In order for the loop statement to execute correctly for every iteration:

 Some property/relationship must be valid at the start of every iteration of the loop In fact, the statements inside the loop body must make sure this pre-condition will be satisfied at the start of the next interation !

Note:

 I have to be vague on what the exact condition is... Because the property is different for different loops

• The technical term for this property that is true at the start of every iteration of the loop is:

 the loop invariant

• Loop invariant (Wikipedia):

 Informally, a loop invariant is a statement of the conditions that should be true on entry into a loop The loop invariant must be true on every entry (i.e., for every iteration) of the loop.

• Examples of loop invariant

• Loop 1: find all divisors of the number n

 ``` x = 1; while ( x <= n ) // Run x = 1, 2, ..., n { Invariant: We have found all divisors of n that are < x x = the current divisor that we will try if ( n % x == 0 ) { // x is a divisor of n System.out.println(x); // Print x (because it's a divisor) } x++; // Make sure we more to the next number !! // or else: infinite loop !!! } Invariant when loop ends: We have found all divisors of n that are <= n ```

Note:

• The loop invariant must be true at the start of every iteration !!!

• Furthermore: the loop invariant is also true when the loop exits

Because:

 The loop exits when the loop-continuation-condition is false. In other words, the program was ready to start the next iteration of the loop Because we are starting a new iteration, the loop invariant must then be true !!!

• Loop 2: find all common divisors of a and b

 ``` min = min(a,b); a = 1; while ( a <= min ) // Run a = 1, 2, ..., min(x,y) { Invariant: We have found all common divisors of a and b that are < x x = the current common divisor that we will try if ( x % a == 0 && y % a == 0 ) { // a is a divisor of x and y System.out.println(a); // Print a (because it's a common divisor) } a++; // Make sure we move to the next number !! // or else: infinite loop !!! } Invariant when loop ends: We have found all common divisors of a and b that are <= min ```

• Loop 3: find all factors of a number x

 ``` f = 2; while ( x > 1 ) { Invariant: x = the number that remains to be factor and we have removed all factors < f if ( x % f == 0 ) { // f is a factor of x System.out.println(f); // Print f (because it's a divisor) x = x / f; // Remove factor f from x } else { f++; // Use next number as factor } } Invariant when loop ends: The number that remains to be factored = 1 and we have removed all factors including the largest factor (I.e.: we have found all the factors) ```

• How to design a loop

• There is no simple answer to this question

• That's may be why Knuth called his collection of books: the Art of Computer Programming

• Use loop invariance