Perl Programming/Keywords/while

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Previous: when Keywords Next: write

The while keyword[edit | edit source]

while is the statement that uses the EXPRESSION, called the condition, to loop through a block until the condition is true. It is the opposite of the until statement.

Syntax[edit | edit source]


Examples[edit | edit source]

The code
$a = 5;
print $a++ while $a < 10;

prints the numbers 5 to 9 consecutively:


The following two print statements
$i = 5;
print $i++ while $i <= 10;
$j = 5;
print $j++ until $j >  10;

return the same:


Previous: when Keywords Next: write