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?

  • 22
    I don't think there is a special term.
    – ChrisF
    Commented Dec 9, 2011 at 9:26
  • 2
    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
    Commented Dec 9, 2011 at 9:30
  • 1
    Even without a break, the loop is not infinite (kill, ctrl-alt-del, unplug...). So why bother with terminology details?
    – mouviciel
    Commented Dec 9, 2011 at 9:37
  • 5
    "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
    Commented Dec 9, 2011 at 9:57
  • 5
    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
    Commented Oct 27, 2013 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++.

  • 1
    Yes, Ada has this: loop ... exit when condition; ... end loop; Commented Dec 9, 2011 at 23:08
  • @Keith: Thanks for confirming! It's almost 20 years since I have done some Ada.
    – sbi
    Commented Dec 10, 2011 at 10:35
  • 1
    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
    Commented Dec 13, 2011 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
    Commented Dec 13, 2011 at 22:07
  • 2
    The term I grew up with was "mid-exit loop." I've worked in a couple of different languages with explicit support for mid-exit loops.
    – mjfgates
    Commented Dec 15, 2011 at 20:48

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

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.

  • 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. Commented Dec 9, 2011 at 14:59
  • 14
    @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). Commented Dec 9, 2011 at 15:54
  • 7
    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
    Commented Dec 10, 2011 at 15:49
  • 1
    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
    Commented Dec 11, 2011 at 11:30
  • Let's say you have an event loop driving an Xorg application, like (in pseudo-code:) while(XEventGet(&ev) != NULL){ ... }, you're naturally going to want to check the keys inside the loop: if(ev.key == XK_q) break;. Doing the following: while(XEventGet(&ev) != NULL && ev.key != XK_q){ ... }, is ugly and arguably harder to read than a mid-loop break. Plus, what if something needs to be done to the value first before it can be checked? You're not seriously going to stuff all of that into the loop's base case, are you? Commented Aug 12, 2016 at 18:25

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

  • 1
    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). Commented Aug 28, 2012 at 0:27

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

  • 2
    Yeah, it should be written: while(keep_going) { doSomething(); if(condition) { keep_going = false; } else { doSomethingElse(); } } Commented Dec 9, 2011 at 17:09
  • 10
    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. Commented Dec 10, 2011 at 0:32
  • 5
    @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
    Commented Dec 10, 2011 at 3:47
  • 2
    Sentinal values are delayed gotos. Commented Dec 10, 2011 at 4:10
  • 2
    -1 for being a single-exit weenie. Commented Dec 12, 2011 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").

  • 1
    But it does have a termination condition; that if/break in the middle is part of the pattern. Commented Aug 28, 2012 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

  • 1
    It's not infinite at all; the point where the termination condition is checked is just in a different place. Commented Aug 28, 2012 at 0:28

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


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.


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.


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

Escapable Loop

  • -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. Commented Dec 10, 2011 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
    Commented Dec 10, 2011 at 3:54

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

Also see is while(true) bad programming practice?


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.

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