Perl redo statement

Perl redo statement Perl loop

The Perl redo statement goes straight to the first line of the loop body and starts repeating the loop, and the statement after the redo statement is no longer executed, and the continue statement block is no longer executed.

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

Grammar

The syntax format looks like this:

redo [LABEL]

WHERE LABEL is optional.

The redo statement with the label modifier LABEL indicates that the loop control process is directed to the first line of the statement block associated with the label modifier LABEL, instead of executing the statement after the redo statement and the continue statement block;

A redo statement without the label modifier LABEL indicates that the loop control process is directed to the first line of the current statement block to begin execution, instead of executing the statement after the redo statement and the continue statement block;

If it is in a for loop or with acontinue statement block, either the incremental list and the continue statement block in the for loop are no longer executed;

Flow chart

Perl redo statement

Instance

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

The above procedure is performed and the output is:

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

Perl redo statement Perl loop