Loop controls in Perl

Walden Systems Geeks Corner Loops in Perl tutorial how to Ruherford NJ New Jersey NYC New York North Bergen County
Perl 5 is a highly capable, feature-rich programming language with over 30 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.

Loops are used for executing a set of statements repeatedly until a particular condition is satisfied. There may be a situation when we need to execute a block of code several times. In general, statements are executed sequentially. Programming languages provide various control structures that allow for more complicated execution paths.

Perl has five types to handle looping. The while loop tests for a condition and repeats a code block while the condition is true. The until loop is similar to the while loop except that it quits repeating when the condition becomes true. The do...while loop is similar to the while loop except it tests the condition after running the block of code. The for loop executes a block of code multiple times. The foreach loop iterates over list values and sets the variable to each element of the list one at a time. Any of these loops can be nested in another. For more detailed information on the various loops, click here.

while loop

$num = 0 ;
while ( $num < 3 )
{
   printf "$num" ;
   $num = $num + 1 ;
}



until loop
$num = 0 ;
until ( $num > 3 )
{
   printf "$num" ;
   $num = $num + 1 ;
}



do...while loop
$num = 0 ;
do
{
   printf "$num" ;
   $num = $num + 1 ;
}
until ( $num < 3 )



for loop
$num = 0 ;
for ( $num = 0; $num < 3; $num = $num + 1 )
{
   printf "$num" ;
}



foreach loop
@num = ( 1, 2 ) ; foreach $element ( @num ) { printf "$element" ; }



Control statements

Control statements change the execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Perl provides four control statements. The next statement stops executing the body and retests the condition. The last statement leaves the loop. The continue statement always executes the continue block just before the condition is reevaluated. The redo statement restarts the loop block without reevaluating the condition. In the case of the redo statement, if there is a continue block, it is not executed.

next statement

$num = 0 ;
while ( $num < 10 )
{
   if ( $num == 4 )
   {
      $num = $num + 1 ;
      next ;
   }
   printf "$num" ;
   $num = $num + 1 ;
}



The output is 1, 2, 3, 5, 6, 7, 8, 9


last statement
$num = 0 ;
while ( $num < 10 )
{
   if ( $num == 4 )
   {
      $num = $num + 1 ;
      last ;
   }
   printf "$num" ;
   $num = $num + 1 ;
}



The output is 1, 2, 3


continue statement
$num = 0 ;
while ( $num < 5 )
{
   printf "$num" ;
}
continue
{
   $num = $num + 1 ;
}



The output is 1, 2, 3, 4


continue statement
$num = 0 ;
while ( $num < 5 )
{
   if( $num == 3 )
   {
      $num = $num + 1 ;
      redo ;
   }
   printf "$num" ;
}


The output is 1, 2, 4