Summary

The main topic of this chapter has been program control. C offers you many aids for structuring your programs. The while and the for statements provide entry-condition loops. The for statements are particularly suited for loops that involve initialization and updating. The comma operator enables you to initialize and update more than one variable in a for loop. For the less common occasion when an exit-condition loop is needed, C has the do while statement.

A typical while loop design looks like this:

get first value
while (value meets test)
{
    process the value
    get next value
}

A for loop doing the same thing would look like this:

for (get first value; value meets test; get next value)
    process the value

All these loops use a test ...

Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.