The while Loop

The while loop is a for loop stripped of the initialization and update parts; it has just a test condition and a body:

while (test-condition)
          body
				

First, a program evaluates the test-condition expression. If the expression evaluates to true, the program executes the statement(s) in the body. As with a for loop, the body consists of a single statement or a block defined by paired braces. After it finishes with the body, the program returns to the test-condition and reevaluates it. If the condition is nonzero, the program executes the body again. This cycle of testing and execution continues until the test-condition evaluates to false. (See Figure 5.3.) Clearly, if you want the loop to terminate eventually, something within the ...

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.