Perl executes code statements in sequential order.

Perl provides loop control to change execution order other than sequential order.

Perl Control statements provides multiple features. Sometimes, You want to do below things

  • How to exit from loop in Perl
  • How to skip loop execution and run loop again?
  • How to continue loop execution in Perl.

Loop Next Control Operator

next statement skip executing the loop iteration. next used in for and while loops

next [label]

next is an keyword and label is an optional.This is an example for printing Odd numbers usingnext` label.

my $num=0;
while($num<20)
{
    if($num%2==0)
    {
        $num++; 
        next;
    }
    print($num." ");
    $num++;
}   

Output:

1 3 5 7 9 11 13 15 17 19 

Loop last Control Operator:

last keyword used to exit an loop execution.

Loop continue Control Operator

Loop redo Control Operator:

redo operator skips everything in the loop and evaluates the condition again

Loop goto Control Operator