Programming languages are still languages, and when translating code from English to programming language X, you may have to change some idioms, reorder words, sentences, or sometimes include unpronounceable curly braces…
When you want an infinite loop, the while (true)
or its equivalents can be used. Without good reasons, no language designer will add yet another keyword just for infinite loops.
Your idea that do { ... }
always implies a trailing while ...
doesn't hold up. In Ruby, do
starts a code block, and in Perl it allows you to use a code block on expression level:
# read a whole file at once
my $file_contents = do {
local $/;
open my $fh, "<", $filename or die "Can't open $filename: $!";
<$fh>;
};
Because do
is always an expression, various other statement modifers may be used as well (if
, unless
, and special-cased keywords to evaluate the block once before testing the condition: while
and until
).
Perl6 is interesting here because it renames the C-style for
to the loop
keyword. The various statements like initialization are optional, so one can write:
loop {
say "hi";
}
… which is the same as say "hi" while True
.
Just use while (true)
– everyone will understand that. The for(;;)
is also a widely-understood idiom. If you are using a language with a preprocessor, you could also do evil stuff like #define forever for(;;)
, but that reduces maintainability.
do forever
and I think that could be justdo { ... }
but that is mostly not allowed. – 909 Niklas Oct 13 at 6:32forever
. Doesn't really matter, though, becauseforever {...}
andwhile (true) {...}
are going to compile down to the same code anyway. – Blrfl Oct 13 at 14:40#define forever while(true)
– Eric Oct 13 at 23:26