Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

10
  • 65
    The idea that while(true) should be harmful seems bizarre to me. There's loops that check before they execute, there's those that check afterwards, and there's those that check in the middle. Since the latter don't have syntactic construct in C and C++, you'll have to use while(true) or for(;;). If you consider this wrong, you haven't given enough thought to the different kind of loops yet. Commented Sep 14, 2009 at 9:37
  • 42
    Reasons why I disagree: while(true) and for(;;;) are not confusing (unlike do{}while(true)) because they state what they're doing right up front. It is actually easier to look for "break" statements than parse an arbitrary loop condition and look for where it's set, and no harder to prove correctness. The argument about where to put code is just as intractable in the presence of a continue statement, and not much better with if statements. The efficiency argument is positively silly. Commented Sep 14, 2009 at 17:15
  • 28
    Whenever you see "while(true)", you can think of a loop that has internal termination conditions, which are no harder than arbitrary end conditions. Simply, a "while(foo)" loop, where foo is a boolean variable set in an arbitrary way, is no better than "while(true)" with breaks. You have to look through the code in both cases. Putting forth a silly reason (and microefficiency is a silly argument) doesn't help your case, by the way. Commented Sep 14, 2009 at 17:49
  • 6
    @Dave: I gave a reason: It's the only way to get a loop that checks the condition in the middle. Classic example: while(true) {string line; std::getline(is,line); if(!is) break; lines.push_back(line);} Of course I could transform this to a preconditioned loop (using std::getline as the loop condition), but this has disadvantages on its own (line would have to be a variable outside the loop's scope). And if I did, I would have to ask: Why do we have three loops anyway? We could always transform everything into a preconditioned loop. Use what fits best. If while(true) fits, then use it. Commented Sep 15, 2009 at 9:07
  • 3
    As a courtesy to people reading this answer, I would avoid commenting on the quality of the good that produced the question and stick to answering the question itself. The question is a variation on the following, which I believe is a feature of many languages: you have a loop nested inside of another loop. You want to break out of the outer loop from the inner loop. How is this done? Commented Jun 30, 2019 at 23:50