The while Loop Statement

The while loop repeats a statement or compound statement again and again, here referred to as loop body, as long as its associate loop condition, consisting of a Boolean expression, is true (see Syntax Box 9.1). When the loop condition is false while being evaluated, flow of control moves to the line immediately following the while loop.

Syntax Box 9.1 The while-Loop

						while_statement:=
while (<Loop_condition>)
    <Loop_body>

where:

<Loop_condition>::= <Boolean_expression>

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

Note:

  • The loop body is repeated as long as <Loop_condition> is true.

The keyword while is suitably chosen; we can even use it to describe the semantics of the while loop in plain English:

While (the ...

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.