On 11/01/12 06:45, Nigel Redmon wrote:
Just to get my fingertips wet again, I "fixed" something trivial that I had commented on 
over two years ago: One of the simplest things you could imagine, an article on the "while 
loop" construct in programming. There were examples in many computer languages, but a writer 
obviously had a special love for Perl—he gave the expected example, then showed that with 
***Perl*** you could write a compact one-line version, unlike C and C++. First thing is it's 
idiotic to show off terse programming skills, giving a hard-to-read version, in an article designed 
to give newbies an explanation of one of the most basic programming structures.

That original writer was correct, although it's not so much that you can write "on one line", but that statements support statement modifiers such as while or for. This is a single statement:

$factorial *= $counter-- while $counter > 0;

and as such is not the same as this:

while($counter > 0) { $factorial *= $counter-- }

Sure, you'll get the same answer, but it compiles differently (see perl -MO=Concise output), lexical scoping rules are different, and you can't have multiple statement modifiers, so while(Y) { while(X) { ... } } has no ... while X while Y; equivalent. The postfix-while/for/etc. idiom is common enough in Perl code to warrant inclusion here precisely *because* it's not something you can do in C/C++.

http://perldoc.perl.org/perlsyn.html#Statement-Modifiers

Oh, yeah, that and the fact it was totally wrong—of course you can do the same 
one-liner while loop in C/C++.
No, you can't - at least the last time I checked neither C nor C++ support statement modifiers. Sure, you can write while(counter > 0) factorial *= counter++; but that's just not the same.

Tom
--
dupswapdrop -- the music-dsp mailing list and website:
subscription info, FAQ, source code archive, list archive, book reviews, dsp 
links
http://music.columbia.edu/cmc/music-dsp
http://music.columbia.edu/mailman/listinfo/music-dsp

Reply via email to