next up previous contents
Next: Control within Loops Up: Control Structures Previous: The foreach loop   Contents


The for statement

Perl's for loop is modeled directly after the for loop in C. It provides for a great deal of flexibility, but takes a little getting used to if you're used the simpler looping constructs of languages like Fortran or Basic. The basic form of the for statement is as follows:

		for(expression-1;expression-2;expression-3)

block of statements
The two semicolons in the for expression are required, but each of the expressions is optional. Additionally, multiple statements can be included in the expressions by separating them with commas.

Perhaps the easiest way to understand what the for statement is doing is to see its equivalent written using a while statement:


		expression-1

while(expression-2)
block of statements
expression-3
Thus expression-1 serves as an initialization for the loop, expression-2 is tested before each execution of the loop, and, if it's true, the loop will continue, and expression-3 is performed at the bottom of the loop to update conditions before the next evaluation of expression-1 is carried out.

While the foreach loop is ideal for processing the elements of a single array, when you are working with multiple arrays it is often necessary to use a for loop instead of a foreach loop. Suppose we have two arrays: one called @prices which contains the prices of some items, and a second called @taxes, which has the taxes for the items whose prices are in the @prices array. We wish to create a third array, called @sum which contains the sum of the price and the tax for each of the items in the arrays. (Of course, the values in the two arrays must be in exactly the same order, so that when we refer to a particular element of the @prices array through a subscript, the corresponding element in the @taxes array would be the taxes on that particular price.) By starting at zero, and incrementing until we reach a value one less than the number of elements in the array, we can use subscripts to access each element of all the arrays in turn:

     @sum = ()
     for($i=0;$i<scalar(@prices);$i++){
         $sum[$i] = $prices[$i] + $taxes[$i];
     }

Some programmers prefer to put all the initialization statments within expression-1, separated by commas:

     for(@sum = (),$i=0;$i<scalar(@prices);$i++){
         $sum[$i] = $prices[$i] + $taxes[$i];
     }
Another alternative would be to use the ``$#'' syntax to find the highest index of any element within the array(Section 4.1), and to explicitly loop up until that index:
     @sum = ()
     for($i=0;$i<=$#prices;$i++){
         $sum[$i] = $prices[$i] + $taxes[$i];
     }
All of these variations accomplish exactly the same thing; it's up to you to decide which you consider to be the best to acheive a particular goal you have in mind.


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