Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl programming: continue block

I have just started learning Perl scripting language and have a question.

In Perl, what is the logical reason for having continue block work with while and do while loops, but not with for loop?

like image 458
madCode Avatar asked Aug 07 '26 22:08

madCode


1 Answers

From http://perldoc.perl.org/functions/continue.html

If there is a continue BLOCK attached to a BLOCK (typically in a while or foreach ), it is always executed just before the conditional is about to be evaluated again, just like the third part of a for loop in C.

Meaning that in the for loop, the third argument IS the continue expression, e.g. for (initialization; condition; continue), so therefore it is not needed. On the other hand, if you use for in the foreach style, such as:

for (0 .. 10) {
    print "$i\n";
} continue { $i++ }

It will be acceptable.

like image 183
TLP Avatar answered Aug 09 '26 16:08

TLP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!