do...while loop statement

In the AWK while loop, the condition is checked at entry, so it is called an entry-controlled loop. The do...while loop is an exit-controlled loop; the condition is checked at exit time. The do...while loop always executes the body at least once and then repeats the body as long as the condition is true.

It differs from the while and for in a way; it tests the conditional-expressions at the bottom instead of the top, so it will always execute the body once, even if the condition evaluates to false as illustrated in Figure 7.6. Its syntax is as follows:

do

action statements

while ( conditional-expression )

 

Following figure 7.6 illustrates the working of a do-while loop statement in AWK programming:

Figure 7.6: A do...while statement flowchart

Here also, newlines are optional after the keyword do and after action statements. If while appears on the same line as statement, then the only statement should be terminated by a semicolon.

Let's understand the working of the while loop with some examples. In the following example the print statement is executed only once because we ensure that the conditional-expression evaluates to false. If this were a while statement, with the same initialization of counter to 1 and conditional-expression, the body of the loop containing action statements would not be executed once:

$ vi dowhile1.awk

BEGIN {
counter = 1;
do
print "Print this line one time"
while ( counter != 1 )
}

$ awk -f dowhile1.awk

The output on execution of the preceding code is as follows:

Print this line one time

In the following example, we will use the marks.txt sample file to calculate the total of marks obtained by the student in different subjects. For each line, the program has to add the values of field 2 through the to last field. So, we first initialize i=2 before entering the loop. Then it execute the loop body followed by a test using conditional-expression if it has reached last field in record (I <= NF). NF represents the total number of fields in each record. The output of this program is the same as the while3.awk program, but it uses the do...while loop construct as follows:

$ vi dowhile2.awk

{
i=2; total=0;
do
{
total = total + $i;
i++;
}
while ( i <= NF )
print "Student Name : ", $1, " ", "Total Marks :", total;
}

$ awk -f dowhile2.awk marks.txt

The output on execution of the preceding code is as follows:

Student Name :  ram         Total Marks : 375
Student Name : amit Total Marks : 323
Student Name : vijay Total Marks : 473
Student Name : satvik Total Marks : 386
Student Name : akshat Total Marks : 353
Student Name : rishi Total Marks : 407
Student Name : tushar Total Marks : 359

In the following example, we will print four fields of each record, one field per line from /etc/passwd file to user info from the user database. The body of the loop is a compound statement containing an action statement and the other conditional if...else...if statement. First, the value of i is initialized to 1. Then the while statement tests whether i is less than or equal to 7. If this is true, statements given inside the while body are executed. Then as last action statement in the while body, i is incremented using i++ and the loop repeats. The loop terminates when i attains the value of 8:

$ vi dowhile3.awk

BEGIN {
FS=":"
}
{

i=1
while ( i<=7 )
{
if ( i == 1 )
{ printf "User Name : %s ", $i }
else if ( i == 3 )
{ printf "UID : %s ", $i }
else if ( i == 6 )
{ printf "Home Directory : %s ", $i }
else if ( i == 7 )
{ printf "Default Shell : %s ", $i
printf "************************************ "
}
i++
}
}

$ awk -f dowhile3.awk /etc/passwd

The output on execution of the preceding code is as follows:

User Name          : root
UID : 0
Home Directory : /root
Default Shell : /usr/bin/zsh
************************************
User Name : daemon
UID : 1
Home Directory : /usr/sbin
Default Shell : /usr/sbin/nologin
************************************
User Name : bin
UID : 2
Home Directory : /bin
Default Shell : /usr/sbin/nologin
************************************
User Name : sys
UID : 3
Home Directory : /dev
Default Shell : /usr/sbin/nologin
************************************
User Name : sync
UID : 4
Home Directory : /bin
Default Shell : /bin/sync
************************************
User Name : games
UID : 5
Home Directory : /usr/games
Default Shell : /usr/sbin/nologin
************************************
User Name : man
UID : 6
Home Directory : /var/cache/man
Default Shell : /usr/sbin/nologin
************************************
..........
..........
..........
************************************
User Name : practice
UID : 1001
Home Directory : /home/practice
Default Shell : /bin/bash
************************************
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.161.225