Syntax of the while
statement
Here is the syntax for the while
statement:
while ( condition )
statement
Some notes on the syntax:
- The condition is a Boolean expression; i.e., something that
evaluates to true or false.
- The condition can be very complicated, with relational operators
and logical operators.
- The statement is a single statement.
However it can be (and usually is) a block statement
containing several other statements.
- The statement is sometimes called the loop body.
Since the statement can be a single statement or
a block statement,
a while
statement can look like
either of the following:
Varieties of while Statements |
while ( condition )
statement;
|
while ( condition )
{
one or more statements
}
|
The style of indenting shown here leads to
fewer errors than alternative styles.