The syntax for while loops in C is this:
while ( condition )
{
...
... <-- statements in the "body" of the loop.
...
}
When the computer reaches a while statement in C, it goes through the
following steps.
- The computer evaluates the the condition that governs the while loop.
- If the condition is true:
- The computer executes the statements in the body of the loop. This is
the sequence of statements enclosed in the braces.
- The computer skips back to step (1).
- If the condition is false, the computer skips directly to the next
statement that follows the loop.
In other words, the computer executes the body of the while loop over and
over until the condition becomes false. When the condition is false, the loop
is over and the computer skips forward to the next statement.
We will now look at three different applications of while loops.
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah