The While Loop

Conditionals are one broad type of control structure and loops are the other. The C language supports two loop formats: while (and its sibling do…while) and for. Each loop type accomplishes the same thing—repeatedly performing some statements for a certain duration—but in slightly different ways.

The while loop is like an if conditional in that it checks a condition and then executes some statements if it's true:

while (condition) {
   /* Do whatever. */

Once the loop has successfully executed its statements, it will then recheck the condition. If the condition is still true, the process will be repeated. If the condition is false, the execution of the loop is over and the program will continue running (Figure 4.13).

Figure 4.13. ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.