Perl | next operator

next operator in Perl skips the current loop execution and transfers the iterator to the value specified by the next. If there’s a label specified in the program, then execution skips to the next iteration identified by the Label.

Syntax : next Label

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w
  
# Perl Program to find the frequency
# of an element
  
@Array = ('G', 'E', 'E', 'K', 'S');
$c = 0;
foreach $key (@Array)
{
    if($key eq 'E'
    {
        $c = $c + 1;    
    }
    next;
}
  
print "Frequency of E in the Array: $c";

chevron_right


Output:

Frequency of E in the Array: 2

 
Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
$i = 0;
  
# label for outer loop
outer:
while ( $i < 3 ) {
  
    $j = 0;
    while ( $j < 3 ) {
          
        # Printing values of i and j
        print "i =  $i and j =  $j\n";
          
        # Skipping the loop if i==j
        if ( $j == $i ) {
              
            $i = $i + 1;
            print "As i == j, hence going back to outer loop\n\n";
              
            # Using next to skip the iteration
            next outer;
        }
        $j = $j + 1;
    }
  
    $i = $i + 1;
  
}# end of outer loop

chevron_right


Output:

i =  0 and j =  0
As i == j, hence going back to outer loop

i =  1 and j =  0
i =  1 and j =  1
As i == j, hence going back to outer loop

i =  2 and j =  0
i =  2 and j =  1
i =  2 and j =  2
As i == j, hence going back to outer loop


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.