Perl has four main types of loop i.e: while, until, for, foreach.
Each of the loops have their own characteristcs, which are described in the
following example:
#!/usr/bin/perl
print "content-type: text/html \n\n";
$i=1;
while ($i<=3) # iterates till the expression is true
{
print "while $i\n";
$i=$i+1;
}
$i=1;
do # the block executes at least once and then tests the expression for next iteration
{
print "do while $i\n";
$i=$i+1;
} while ($i<=0); # iterates till the expression is true
$i=3;
until ($i<1) # iterates till the expression is false
{
print "until $i\n";
$i=$i-1;
}
$i=3;
do # the block executes at least once and then tests the expression for next iteration
{
print "do until $i\n";
$i=$i-1;
} until ($i<=3); # iterates till the expression is false
@array=(1,2,3,4,5); # an array of integers
for ($i=0; $i<$#array; $i++) # iterates till the expression is true
{
print "for $array[$i]\n";
}
foreach $values (@array) # iterates till the end of the supplied list is reached
{
print "foreach $values\n";
}
Output:
-------
while 1
while 2
while 3
do while 1
until 3
until 2
until 1
do until 3
for 1
for 2
for 3
for 4
for 5
foreach 1
foreach 2
foreach 3
foreach 4
foreach 5
