+10  A: 

You can do:

while (1) {
   //some code
   if ( condition) {
       break;
   }
   // some more code
}
Sean McCauliff
I know I could do that, but I was just wondering if it could be done more like the way I have it in my question. Do you know if it is possible that way?
Silmaril89
I don't believe it is, no. This is the way the language is set up, the compilers won't recognize it. Even if they did compile it, it would likely still end up like the above answer anyway.
Corazu
Ok, thanks for responding.
Silmaril89
+2  A: 

Well, I think you should move the condition to the middle of the loop(?):

while (true)
{
  ...
  // Insert at favorite position
  if (condition)
    break;
  ...
}
AraK
+1  A: 

The answer is no, you can't have your loop automatically terminate when the condition that the while statement is supposed to evaluate is true until it actually evaluates it at the top (or bottom) of the loop. The while statement can't be placed in the middle of the loop.

sean e
+3  A: 

Technically, yes:

 for ( ;CodeBefore, Condition; ) {CodeAfter}
ima
Nice, but sick at the same time...
sean e
+1  A: 

A little background and analysis: what you're asking for I've heard called a "Dahl loop", named after Ole-Johan Dahl of Simula fame. As Sean E. states, C++ doesn't have them (ima's answer aside), though a few other languages do (notably, Ada). It can take the place of "do-while" and "while-do" loops, which makes it a useful construct. The more general case allows for an arbitrary number of tests. While C++ doesn't have special syntax for Dahl loops, Sean McCauliff and AraK's answers are completely equivalent to them. The "while (true)" loop should be turned into a simple jump by the compiler, so the compiled version is completely indistinguishable from a compiled version of a hypothetical Dahl loop. If you find it more readable, you could also use a

do {
    ...
} while (true);
outis
By the way, I vaguely remember someone else on SO calling them by another name, also a surname. Does anyone know what that name might have been? Does anyone remember the post and what it might have been in response to?
outis