Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - difference between 'next' and 'continue'?

Tags:

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.

like image 932
JDS Avatar asked Aug 22 '12 21:08

JDS


People also ask

What does next mean in Perl?

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.

What is continue in Perl?

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.

How does next statement differ from last statement in Perl?

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.

How do I skip an iteration in Perl?

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.


2 Answers

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 breaks 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.

like image 157
amon Avatar answered Sep 21 '22 17:09

amon


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

like image 43
Suneeldatta Kolipakula Avatar answered Sep 20 '22 17:09

Suneeldatta Kolipakula