Solution for Exercise 9-3

ex0903

#include <stdio.h>

int main()
{
    int count;

    for(count=-5; count<6; count=count+1)
    {
        printf("%d\n",count);
    }
    return(0);
}

Notes

* After initialization, the for loop ignores its first part. So after the value of count is set equal to -5, that part of the for statement is finished. From then on, the middle part is compared at the start of each iteration of the loop. "Is the value of count less than 6?" If so, the loop continues.

* After the loop spins, and before it repeats again, the third part of the for statement is executed, count=count+1. Then the comparison is made, "Is the value of count less than 6?"

* Most for loops are constructed in the pattern shown above as well as in Listing 9-1 in the book. The initialization usually starts at zero, although as this code demonstrates it doesn't always have to.