Summary of Perl Looping Constructs

#!/usr/bin/perl -w

#############  Summary of Perl Loops and Flow Control Constructs   #########

for($i = 10; $i >= 0; $i--){  #### For is good for backwards counting!
     sleep(1);
     print "$i seconds\n";
}
print "Kaboom!\n\n";

print "The even numbers between 0 and 20 are:\n";
print "--------------------------------------\n";
for ($i = 0; $i <= 20; $i += 2){ #####  For is good for counting in steps
    print "$i\n";                #####  greater than one!
} 
print "\n";


#####################  Demo of Labeled Blocks  ###########################
LINE: while (print(">"), $line = <STDIN>, $line !~ /^\s*quit\s*$/i)
      {
           @words = $line =~ m/[a-z]+/g;
           while ($word = shift(@words))
           {
                next LINE if $word eq "foobar";
                print "$word\n";
           }
       }

###############  Conversion of the Above While to an Until  ###############
NEWLINE: until (print(">>>"), $line = <STDIN>, $line =~ /^\s*quit\s*$/i)
         {
              @words = $line =~ m/[a-z]+/g;
              until (!defined($word = shift @words)) 
              {
                   next NEWLINE if $word eq "foobar";
                   print "$word\n";
              }
          }


INT:  foreach $i (1..5)
      {
           foreach $j (1..5)
           {
                next INT if $i == $j;
                print "$i $j\n";
           }
      }                     

  
   $i = 0;
   NUMBER: while(1)
           {
                $i++;
                print "$i\n";
                last NUMBER if $i > 5;
           }
######################  Do...Whiles and Do...Untils  ######################
do      ############  Perl does not acknowledge this as a loop if
{       ############  you do a next or last.
      print "Enter an int: ";
      $line = <STDIN>;
      next if $line == 10;   ######  Fatal error!
}while ($line < 10);
print "\n";



do      ############  Perl does not acknowledge this as a loop if
{       ############  you do a next or last.
      print "(Second do...while) Enter an int: ";
      $line = <STDIN>;
      last if $line == 10;  ######  Fatal error if this statement executed!!
}until ($line >= 10);


##########################  The Redo Directive  ##########################

$i = 0;
FOO:  while($i < 5)
      {
          $i++;
          redo FOO if $i == 3;
          print "$i\n";
      }
##########################  Sample Program Session  #######################

$ loops.pl
10 seconds
9 seconds
8 seconds
7 seconds
6 seconds
5 seconds
4 seconds
3 seconds
2 seconds
1 seconds
0 seconds
Kaboom!

The even numbers between 0 and 20 are:
--------------------------------------
0
2
4
6
8
10
12
14
16
18
20

>  what is a perl
what
is
a
perl

>it is a foobar which is snafued
it
is
a
>   quit

>>>what is a perl
what
is
a
perl

>>>it is a foobar which is snafued
it
is
a
>>>quit

2 1   #####  Identical pairs not printed!!
3 1
3 2
4 1
4 2
4 3
5 1
5 2
5 3
5 4

1
2
3
4
5
6

Enter an int: 22

(Second do...while) Enter an int: 22

1
2    #######  Print of "3" skipped because of redo.
4
5
##########  Part of Another Session where Do...While Blows Up  #############

Enter an int: 10
Can't "next" outside a block at loops.pl line 64, <STDIN> chunk 3.
$