Take the 2-minute tour ×
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.

Suppose I have a loop in C++ or C# that goes like this:

while( true ) {
    if( condition() ) {

This is commonly called "infinite loop". Yet it is not technically infinite - it will stop once control flows through break.

What's the term for such loop - that has "loop forever" loop control statement and "break" inside?

share|improve this question

closed as off-topic by gnat, MichaelT, BЈовић, GlenH7, Dan Pichelman Oct 28 '13 at 21:23

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

I don't think there is a special term. –  ChrisF Dec 9 '11 at 9:26
Is there any guarantee that control will ever flow through break? In that example, what if condition() always returns false? I'd say it's an infinite loop with conditional breaks. –  JohnL Dec 9 '11 at 9:30
Even without a break, the loop is not infinite (kill, ctrl-alt-del, unplug...). So why bother with terminology details? –  mouviciel Dec 9 '11 at 9:37
"What's the term?" - "This is commonly called infinite loop". That's your answer right there. You're clearly not be happy with the term, but that doesn't take away the fact that (natural) languages are descriptive. The "term for X" is whatever people are using, not what they should be using. –  MSalters Dec 9 '11 at 9:57
This question appears to be off-topic because it is a "name that thing" question. "Name that thing" are bad questions for the same reasons that "identify this obscure TV show, film or book by its characters or story" are bad questions: you can't Google them, they aren't practical in any way, they don't help anyone else, and allowing them opens the door for the asking of other types of marginal questions. See blog.stackoverflow.com/2012/02/lets-play-the-guessing-game –  gnat Oct 27 '13 at 21:05

13 Answers 13

Studying CS, this professor taught us that there are pre-checking loops (while(cond) {}), post-checking loops (do {} while cond);), and middle-checking loops. (I might have badly translated this into English, but you get the idea.)

C and C++ do not have the latter (ISTR Ada having it, BICBW), so your construct is used for that in C and C++.

share|improve this answer
Yes, Ada has this: loop ... exit when condition; ... end loop; –  Keith Thompson Dec 9 '11 at 23:08
@Keith: Thanks for confirming! It's almost 20 years since I have done some Ada. –  sbi Dec 10 '11 at 10:35
For what it's worth, I've been a professional programmer for over 20 years (and a hobbyist longer than that), and I've never heard those terms. –  offby1 Dec 13 '11 at 18:12
Regarding the translation - I think "in" is usually more fitting when "pre" and "post" are used, e.g "preorder/postorder/inorder tree traversal". –  Oak Dec 13 '11 at 22:07
@Oak: Then that would be an in-checking loop? Sounds strange to me, but then I'm not a native. Feel free to elaborate. –  sbi Dec 14 '11 at 9:20

Lacking an official name, I'd call it the Broken Loop. The ambiguity of this term is intended, since a break in the middle of a loop is a bit unclean, almost like a goto.

share|improve this answer
I'd say, far worse than a goto. I'd much rather see a goto than a falsly constructed condition in a loop like that. –  Brian Knoblauch Dec 9 '11 at 14:59
@Brian What? The 'true' condition makes it obvious and loops like that are really common. For example, if you want to add every line of a file into a list, you have to try read the line (do something), stop if it failed (if condition break), otherwise add it to the list and repeat (do something else). –  Strilanc Dec 9 '11 at 15:54
There's nothing wrong with break in the middle of a loop. If you need to carry out some operation at each start of the cycle, you will have to either duplicate the code or stuff these operations into the condition. Either variant clearly has disadvantages. goto also had valid applications, e.g. emulation of a try...finally block in C. –  Malcolm Dec 10 '11 at 15:49
Malcolm: The possible problem is that during reading the code, it is harder to see when and why the loop is exited. Additional problems arise when you nest two or more such loops, and want to break out of the outer loop based on a condition found in the inner loop. –  user281377 Dec 11 '11 at 11:30

There is no definitive name. Infinite loop is, I think, the appropriate term. No loops are truly infinite, but this has the potential to be effectively infinite since it's possible that the branch that contains the break will never occur.

If you tell someone "create an infinite loop, and use a break for condition X" and they'll know what you mean. If someone is reviewing your code and says nothing more than "I don't like the infinite loop you wrote" you'll know what they are talking about (unless you have more than one, of course).

share|improve this answer
It's not an infinite loop; it's got a well-defined termination condition, just like a normal while loop and the way you do the proof of termination is exactly the same (finding a monotonically-decreasing metric is a great start). –  Donal Fellows Aug 28 '12 at 0:27

It's a do-while loop with the conditional in the wrong place.

share|improve this answer
Yeah, it should be written: while(keep_going) { doSomething(); if(condition) { keep_going = false; } else { doSomethingElse(); } } –  Stephen Gross Dec 9 '11 at 17:09
so... you're saying that if you're teaching a computer course and want to describe this, you would say "and now, we're going to learn about the *do-while loop with the conditional in the wrong place"? That sounds more like a religious opinion rather than a name. –  Bryan Oakley Dec 10 '11 at 0:32
@StephenGross That code snippet is a horrible suggestion. Refactor to put the actual condition in the loop definition, or just use break or continue. Avoid sentinel values at all costs, they're just another arbitrary piece of state to mentally keep track of, that disguises the purpose of the code. –  Izkata Dec 10 '11 at 3:47
Sentinal values are delayed gotos. –  Winston Ewert Dec 10 '11 at 4:10
-1 for being a single-exit weenie. –  Donal Fellows Dec 12 '11 at 13:50

I'd vote for "unconditional loop", similar to "unconditional jump". It illustrates exactly what is going on (the code loops unconditionally), without lying (unlike "infinite loop").

share|improve this answer
But it does have a termination condition; that if/break in the middle is part of the pattern. –  Donal Fellows Aug 28 '12 at 0:30

What's the term for such loop - that has "loop forever" loop control statement and "break" inside?

Its an infinite loop with a break condition.

I would agree with ammilind in that if you wanted to give it a special name you could call it an Infinite Partial Loop

share|improve this answer
It's not infinite at all; the point where the termination condition is checked is just in a different place. –  Donal Fellows Aug 28 '12 at 0:28

The very first course in CS at Stanford (Programming Methodology by Mehran Sahami) refers to this as a loop and a half. And it is not necessarily bad programming practice. Consider this example on collecting user input (taken from The Art and Science of Java by Eric Roberts, where Roberts also calls it a-loop-and-a-half):

prompt user and read in the first value
while (value != sentinel) {
    process the data value
    prompt user and read in a new value

And then the same thing solved using the loop and a half idea to avoid duplicate code:

while (true) {
    prompt user and read in a value
    if (value == sentinel) break;
    process the data value
share|improve this answer

There is no standard term, but I would say it as Partial loop.

This loop is used when you want to break only after executing some part of the loop one last time (i.e. partial execution). It's used when you don't find a suitable situation where you may want to break the whole loop.

In this case, you want to break the loop after at least executing doSomething() one last time.

share|improve this answer

On Rosetta Code, this particular pattern is described as an “N plus one half” loop. While it's not my favorite term, it's not terrible and is clearly a pattern that is useful for some kinds of loops. (The alternatives are to duplicate the before-the-condition code — potentially tricky in real programs — or to increase the nesting depth of the after-the-condition code while adding a loop condition variable; neither improves the maintainability or understandability of the code. The only reason for rejecting such constructs is if one insists on writing loops to be break-free.)

share|improve this answer

I have to agree with sbi here - I like the middle-checking loop term. This kind of construct was more popular when Structured Programming started to roll and many languages had syntactic support for them.

That said, it is now widespread knowledge that while loops are usually more mantainable, since it is easier to reason about invariants and they often handle the tricky empty case better.

In your particular case your loop is just equivalent to

for(; doSomething(), !condition(); doSomethingElse()){}

so I would only use the break version if either doSomething or doSomethingElse involved multiple statements and I'd rather not put them away into separate functions like you did.

That said, if your loop is more complicated then a (start, check, increment) iteration then you should consider refactoring it into something simpler.

share|improve this answer

I guess if we are going to try to make up a term for this, maybe:

Escapable Loop

share|improve this answer
+1 for a clever phrase. –  Stephen Gross Dec 9 '11 at 17:10
-1. the question isn't about making up a name, it's about whether a common name already exists. Plus, all loops are "escapable" so it's not a particularly effective name. –  Bryan Oakley Dec 10 '11 at 0:30
@BryanOakley An infinite loop, by definition, shouldn't be breakable. There probably shouldn't be a term for this, other than, bad programming. –  LarsTech Dec 10 '11 at 3:54

I call it what it is, a "while true loop."

Also see is while(true) bad programming practice?

share|improve this answer

It is not so bad in every case. I find myself writing this kind of loops with certain kind of APIs. Say, for instance, that you have a loop object, and need to check for some condition quite deep in it, like so:

while (loop
    .isCancelled()) {
        // ... do something with the current Handler ....
        loop = ..... // prepare for next loop

Now suppose every getXXX-method could potentially return null. Then it would be still possible to write a boolean expression, although a quite complicated and unreadable one. And then we have to do redo almost the same to get at the current handler object. In such cases, I find it easier to write a while (true) loop with break and continue.

share|improve this answer

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