Learning Perl

Learning PerlSearch this book
Previous: 4.2 The if/unless StatementChapter 4
Control Structures
Next: 4.4 The for Statement
 

4.3 The while/until Statement

No programming language would be complete without some form of iteration[2] (repeated execution of a block of statements). Perl can iterate using the while statement:

while (some_expression) {
    statement_1;
    statement_2;
    statement_3;
}

[2] That's why HTML is not a programming language.

To execute this while statement, Perl evaluates the control expression (some_expression in the example). If its value is true (using Perl's notion of truth), the body of the while statement is evaluated once. This is repeated until the control expression becomes false, at which point Perl goes on to the next statement after the while loop. For example:

print "how old are you? ";
$a = <STDIN>;
chomp($a);
while ($a > 0) {
    print "At one time, you were $a years old.\n";
    $a--;
}

Sometimes it is easier to say "until something is true" rather than "while not this is true." Once again, Perl has the answer. Replacing the while with until yields the desired effect:

until (some_expression) {
    statement_1;
    statement_2;
    statement_3;
}

Note that in both the while and the until form, the body statements will be skipped entirely if the control expression is the termination value to begin with. For example, if a user enters an age less than zero for the program fragment above, Perl skips over the body of the loop.

It's possible that the control expression never lets the loop exit. This is perfectly legal, and sometimes desired, and thus not considered an error. For example, you might want a loop to repeat as long as you have no error, and then have some error-handling code following the loop. You might use this for a daemon that is meant to run until the system crashes.

4.3.1 The do {} while/until Statement

The while/until statement you saw in the previous section tests its condition at the top of every loop, before the loop is entered. If the condition was already false to begin with, the loop won't be executed at all.

But sometimes you don't want to test the condition at the top of the loop. Instead, you want to test it at the bottom. To fill this need, Perl provides the do {} while statement, which is just like[3] the regular while statement except that it doesn't test the expression until after executing the loop once.

do {
    statement_1;
    statement_2;
    statement_3;
} while some_expression;

[3] Well, not quite just like; the loop control directives explained in Chapter 9, Miscellaneous Control Structures, don't work for the bottom-testing form.

Perl executes the statements in the do block.When it reaches the end, it evaluates the expression for truth. If the expression is false, the loop is done. If it's true, then the whole block is executed one more time before the expression is once again checked.

As with a normal while loop, you can invert the sense of the test by changing do {} while to do {} until. The expression is still tested at the bottom, but its sense is reversed. For some cases, especially compound ones, this is the more natural way to write the test.

$stops = 0;
do {
    $stops++;
    print "Next stop? ";
    chomp($location = <STDIN>);
} until $stops > 5 || $location eq 'home';


Previous: 4.2 The if/unless StatementLearning PerlNext: 4.4 The for Statement
4.2 The if/unless StatementBook Index4.4 The for Statement