Solution for Exercise 11-4

ex1104

#include <stdio.h>

int main()
{
    int d;

    d = -10;
    while(d<10)
    {
        printf("%d ",d);
        d++;
    }
    while(d>=-10)
    {
        printf("%d ",d);
        d--;
    }
    putchar('\n');
    return(0);
}

Notes

* As with the solution for Exercise 11-3, the first while loop's condition stops short of the value 10. That's because variable d equals 10 when the loop is finished. The second while loop takes advantage of that starting condition by not having to initialize variable d.

* The increment operator is used at Line 11; the decrement operator at Line 16.

* You could have coded the second while loop as:

That still works, and the output is the same, but it makes the code a little less readable. The goal was to display values between -10 and 10. Tossing an 11 in there might be considered confusing.