Solution for Exercise 9-19

ex0919

#include <stdio.h>

int main()
{
    int count;

    count = 0;
    while(1)
    {
        printf("%d, ",count);
        count = count+1;
        if( count > 50)
            break;
    }
    putchar('\n');
    return(0);
}

Notes

* Sure, you could have written the loop as while(count<=50). That works! But this is just a demonstration program. A more realistic example would show that the value of count doesn't steadily increase. In fact, imagine that count isn't supposed to increase or that if it goes beyond 50 the program must handle that situation in a special way.

* In real life, the condition that breaks the loop is often some action that takes place in a function, or it could be something as simple as the user typing a key.

* Also in real life, these types of loops generally don't output anything.