Since this statement is so common:

while (true) (Java)


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 (;;)).

  • 26
    what about the word "do" implies an infinite loop?
    – Lucina
    Oct 13 '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.
    – Niklas R.
    Oct 13 '13 at 6:32
  • 2
    #define forever while(true)
    – Eric
    Oct 13 '13 at 23:26
  • 3
    #undef forever and just use while (true). No need to be creative. Aug 23 '14 at 23:37
  • 1
    A missing trailing while (...) is far more likely to be an error than it is an intended infinite loop. Allowing it would be silly. Aug 24 '14 at 18:20

10 Answers 10


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.

  • 3
    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. Oct 14 '13 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. Oct 14 '13 at 15:40
  • 1
    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) Oct 14 '13 at 15:52
  • @CodesInChaos ...exactly. That's the point. This is as simple as the statement gets. Oct 14 '13 at 15:58
  • Java and C can do this code too. It's not unique to Python. I'm not sure what point you're tying to make. Aug 24 '14 at 18:20

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: $!";

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.


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

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.


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.


do/while is semantically different from for and while in that the condition is checked after the loop body has executed, not before. The do { ... } while(condition) structure makes that behavior clear. Now, dmr could have used a different method to convey the same semantics, like

until ( condition ) { ... }

but it isn't as clear (to me, anyway) that condition is evaluated at the end of the loop.

For an infinite loop it doesn't matter; just use for(;;) (which I think is more idiomatic than while(1)).

  • What you wrote looks like repeat-until loops in Pascal and Lua. The loop has an exit condition at the end of the loop instead of an entry condition at the start.
    – marcus
    Nov 11 '13 at 22:34

What should be the meaning of:

while(read_data() != null)

If some other keyword had been used for expressing the continuation condition of a do loop (as with Pascal's Repeat...Until) then it might have been possible to make the continuation condition optional. As it is, however, while(condition); can either appear as the last thing in a do/while or as a stand-alone loop. Something must appear between the controlled statement of a do/while and a succeeding stand-alone statement, and its easiest to simply have that something always be a while(expr); clause.


My guess is: for no specific reason other than personal tastes of the language designers.

Some languages, such as PL/SQL, Ada, Common Lisp, Perl6 and Go (as amon and topskip have demonstrated) do have a loop structure that loops infinitely when used by itself, but can be optionally extended with several clauses, such as while, until, initialization, increments, etc.

Most likely the C designers thought that for+while+do/while were the most useful idioms and then C++, Java and others just copied the loop statements, preferring to focus their design efforts on other language features, such as object orientation.

In other words, I don't think language designers think that's a bad feature, it's just that eventually you have to stop adding features, and there's a solid tradition in C-based languages to have for+while+do/while. In other languages, the feature you suggested does exist and it's useful.



    goto there;

But really "while(true)" is clear and concise.

A naked "while" is ambiguous, why should it mean loop forever. Logically its "while nothing" which implies you never go there.

Similarly a naked "do" would imply "do once". If someone asks you to "do the dishes" you wash them once, you do not wash them over and over again until someone asks you to stop.

Perhaps more importantly an infinite loop is an important thing to notice if you are trying to understand a programs logic, it should have a noticeable syntax and not a single word easily passed over.


Infinite loops are generally regarded as bad pratcice, and thus it wouldn't make sense to include a dedicated syntax for them in a language. Special syntax (or 'syntatic sugar') is added for common tasks, such as an unless block in addition to an if block, to make the code more 'friendly' and readable.

Since infinite loops are fairly rare, and are generally regarded as bad practice, it wouldn't be a good idea to support them in a language via a dedicated syntax.


From my point of view using do...while to create infinite loops leads to code difficult to maintain. When you encounter a do you have to scroll all the way to see if it is a infinite loop or not. On the other hand using for(;;) or while(1) at the beginning of the loop is a lot more clear. As it was already mentioned you can still create infinite loops with the current design of the language so you don't need to change it. Since there is no actual need for a design change in the language none is actually changing it.

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