Condition Statement

Conditionals are put to use in IF THEN ELSE. The following are acceptable uses of IF:
 
        if (EXPRESSION ) { 
                STATEMENTS; 
        } 
         
        if (EXPRESSION ) { 
                # executed if true 
                STATEMENTS; 
        } ELSE { 
                # executed if false 
                STATEMENTS; 
        } 
 
        if (EXPRESSION ) { 
                STATEMENTS; 
        } elsif { 
                STATEMENTS; 
        # optional additional ELSIF's 
        } else { 
                STATEMENTS; 
        }
Here is an example:
   if($x==14){
           print("\$x is 14");
   } elsif($x==15){
           print("\$x is 15");
   } else{ print("\$x is not 14 or 15");
   }
Unlike C, the braces are not optional - requiring braces avoids the dangling else problem

Perl has no SWITCH statement - it can be imitated several ways, including using ELSIF for each possible case.


Previous PageTable of ContentNext Page