Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Since this statement is so common:

while (true) (Java)

or

while (1) (C)

or sometimes

for (;;)

Why is there not a single instruction for this? I could think that an instruction that could do it is just do but do requires a while at the end of the block but it would be more logical to write an infinite loop like this

do {

//loop forever

}

Why not? AFAIK the instruction do always requires a while at the end but if we could use it like above then it would be a clear way to define something like while (true) which I think should not be written like that (or for (;;)).

share|improve this question
16  
what about the word "do" implies an infinite loop? –  Buttons Oct 13 at 6:19
 
What we're trying to program in an instuction that does do foreverand I think that could be just do { ... } but that is mostly not allowed. –  909 Niklas Oct 13 at 6:32
 
If someone were going to add a statement like this, a better choice would be forever. Doesn't really matter, though, because forever {...} and while (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

5 Answers

This is largely because creating a separate command would be unnecessary. Adding extra, unneeded commands to a programming language is often considered poor design. Why create a special instruction when one already exists that works?

Your idea of having only one of the "do/while" pair is how it works in other languages, like Python, for instance, since Python doesn't having closing brackets to worry about.

while True:
    ...

It is simply a matter of how any given language is designed. Remember that programming languages are man-made things - they can take whatever form we, as humans, design them to take. You could make your own language that follows the principle you desire, if you felt it was important enough.

share|improve this answer
 
your python example doesn't match the purpose of do{}while(). The idea behind do{}while() is checking the condition at the end of the loop, not at the beginning. –  CodesInChaos Oct 14 at 15:26
 
@CodesInChaos I think you misunderstand the question. The original poster is asking about creating an intentionally infinite loop, and why a conditional is necessary at all in such a common structure, whether at the beginning or end. –  Southpaw Hare Oct 14 at 15:40
 
The two part do...while only exists to check after the loop. For checking at the beginning and infinite loops c doesn't require a pair either. There is no difference as far as this question is concerned between python's while True: and c's while(true) –  CodesInChaos Oct 14 at 15:52
 
@CodesInChaos ...exactly. That's the point. This is as simple as the statement gets. –  Southpaw Hare Oct 14 at 15:58

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.

share|improve this answer

I would assume that language designers care somewhat about helping developers write accurate code. A language may prefer to not introduce a construct that leads to an infinite loop without the developer explicitly request it. A language like REXX take this concept further and introduces a forever loop as in:

do forever
...
end

So, to answer your question the reasons may be:

  1. To help developers write better code.

  2. To be similar to other popular languages.

  3. To use a single construct instead of several.

share|improve this answer

I would say that do while is more explicit than just do. In programming it's often beneficial to be explicit. This translates into code that is easier to read. With the proposed do loop the while(true) portion or condition is still implied.

So to sum it all up I'd say it's because most languages are designed to be explicit and readable.

share|improve this answer

Go has a single instruction for it: for. The for loop has several faces, the simplest form is

for {
    // do something
}

which is equivalent to

for true {
    // do something
}

which is the same as

for ; true ; {
    // do something
}

which is similar to C's for-loop just with first and last part empty.

See the specification.

share|improve this answer
 
It is not clear how this answers the question. You are providing a counter-example, to the OP's statement that a do or some similar syntax is always required, but I believe the question is why don't most languages include this kind of syntax. –  DemetriKots Oct 13 at 19:44
1  
@DemetriKots: I am providing a counter example. The question is "Why does the instruction “do” require a “while”?". I show a common programming language which doesn't require it, thereby answering the question to something like "the preposition is incorrect, therefore the question is not answerable". –  topskip Oct 13 at 19:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.