This is fundamentally what a for loop does. It initializes in some way (usually a variable) and loops while a certain condition is true, performing some action after each time through the loop.

Here are some benchmark results to peruse...

Benchmark: timing 100000 iterations of Deparses, for... Deparsed: 5 wallclock secs ( 4.43 usr + 0.00 sys = 4.43 CPU) for: 6 wallclock secs ( 4.39 usr + 0.00 sys = 4.39 CPU)
Not too surprising (in fact, I would have been shocked had there really been a substantial difference).

Just thinking of really low-level code, the Deparse version is really just the "under-the-hood" action of a for loop. Think about assembly (pseudo) code:

# Warning: I make no claims to know assembly, this is an example only +:-) mov bx 0 ; Initialize our loop variable mov ax, 10 ; Remember our exit condition top: ; Mark the top of the loop jnle last: ; Quit if bx is not less than ax print bx ; Do the body of the loop add bx 1 ; Add one to the loop variable jmp top: ; go to the top of the loop last:
So, the Deparsed code is just a closer representation of the low-level code.

The continue block (from perlsyn)

If there is a continue BLOCK, 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. Thus it can be used to increment a loop variable, even when the loop has been continued via the next statement (which is similar to the C continue statement).
So, the continue block executes once per iteration of the loop. Why would you use one yourself?

Russ
Brainbench 'Most Valuable Professional' for Perl


In reply to RE: why does a for loop parse like this? by Russ
in thread why does a for loop parse like this? by eduardo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.