next up previous contents
Next: The foreach loop Up: Control Structures Previous: Compound Statements   Contents


The while statement

The if statement is useful when you want to evaluate an expression and perform some action based on the validity of that expression. But notice that once the block of statements associated with the expression are evaluated, control returns to the line after the if (and its elsif and else clauses, if there are any). In many common situations, it is necessary to repeatedly execute statements, perhaps with the value(s) of one or more variables changing each time. Perl offers a number of such looping operators. Unlike the if statement, an expression will be evaluated repeatedly, and statements associated with the loop will be executed if the expression is true. The simplest such looping construct is the while loop.


  

while (expression)
block of statements
Perl evaluates the expression before entering the while loop; if it is false, control transfers to the point after the while statement. (All of the considerations about indentation presented in Section 5.3 are equally true when applied to the while statement.) If the expression is true, the block of statements is executed, and then the expression is evaluated. Thus, as long as the expression is true, the statements will continue to be evaluated over and over. To make the while loop terminate, it's usually necessary to change the value of the variables involved in the expression which is being evaluated, or to use one of the statements described in Section 5.8 to modify the loop's behavior.

In some cases, it's easier to think of continuing to execute the statements associated with a loop if some expression is false, instead of the case of the while loop, which continues when the expression is true. In this case, perl provides the until loop. As its name implies, the statements associated with an until loop continue to be executed until the expression is true -- thus, as long as the expression is false, execution continues. There's no difference in efficiency, but you may sometimes find one form preferable to the other.

As a simple illustration, suppose we have an array called @prices, which represents the prices of some items, and we wish to sum up all of the prices to produce a total price. The following code fragment does the job:

     $n = scalar(@prices);
     $i = $total = 0;
     while($i < $n){
         $total += $prices[$i];
         $i++;
     }
Notice that we wish to continue the loop only as long as our counter variable, $i is less than the number of elements in the array ($n; See Section 4.2 for an explanation of why $n contains the number of elements in @prices). We want the counter to be less than $i because arrays in perl are zero-based (Section 4.1). The loop will therefore stop before we reach the point where we would be trying to access the element with an index equal to the number of elements in the array, since that element does not exist.

The same effect can be achieved with an until loop by simply changing the expression to be tested to one which will become false when we try to access an non-existent element. In this case, we stop the loop if the counter variable is greater than or equal to the number of elements in the array.

     $n = scalar(@prices);
     $i = $total = 0;
     until($i >= $n){
         $total += $prices[$i];
         $i++;
     }
The choice between a while loop and a until loop is strictly one of personal taste, but you may find that some loops are a little easier to write and understand using one of the two forms.


next up previous contents
Next: The foreach loop Up: Control Structures Previous: Compound Statements   Contents
Phil Spector 2002-10-18