Quick Perl question: when going through a loop (say a while loop), what is the difference between a next
and continue
command? I thought both just skip to the next iteration of the loop.
Advertisements. The Perl next statement starts the next iteration of the loop. You can provide a LABEL with next statement where LABEL is the label for a loop. A next statement can be used inside a nested loop where it will be applicable to the nearest loop if a LABEL is not specified.
A continue BLOCK, is always executed just before the conditional is about to be evaluated again. A continue statement can be used with while and foreach loops. A continue statement can also be used alone along with a BLOCK of code in which case it will be assumed as a flow control statement rather than a function.
next of Perl is the same as the continue in other languages and the last if Perl is the same as the break of other languages. redo probably does not have its counterpart. next will jump to the condition and if it is true Perl will execute the next iteration. last will quite the loop.
You can skip to the next iteration of a loop with the next built-in. Since you are reading line by line, that's all you need to do. For checking if the character is present, use a regular expression. That's done with the m// operator and =~ in Perl.
The continue
keyword can be used after the block of a loop. The code in the continue
block is executed before the next iteration (before the loop condition is evaluated). It does not affect the control-flow.
my $i = 0; when (1) { print $i, "\n"; } continue { if ($i < 10) { $i++; } else { last; } }
Is almost equivalent to
foreach my $i (0 .. 10){ print $i, "\n"; }
The continue
keyword has another meaning in the given
-when
construct, Perl's switch
-case
. After a when
block is executed, Perl automatically break
s because most programs do that anyway. If you want to fall through to the next cases the continue
has to be used. Here, continue
modifies the control flow.
given ("abc") { when (/z/) { print qq{Found a "z"\n}; continue; } when (/a/) { print qq{Found a "a"\n}; continue; } when (/b/) { print qq{Found a "b"\n}; continue; } }
Will print
Found a "a" Found a "b"
The next
keyword is only available in loops and causes a new iteration incl. re-evaluation of the loop condition. redo
jumps to the beginning of a loop block. The loop condition is not evaluated.
Execution of next statment will skip executing rest of the statments in the loop for that pirticular iteration.
Statments in the continue block will execute for each and every iteration, irrespective of loop executed as usual or loop need to terminate the pirticular iteration by encountring the next statment. example with out continue block:
my $x=0; while($x<10) { if($x%2==0) { $x++; #incrementing x for next loop when the condition inside the if is satisfied. next; } print($x."\n"); $x++; # incrementing x for the next loop }
In above example incrementation of x need to be written 2 times. But if we use continue statment which can hold the statments that needed to be executed all the times we can increment x only one time inside continue loop.
my $x=0; while($x<10) { if($x%2==0) { next; } print($x."\n"); } continue { $x++; }
output in both the cases are 1,3,5,7,9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With