Ahem.
There’s one wrinkle when while and until are used as statement modifiers. If the statement they are modifying is a begin/end block, the code in the block will always execute at least one time, regardless of the value of the boolean expression.
– David Thomas and Andrew Hunt, Programming Ruby: The Pragmatic Programmer’s Guide, section Expressions.
And the proof:
$ cat crud.rb
print "Hello\n" while false
begin
print "Goodbye\n"
end while false
$ ruby crud.rb
Goodbye
$
Case closed.

But isn’t that really just do… while? I’d expect the body of the loop to execute once. E.g. in C++ (sorry):
int i = 0;
do
{
++i;
} while (false);
// down here I’d expect i == 1
I suppose the inconsistency is that the first statement sans block should also execute once, is that it?
Off I bugger…
mrmoo – no, that’s not really an inconsistency, since
print “Hello\n” while false
is really just syntactic sugar for
while false
print “Hello\n”
end
…which I don’t think anyone would expect to execute at all.
Mike:
I’m a bit lost as to the point of these posts. Unless you’re talking about something like Scheme (that only defines 20 or so keywords), any sufficiently advanced language is going to have made design decisions that people disagree with. I’d venture a guess that most language designers are unhappy about something in their own language, but since I haven’t written one, I can’t comment on that directly.
mrm00 asks: “But isn’t that really just do…while? I’d expect the body of the loop to execute once. [...] I suppose the inconsistency is that the first statement sans block should also execute once, is that it?”
That’s the truly horrible part, yes. In C, the semantics of a do…while loop are not dependent on whether or not the controlled statement is a block! So the following two lines are exactly equivalent:
do { puts("hello"); } while (0);
do puts("hello"); while (0);
This sort of orthogonality is a very desirable property, and C better embodies it than any of the other popular imperative languages. It’s also seen in, for example, the absence of an elseif (or elseif!) keyword: in C, things Just Work if you chain successive else and if clauses, and that is as it should be.
But the C formulation of this construct also wins by being explicit about what’s happening: the do keyword up front makes it obvious that we have a do…while loop here, whereas in Ruby’s Perl-inspired statement-modifier formulation, it look like it should just, well, modify the statement.
(Another question is what the heck a do…while loop is for. I suppose I must have used one at some point in the last 30 years, but if I did I can’t remember it.)
@duwanis I guess
begin
insult_language_amusingly
end while more_languages
would have been more concise, but where’s the fun in that? ;-)
That’s messed up.
Pingback: So what actually is my favourite programming language? « The Reinvigorated Programmer
I have found “xyz language sucks” quite fun,
about the C language:
“do…while loop is for. I suppose I must have used one at some point in the last 30 years, but if I did I can’t remember it.”
I have used them often, every time I have need to execute code at least once.
But my programming style is a bit rough.
I am loving this conversation… continue guys! :D
The real hate is what in any other language I’d call a “creeping perlism”, but in this case is undoubtedly inherited from perl… having a “suffix conditional” for ANY purpose whatsoever by a “do while” block. That is, that program should be a syntax error.
Got to agree on while-modifiers. if-modifiers I have no problem with, in fact I use them all the time in my own code. They are unambiguous because there is only one kind of if statement that such a modifier could represent. But there are two kinds of while loops, the while (cond) statement type that executes zero or more times and the do statement while (cond) kind, that executes one or more times; and it’s not clear which of those the while modifier is meant to represent. I, and I think most people, would guess zero-or-more; in Ruby, it’s one-or-more. I couldn’t even tell you which it is in Perl without looking it up or testing it, which, given that Perl has been my Language Of Choice for the last decade, tells you something in itself.