|Don't ask to ask, just ask|
Re^3: If I was forced to use only one kind of loop for the rest of my days it would be aby Roger_B (Scribe)
|on Oct 01, 2005 at 13:35 UTC||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,
In fact, you'll need an extra block to get the correct scope:
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.
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.
the for deparses as a while!