next up previous contents
Next: Compound Statements Up: Control Structures Previous: Combining Logical Expressions   Contents

if and unless statements

The form of the if statement is as follows:



  if expression
  block of statements
  else
  block of statements


For a definition of block of statements see Section 5.1. In the first case, the else and its corresponding block of statements are optional.

In addition to the if statement, perl also offers the unless statement. It can be used exactly like if; the only difference is that now the corresponding block of statements will be executed if the expression if false.

When there is more than one alternative after an if statement, perl provides the elsif statement, whose basic form is illustrated below.

  if expression
  block of statements
  elsif expression
  block of statements
  elsif expression
  block of statements
  $\ldots$
  else
  block of statements


While it makes no difference whatsoever to perl itself, the indentation scheme suggested by the above descriptions is very helpful in allowing you to visualize what your program does, and, more importantly it helps other perl programmers who may want to use or modify your programs understand the logical flow of your program. When reading a program, and encountering an if statement, it's very helpful if you can quickly determine the statements which will be executed if the expression is true (the indented statements), as well as those which will be executed if the expression is false (the statements which are at the same level of indentation as the if itself. Since statements like the if statement can be nested, developing a consistent indentation scheme is very important if you want to be able to quickly understand what your program (or someone else's) is trying to do.

It's important to realize that, while the expression of an if statement must always be evaluated, the same is not true of expressions associated with elsif statements. Perl will only evaluate expressions until it finds one which is true -- it then executes the corresponding block of statements, and transfers execution to the next line after the entire group of if/elsif/else statements. Suppose we wish to print a series of numbers, with one asterisk if they are less than 0, two asterisks if they are between 0 and 10, and three asterisks if the number is greater than 10. The following code fragment could be used:

     if ($x < 0){
         print "$x *";
     }
     elsif ($x < 10){
         print "$x **";
     }
     else{
         print "$x ***";
     }
Notice that, in the second expression ($x < 10), there was no need to insure that $x was greater than 0. If $x was not greater than 0, the initial expression ($x < 0) would have been true; its associated block of statements would have been executed, and the rest of the if/elsif/else construction would have been skipped. Similarly, there's no need to check if $x is greater than 10 in the final else clause -- if it was not greater than 10, one of the previous expressions would have been true, and the else clause would never be executed. Whenever you're testing a number of alternatives of which exactly one and only one can be true, it's easier and more efficient to use a series of if and elsif statements rather than several separate if statements.


next up previous contents
Next: Compound Statements Up: Control Structures Previous: Combining Logical Expressions   Contents
Phil Spector 2002-10-18