Solution for Exercise 9-21

ex0921

#include <stdio.h>

int main()
{
    int x;

    for(x=0;x<10;x=x+1,printf("%d\n",x))
        ;
    return(0);
}

Notes

* Everything happens in Line 7. Everything!

* First, the value of variable x is initialized to zero.

* Second, the loop spins while the value of x is less than 10. So far, so good.

* Third, two statements belong to the "repeat each" part of the for statement. The first adds 1 to the value of x, x=x+1. Then comes a comma, then printf("%d\n",x). Such a construction is perfectly legitimate.

* To be clear that the for statement is the only part of the loop, the semicolon appears on Line 8 all by itself, neatly indented.