Easy Tutorial
❮ Perl While Loop Perl Directories ❯

Perl redo Statement

Perl Loops

The Perl redo statement directly restarts the current iteration of the loop from the first line of the loop body, skipping any statements following the redo statement and the continue block.

The continue statement can be used in while and foreach loops.

Syntax

The syntax is as follows:

redo [LABEL]

Where LABEL is optional.

A redo statement with the LABEL modifier directs the loop control flow to the first line of the block associated with the LABEL modifier, skipping any statements following the redo statement and the continue block.

A redo statement without the LABEL modifier directs the loop control flow to the first line of the current block, skipping any statements following the redo statement and the continue block.

In a for loop or if a continue block is present, the increment list in the for loop and the continue block are not executed.

Flowchart

Example

#/usr/bin/perl

$a = 0;
while($a < 10){
   if( $a == 5 ){
      $a = $a + 1;
      redo;
   }
   print "a = $a\n";
}continue{
   $a = $a + 1;
}

Executing the above program will produce the following output:

a = 0
a = 1
a = 2
a = 3
a = 4
a = 6
a = 7
a = 8
a = 9

Perl Loops

❮ Perl While Loop Perl Directories ❯