Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

What could be the exact usage of While - continue loop

by jesuashok (Curate)
on May 09, 2007 at 03:21 UTC ( #614288=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

esteemed monks,

#!/usr/bin/perl use strict; my $i = 0 ; while ( $i < 10 ) { print "Hello World\n"; } continue { $i ++; }
#!/usr/bin/perl use strict; my $i = 0 ; while ( $i < 10 ) { print "Hello World\n"; $i ++; }
whatever code written using while ( <condition> ) { } continue { } can be written using simple while ( <condition> ) { }. So, I would like to know the exact usage of while () {} continue {} loop.

I am very sorry to bother you if this has been discussed already in perlmonks.

Comment on What could be the exact usage of While - continue loop
Select or Download Code
Re: What could be the exact usage of While - continue loop
by GrandFather (Cardinal) on May 09, 2007 at 03:36 UTC

    A common usage of continue is to allow some loop-end code to execute even when next is used to provide an early exit from a loop iteration. Consider:

    use warnings; use strict; for my $yodda (1 .. 4) { print "Odd is " and next if $yodda & 1; print "Even is "; } continue { print "$yodda\n"; }

    Prints:

    Odd is 1 Even is 2 Odd is 3 Even is 4

    Often early exits are used to avoid excessive control structure nesting - especially of if controlled blocks


    DWIM is Perl's answer to Gödel
Re: What could be the exact usage of While - continue loop
by naikonta (Curate) on May 09, 2007 at 04:18 UTC
    The continue block is guaranteed to be executed in each iteration.
    my $i = my $j = 0; while ($i++ < 5) { next if $i == 2 || $i == 4; print "i:$i\n"; } continue { $j++; printf "j:$j\n"; }
    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.

    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"; }

    Update: added note following correction by GrandFather

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      Actually the continue block is not executed for a redo. Consider:

      use warnings; use strict; my $yodda = 0; while ($yodda < 4) { redo if ++$yodda & 1; print "Even is "; } continue { print "$yodda\n"; }

      Prints:

      Even is 2 Even is 4

      DWIM is Perl's answer to Gödel
        Well, you hit me at the "regardless" part then :-)

        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: What could be the exact usage of While - continue loop
by davido (Archbishop) on May 09, 2007 at 06:24 UTC

    continue simply allows you to add code to a loop that is guaranteed to execute on each iteration even if next is invoked. It's further documented in perlsyn.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://614288]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-10-23 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (126 votes), past polls