|
|
The while statement is exactly that of the C
programming language:
while (expression) statement
The expression is evaluated; if it is nonzero and non-null, the statement is executed, and the expression is tested again. The cycle repeats as long as the expression is nonzero. For example, use the following to print all input fields one per line:
{ i = 1 while (i <= NF) { print $i i++ } }The do-while statement has the following form:
The statement is executed repeatedly until the value of the expression becomes zero. Because the test takes place after the execution of the statement (at the bottom of the loop), it is always executed at least once. As a result, the do statement is used much less often than while or for, which test for completion at the top of the loop.
The following example of a do-while statement prints all lines except those occurring between the strings ``start'' and ``stop'':
/start/ { do { getline x } while (x !~ /stop/) } { print }