Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
While it's true you can do anything you can do with a for loop with a while loop, I picked for.

I really like the capability that for gives you to keep all the operations controlling the loop - initialisation, incrementing and testing - in one place. In C99 (and Perl) you can declare variables in the for statement, and they will have a scope of precisely the for loop. You'll need an extra block to do that with while. So taking the example above, I assume this is intended to synthesise (updated:) ambrus noted that this is equivalent to:

// UNTESTED for(int a = 1; a<10; a++) { printf("%d\n",a); }

In fact, you'll need an extra block to get the correct scope:

// UNTESTED // UPDATED: de-obfuscated { int a = 1; while(a<10) { printf("%d\n",a); a++; } }

Which is starting to look rather messy.

Context switch back to Perl here!

Synthesising while and foreach from for seems reasonably intuitive to me on the other hand.

# UNTESTED for (;EXPR;) BLOCK # leave first and third EXPRs blank to be # equivalent to while (EXPR) BLOCK # UNTESTED for (my @list = LIST, VAR = shift @list; # iterate manually through @list; # LIST in "for" to be VAR = shift @list) BLOCK # somewhat like # foreach VAR (LIST) BLOCK # without the usual aliasing

OK, the foreach is starting to look a bit funny, but a foreach from while will be worse, and spread into the block, rather than be confined to the statement itself.

One issue is that for doesn't have a continue block, but as I come from C I don't tend to use those. continue blocks also split up code that happens every time round the loop, which puts me off. for's third expression is roughly equivalent I suppose, but stuffing a large continue block there could hardly be called good style.

Recursion is another story but I don't think I could live with it as the only looping mechanism. I'd rather have the reverse issue of synthesising recursion, say with a list of hashes, on the rare occasions I really needed it.

updated in response to ambrus's comments.

Also, note:

perl -MO=Deparse -e "for (;<>;){print}" while (defined($_ = <ARGV>)) { print $_; } -e syntax OK

the for deparses as a while!

In reply to Re^3: If I was forced to use only one kind of loop for the rest of my days it would be a by Roger_B
in thread If I was forced to use only one kind of loop for the rest of my days it would be a by collin

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (8)
    As of 2019-10-23 16:29 GMT
    Find Nodes?
      Voting Booth?