While Loops

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.

  1. The computer evaluates the the condition that governs the while loop.
  2. If the condition is true:

  3. 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