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);
}

Output

-5
-4
-3
-2
-1
0
1
2
3
4
5

Notes

* After initialization, the for loop, where count is set equal to -5, the statements start repeating. From then on, the middle evaluation is checked at the start of each iteration of the loop. "Is the value of count less than 6?" If so, the loop continues.

* After the statements repeat, the third part of the for statement is executed, count=count+1. Then the comparison is made (the middle part), "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, though as this code demonstrates it doesn't always have to.