Perl executes code statements in sequential order.

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

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

  • How to exit from a loop in Perl
  • How to skip loop execution and run the 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 a keyword and label is optional.This is an example of printing Odd numbers using thenext` 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 a loop execution.

Loop continues Control Operator

Loop redo Control Operator:

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

Loop goto Control Operator