< BACKCONTINUE >

B.13 Loops

Loops repeatedly execute the statements in a block until a conditional test changes value. There are several forms of loops in Perl:

while(CONDITION) {BLOCK}
until(CONDITION) {BLOCK}
for(INITIALIZATION ; CONDITION ; RE-INITIALIZATION ) {BLOCK}
foreach VAR (LIST) {BLOCK}
for VAR (LIST) {BLOCK}
do {BLOCK} while (CONDITION)
do {BLOCK} until (CONDITION)

The while loop first tests if the conditional is true; if so, it executes the block and then returns to the conditional to repeat the process; if false, it does nothing, and the loop is over. For example:

$i = 3;
while ( $i ) {
	print "$i\n";
	$i--;
}

This produces the output:

3
2
1

Here's how the loop works. The scalar variable $i is first initialized to 3 (this isn't part of the loop). The loop is then entered, and $i is tested to see if it has a true (nonzero) value. It does, so the number 3 is printed, and the decrement operator is applied to $i, which reduces its value to 2. The block is now over, and the loop starts again with the conditional test. It succeeds with the true value 2, which is printed and decremented. The loop restarts with a test of $i, which is now the true value 1; 1 is printed and decremented to 0. The loop starts again; 0 is tested to see if it's true, and it's not, so the loop is now finished.

Loops often follow the same pattern, in which a variable is set, and a loop is called, which tests the variable's value and then executes a block, which includes changing the value of the variable.

The for loop makes this easy by including the variable initialization and the variable change in the loop statement. The following is exactly equivalent to the preceding example and produces the same output:

for ( $i = 3 ; $i ; $i-- ) {
	print "$i\n";
}

The foreach loop is a convenient way to iterate through the elements in an array. Here's an example:

@array = ('one', 'two', 'three');

foreach $element (@array) {
	print $element\n";
}

This prints the output:

one
two
three

The foreach loop specifies a scalar variable $element to be set to each element of the array. (You may use any variable name or none, in which case the special variable $_ is used automatically.) The array to be iterated over is then placed in parentheses, followed by the block. You can use for instead of foreach as the name of this loop, with identical behavior.

The first time through the loop, the value of the first element of the array is assigned to the foreach variable $element. On each succeeding pass through the loop, the value of the next element of the array is assigned to the foreach variable $element. The loop exits after it has reached the end of the array.

There is one important point to make, however. If in the block you change the value of the loop variable $element, the array is changed, and the change stays in effect after you've left the foreach loop. For example:

@array = ('one', 'two', 'three');

foreach $element (@array) {
	$element = 'four';
}

foreach $element (@array) {
	print $element,"\n";
}

produces the output:

four
four
four

In the do-until loop, the block is executed before the conditional test, and the test succeeds until the condition is true:

$i = 3;
do {
	print $i,"\n";
	$i--;
} until ( $i );

This prints:

3

In the do-while loop, the block is executed before the conditional test, and the test succeeds while the condition is true:

$i = 3;
do {
	print $i,"\n";
	$i--;
} while ( $i );

This prints:

3
2
1
< BACKCONTINUE >

Index terms contained in this section

$ (dollar sign)
      $_ variables
conditional statements
      expressions in loops
do-until loops
do-while loops
for loops
foreach loops
loops
Perl
      loops
variables
      testing and changing value in loops
while loops

© 2002, O'Reilly & Associates, Inc.