The while Statement

This is the general form of the while loop:

while (expression)
      statement

The statement part can be a simple statement with a terminating semicolon, or it can be a compound statement enclosed in braces.

The examples so far have used relational expressions for the expression part; that is, expression has been a comparison of values. More generally, you can use any expression. If expression is true (or, more generally, nonzero), the statement is executed once, and then the expression is tested again. This cycle of test and execution is repeated until expression becomes false (zero). Each cycle is called an iteration (see Figure 6.1).

Figure 6.1. Structure of the while loop.

Terminating a while Loop

Here is ...

Get C Primer Plus®, Third 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.