next up previous contents index
Next: Conditionals Up: Loops Previous: General for ( )   Contents   Index


while () {}

Essentially any loop one encounters can be phrased in terms of a for loop. However, sometimes occasions arise where a while() syntax may be preferable:
  my $count = 10;
  while ($count > 0) {
     print qq{\$count is now $count\n};
     $count--;
  }
In a while() loop, the statements occuring within the enclosing left and right curly braces are executed while the statement within the enclosing round brackets defining the loop is true.

In a while loop, if the initial condition happens to be false, the enclosing statements will not be executed at all. If for the problem at hand it is desired to execute the statements at least once, the following construction can be used:

  my $count = 10;
  do {
    print qq{\$count is now $count\n};
    $count--;
  } while ($count > 0);
You can also use until rather then while in this context, which will execute the block of statements until the specified condition is true:
  my $count = 10;
  do {
    print qq{\$count is now $count\n};
    $count--;
  } until ($count <= 0);

There is one further special form of a while loop that we will encounter:

my $count = 0;
while (1) {
    $count++;
    next if ($count < 3);
    last if ($count > 10);
    print "$count\n";
}
In this form, the condition to be satisfied to continue the while loop is given as "1", which is always true (in the sense of ``truthfulness'', "0" and an empty string are false). Without further action such a loop would be endless (press Control-C to exit a program), but here we put in a last statement, which means that this is the last time through the loop. We qualify this statement by demanding that $count > 10 in order for the loop to terminate (and also demand that $count >= 3 in order for the next series of statements in the loop be carried out).

Note that loops can be nested:

for (my $i=0; $i<10; $i++) {
  for (my $j=20; $j<30; $j=$j+2) {
    print qq{\$i=$i and \$j=$j\n};
  }
}
In this example $i is first set to 0, and the inner loop is executed for the specified range of $j. $i is then increased by 1, and the inner $j loop is then iterated over again, and so on, until the outer $i loop finishes. This example also illustrates an important style consideration in programming - although not required, indenting the loops as indicated above helps to maintain readability of the code; the above example is, for Perl, equivalent to
for (my $i=0; $i<10; $i++) { for (my $j=20; $j<30; $j=$j+2) 
{ print qq{\$i=$i and \$j=$j\n}; } }
but the latter is much harder to understand exactly what is being looped over and at what point the statements are being executed.

The next and last statements operate on the inner-most loop in which they occur. In the case of nested loops, though, you may want to break or continue on an outer loop. For this, you can label the loops, and then use next LABEL or last LABEL to refer explicitly to the loop you want to affect. For example, in

ONE: for (my $i=0; $i<5; $i++) {
  TWO: for (my $j=0; $j<4; $j++) {
      last ONE if ($i > 1);
      print qq{\$i is $i and \$j is $j\n};
  }
}
the last ONE; statement will cause the for loop involving the variable $i to break if the condition $i > 1 is met.
next up previous contents index
Next: Conditionals Up: Loops Previous: General for ( )   Contents   Index