The do-while Loop Statement

The do-while loop is analogous to the while loop in that the loop body is repeatedly executed over and over again as long as the Boolean expression is true. However, there is one important difference—the loop body, located between the keywords do and while (see Syntax Box 9.2), is executed before the Boolean expression is evaluated. Consequently, the loop body of the do-while loop is always executed at least once, even if the Boolean expression is false initially.

Syntax Box 9.2 The do-while Statement

						do_while_statement:=
     do
       <Loop_body>
     while (<Loop_condition>);

where:

						<Loop_body>::= <Statement>;
           ::= <Compound_statement>

<Loop_condition>::= <Boolean expression>

Note:

  • The <Loop_body> is repeated as long as <Loop_condition> ...

Get C# Primer Plus 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.