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:
@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" ;
|
Output:
Frequency of E in the Array: 2
Example 2:
$i = 0;
outer:
while ( $i < 3 ) {
$j = 0;
while ( $j < 3 ) {
print "i = $i and j = $j\n" ;
if ( $j == $i ) {
$i = $i + 1;
print "As i == j, hence going back to outer loop\n\n" ;
next outer;
}
$j = $j + 1;
}
$i = $i + 1;
}
|
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
Last Updated :
01 Apr, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...