in reply to What could be the exact usage of While - continue loop
See that the i line is not always printed because it depends on if checking, while j line is always printed regardless what happens inside the while block*. This won't happen if printing j line is put inside the block as i.my $i = my $j = 0; while ($i++ < 5) { next if $i == 2 || $i == 4; print "i:$i\n"; } continue { $j++; printf "j:$j\n"; }
But frankly, I never use continue myself in practical. It exists for some reason regarding the for loop (see Programming Perl). From the doc:
"last", "next", or "redo" may appear within a "continue" block. "last" and "redo" will behave as if they had been executed within the main block. So will "next", but since it will execute a "continue" block, it may be more entertaining.Some monk commented on that emphasized text but I can't recall the node.
* Unless redo take place, as shown by GrandFather below. The same thing with last. Consider,
my $yodda = 0; while ($yodda < 6) { last if $yodda == 5; redo if ++$yodda & 1; print "Even is "; } continue { print "$yodda\n"; }
Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What could be the exact usage of While - continue loop
by GrandFather (Saint) on May 09, 2007 at 04:37 UTC | |
by naikonta (Curate) on May 09, 2007 at 04:58 UTC |