ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
4.3 The while/until StatementNo programming language would be complete without some form of iteration[2] (repeated execution of a block of statements). Perl can iterate using the while (
To execute this 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 until ( Note that in both the 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 StatementThe 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 { statement_1; statement_2; statement_3; } while some_expression;
Perl executes the statements in the As with a normal $stops = 0; do { $stops++; print "Next stop? "; chomp($location = <STDIN>); } until $stops > 5 || $location eq 'home'; |