Go forward to Do Statement.
Go backward to If Statement.
Go up to Statements.

The `while' Statement
=====================

   In programming, a "loop" means a part of a program that is (or at
least can be) executed two or more times in succession.

   The `while' statement is the simplest looping statement in `awk'.
It repeatedly executes a statement as long as a condition is true.  It
looks like this:

     while (CONDITION)
       BODY

Here BODY is a statement that we call the "body" of the loop, and
CONDITION is an expression that controls how long the loop keeps
running.

   The first thing the `while' statement does is test CONDITION.  If
CONDITION is true, it executes the statement BODY.  (CONDITION is true
when the value is not zero and not a null string.)  After BODY has been
executed, CONDITION is tested again, and if it is still true, BODY is
executed again.  This process repeats until CONDITION is no longer
true.  If CONDITION is initially false, the body of the loop is never
executed.

   This example prints the first three fields of each record, one per
line.

     awk '{ i = 1
            while (i <= 3) {
                print $i
                i++
            }
     }'

Here the body of the loop is a compound statement enclosed in braces,
containing two statements.

   The loop works like this: first, the value of `i' is set to 1.
Then, the `while' tests whether `i' is less than or equal to three.
This is the case when `i' equals one, so the `i'-th field is printed.
Then the `i++' increments the value of `i' and the loop repeats.  The
loop terminates when `i' reaches 4.

   As you can see, a newline is not required between the condition and
the body; but using one makes the program clearer unless the body is a
compound statement or is very simple.  The newline after the open-brace
that begins the compound statement is not required either, but the
program would be hard to read without it.