do
action
while (condition)
The newline is optional after do. It is also optional
after action providing the statement is terminated
by a semicolon. The main feature of this loop is that the conditional
expression appears after the action. Thus, the action is
performed at least once. Look at the following do loop.
BEGIN {
do {
++x
print x
} while ( x <= 4 )
}
In this example, the value of x is set in the body
of the loop using the auto-increment operator. The body of the loop
is executed once and the expression is evaluated. In the previous
example of a while loop, the initial value of
i was set before the loop. The expression was
evaluated first, then the body of the loop was executed once. Note
the value of x when we run this example:
$ awk -f do.awk
1
2
3
4
5
Before the conditional expression is first evaluated,
x is incremented to 1. (This relies on the fact
that all awk variables are initialized to zero.) The body of the loop
is executed five times, not four; when x equals 4,
the conditional expression is true and the body of the loop is
executed again, incrementing x to 5 and printing
its value. Only then is the conditional expression evaluated to false
and the loop exited. By changing the operator from "<=" to "<",
or less than, the body of the loop will be executed four times.
To keep in mind the difference between the do loop
and the while loop, remember that the
do loop always executes the body of the loop
at least once. At the bottom of the procedure,
you decide if you need to execute it again.
For an example, let's look at a program that loops through the fields
of a record, referencing as many fields as necessary until their
cumulative value exceeds 100. The reason we use a
do loop is that we will reference at least one of
the fields. We add the value of the field to the total, and if the
total exceeds 100 we don't reference any other fields. We reference
the second field only if the first field is less than 100. Its value
is added to the total and if the total exceeds 100 then we exit the
loop. If it is still less than 100, we execute the loop once again.
{
total = i = 0
do {
++i
total += $i
} while ( total <= 100 )
print i, ":", total
}
The first line of the script initializes the values of two variables:
total and i. The loop
increments the value of i and uses the field
operator to reference a particular field. Each time through the loop,
it refers to a different field. When the loop is executed for the
first time, the field reference gets the value of field one and
assigns it to the variable total. The conditional
expression at the end of the loop evaluates whether
total exceeds 100. If it does, the loop is exited.
Then the value of i, the number of fields that
we've referred to, and the total are printed. (This script assumes
that each record totals at least 100; otherwise, we'd have to check
that i does not exceed the number of fields for the
record. We construct such a test in the example presented in next
section to show the for loop.)
Here's a test file containing a series of numbers:
$ cat test.do
45 25 60 20
10 105 50 40
33 5 9 67
108 3 5 4
Running the script on the test file produces the following:
$ awk -f do.awk test.do
3 : 130
2 : 115
4 : 114
1 : 108
For each record, only as many fields are referenced as needed for the total
to exceed 100.