3

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?

pavium
  • 14,808
  • 4
  • 33
  • 50
madCode
  • 3,733
  • 5
  • 26
  • 31
  • 2
    13 years in Perl and I have *never* programmed a continue block. Mainly because the same statements can be placed at the end of the loop. It's basically like a `next`-with-behavior. However, I only `next` when I don't want to do anything anymore with the iteration. And it's much easier to code `for( ; cond() ; iteration())...` and get the same behavior. – Axeman May 11 '11 at 12:44
  • I agree, Axeman. For is certainly more preferable and has the same behavior. Thanks. – madCode May 11 '11 at 14:38
  • `continue` still executes when you use `next`. I use it when I need to break out of an inner loop to the outer loop, but still want to make sure some condition gets executed. Yes, I could do this by placing the thing I want executed after the inner loop and using `next`, but then everything else there will get executed too. And it's just one more thing cluttering up the flow of the program. With `continue` I can just know that it will be executed, and can have some things be skipped if I want. – felwithe Apr 10 '18 at 20:14

3 Answers3

7

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.

TLP
  • 66,756
  • 10
  • 92
  • 149
  • Awesome @TLP! I totally get it. Thanks! – madCode May 11 '11 at 14:38
  • This syntax is correct, but in this context syntax for is interpreted as foreach statement. it would be better if you'll use for as for, and foreach as foreach. you will be prevented by many mistakes. – Znik Feb 05 '16 at 11:41
  • @Znik It is impossible to make mistakes, as `for` and `foreach` is actually aliases to the same function. – TLP Feb 05 '16 at 14:54
  • Can't we simply do : for (0 .. 10) { print "$i\n"; $i++ } Am I missing something here? – Rohit Rane Feb 12 '18 at 04:38
4

I suspect that the continue block isn't used in for loops since it is exactly equivalent to the for loop's 3rd expression (increment/decrement, etc.)

eg. the following blocks of code are mostly equivalent:

for ($i = 0; $i < 10; $i++)
{
}

$i = 0;
while ($i < 10)
{
}
continue
{
    $i++;
}
Hasturkun
  • 35,395
  • 6
  • 71
  • 104
2

You can use a continue block everywhere it makes sense: with while, until and foreach loops, as well as 'basic' blocks -- blocks that aren't part of another statement. Note that you can use the keyword for instead of foreach for the list iteration construct, and of course you can have a continue block in that case.

As everybody else said, for (;;) loops already have a continue part -- which one would you want to execute first?

continue blocks also don't work with do { ... } while ... because syntactically that's a very different thing (do is a builtin function taking a BLOCK as its argument, and the while part is a statement modifier). I suppose you could use the double curly construct with them (basic block inside argument block), if you really had to:

do {
    {
        ...;
        continue if $bad;
        ...;
    }
    continue {
        ...; # clean up
    }
} while $more;
LHMathies
  • 2,384
  • 16
  • 21