Perl programming: continue block

I just started to learn Perl scripting language and asked a question.

In Perl, what is the logical reason for continueblocking work with loops whileand do while, but not with loops for?

+3
source share
3 answers

You can use the block continuewherever it makes sense: with loops while, untiland foreach, as well as the "base" blocks - blocks that are not part of another statement. Note that you can use the keyword forinstead foreachto construct an iteration of the list, and of course in this case you can have a block continue.

, for (;;) continue - ?

continue do { ... } while ..., (do - , BLOCK , while - ). , ( ), :

do {
    {
        ...;
        continue if $bad;
        ...;
    }
    continue {
        ...; # clean up
    }
} while $more;
+2

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

, ( foreach), , , for C.

, for - , . for (initialization; condition; continue), . , for foreach, :

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

.

+7

I suspect the block is continuenot used in loops for, as it is exactly equivalent to the third loop expression for(increment / decrement, etc.)

eg. The following blocks of code are basically equivalent:

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

$i = 0;
while ($i < 10)
{
}
continue
{
    $i++;
}
+4
source

All Articles