6.17. Conditional Statements
The conditional statements in awk were borrowed from the C language. They are used to control the flow of the program in making decisions.
6.17.1 if Statements
Statements beginning with the if construct are action statements. With conditional patterns, the if is implied; with a conditional action statement, the if is explicitly stated, and followed by an expression enclosed in parentheses. If the expression evaluates true (nonzero or non-null), the statement or block of statements following the expression is executed. If there is more than one statement following the conditional expression, the statements are separated either by semicolons or a newline, and the group of statements must be enclosed in curly braces so that the statements are executed as a block.
FORMAT
if (expression) {
statement; statement; ...
}
Example 6.127.
1 % nawk '{if ( $6 > 50 ) print $1 "Too high"}' filename
2 % nawk '{if ($6 > 20 && $6 <= 50){safe++; print "OK"}}' filename
EXPLANATION
In the if action block, the expression is tested. If the value of the sixth field ($6) is greater than 50, the print statement is executed. Because the statement following the expression is a single statement, curly braces are not required. (filename represents the input file.) In the if action block, the expression is tested. If the sixth field ($6) is greater than 20 and the sixth field is less than or equal to 50, the statements following the expression are executed as a block and must be enclosed in curly braces.
6.17.2 if/else Statements
The if/else statement allows a two-way decision. If the expression after the if keyword is true, the block of statements associated with that expression are executed. If the first expression evaluates to false or 0, the block of statements after the else keyword is executed. If multiple statements are to be included with the if or else, they must be blocked with curly braces.
FORMAT
{if (expression) {
statement; statement; ...
}
else{
statement; statement; ...
}
}
Example 6.128.
1 % nawk '{if( $6 > 50) print $1 " Too high" ;\
else print "Range is OK"}' filename
2 % nawk '{if ( $6 > 50 ) { count++; print $3 } \
else { x+5; print $2 } }' filename
EXPLANATION
If the first expression is true, that is, the sixth field ($6) is greater than 50, the print function prints the first field and Too high; otherwise, the statement after the else, Range is OK, is printed. If the first expression is true, that is, the sixth field ($6) is greater than 50, the block of statements is executed; otherwise, the block of statements after the else is executed. Note that the blocks are enclosed in curly braces.
6.17.3 if/else and else if Statements
The if/else and else if statements allow a multiway decision. If the expression following the keyword if is true, the block of statements associated with that expression is executed and control starts again after the last closing curly brace associated with the final else. Otherwise, control goes to the else if and that expression is tested. When the first else if condition is true, the statements following the expression are executed. If none of the conditional expressions test true, control goes to the else statements. The else is called the default action because if none of the other statements are true, the else block is executed.
FORMAT
{if (expression) {
statement; statement; ...
}
else if (expression){
statement; statement; ...
}
else if (expression){
statement; statement; ...
}
else{
statement
}
}
Example 6.129.
(In the Script)
1 {if ( $3 > 89 && $3 < 101 ) Agrade++
2 else if ( $3 > 79 ) Bgrade++
3 else if ( $3 > 69 ) Cgrade++
4 else if ( $3 > 59 ) Dgrade++
5 else Fgrade++
}
END{print "The number of failures is" Fgrade }
EXPLANATION
The if statement is an action and must be enclosed in curly braces. The expression is evaluated from left to right. If the first expression is false, the whole expression is false; if the first expression is true, the expression after the logical AND (&&) is evaluated. If it is true, the variable Agrade is incremented by 1. If the first expression following the if keyword evaluates to false (0), the else if expression is tested. If it evaluates to true, the statement following the expression is executed; that is, if the third field ($3) is greater than 79, Bgrade is incremented by 1. If the first two statements are false, the else if expression is tested, and if the third field ($3) is greater than 69, Cgrade is incremented. If the first three statements are false, the else if expression is tested, and if the third field is greater than 59, Dgrade is incremented. If none of the expressions tested above is true, the else block is executed. The curly brace ends the action block. Fgrade is incremented.
|