< previous page page_601 next page >

Page 601
// alpha[i..j] have been printed
(Note that this dot-dot notation is not valid syntax in C++ language statements. We are talking only about comments in a program.) Here is the loop to zero out the alpha array, documented with a loop invariant and a loop postcondition:
for (i = 0; i < 100; i++)

        // Invariant (prior to test):
        //     alpha[0..i-l] == 0.0
        // && 0 <= i <= 100

    alpha[i] = 0.0;

// Assert:
//     alpha[0..99] == 0.0  &&  i == 100
Notice that the loop invariant states that i<100, whereas the While condition is i < 100. Remember that a loop invariant is logically positioned just before the loop test:
0601-01.gif
After the final loop iteration, which sets alpha[99] to zero, i is incremented to 100. Prior to the final loop test, then, the invariant i<100 is true because i equals 100; however, the loop condition is now FALSE and control exits the loop.
C++ does not prevent us from running off the end of an array. Writing loop invariants can give us an extra measure of safety by forcing us to think carefully about the number of iterations in an array-processing loop.
Initializing Arrays in Declarations
You learned in Chapter 8 that C++ allows you to initialize a variable in its declaration:
int delta = 25;

 
< previous page page_601 next page >