6.17. Conditional Statements

 <  Day Day Up  >  

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 (  > 50 ) print  "Too high"}' filename  2   %  nawk '{if ( > 20 &&   <= 50){safe++; print "OK"}}' filename  

EXPLANATION

  1. 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.)

  2. 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(  > 50) print   " Too high" ;\  else print "Range is OK"}' filename  2   %  nawk '{if (  > 50 ) { count++; print  } \   else { x+5; print  } }' filename  

EXPLANATION

  1. 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.

  2. 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 (  > 89 &&  < 101 ) Agrade++ 2   else if (  > 79 ) Bgrade++ 3   else if (  > 69 ) Cgrade++ 4   else if (  > 59 ) Dgrade++ 5   else Fgrade++     }     END{print "The number of failures is" Fgrade } 

EXPLANATION

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. If none of the expressions tested above is true, the else block is executed. The curly brace ends the action block. Fgrade is incremented.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net