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 (
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 Sometimes the control expression never lets the loop exit. This case is perfectly legal, and sometimes desired, and is 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 program that is meant to run until the system terminates. |
|