5

I want to have a while loop do something like the following, but is this possible in c++? If so, how does the syntax go?


do {
    //some code
    while( expression to be evaluated );
    // some more code
}

I would want the loop to be exited as soon as the while statement decides the expression is no longer true( i.e. if expression is false, //some more code is not executed)

L0j1k
  • 12,255
  • 7
  • 53
  • 65
adhanlon
  • 6,407
  • 13
  • 43
  • 41
  • What's wrong with `if(condition) break;` Why don't you use that? – Naveen Nov 27 '09 at 06:43
  • 1
    There is no (and can't be) language constructs for every possible use case you can imagine. – n0rd Nov 27 '09 at 06:48
  • The `while` in the sample code will compile as a loop with no body, which could be an infinite loop, depending on the expression. `do *statement*` must be followed by a `while`, so that part shouldn't compile. – outis Nov 27 '09 at 07:33
  • Even if it could work it would result in unreadable code, therefore you should not even "hope" that it could work. – User Apr 11 '11 at 19:33

6 Answers6

12

You can do:

while (1) {
   //some code
   if ( condition) {
       break;
   }
   // some more code
}
Sean McCauliff
  • 1,494
  • 1
  • 13
  • 26
  • 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? – adhanlon Nov 27 '09 at 06:39
  • 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 Nov 27 '09 at 06:42
6

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
  • 75,655
  • 22
  • 151
  • 221
  • 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 Nov 27 '09 at 07:28
  • N and a half loop. One of the first google reponses for the term: http://www.cs.duke.edu/~ola/patterns/plopd/loops.html#loop-and-a-half PS. Better late than never – Deiwin May 02 '13 at 17:42
  • @Deiwin: thanks for the additional term, but the one I was looking for is a surname. Part of why I'm curious is it was presumably named after someone else of note, and I'd like to learn the history. The post has probably been lost to time. – outis May 12 '13 at 03:41
3

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

while (true)
{
  ...
  // Insert at favorite position
  if (condition)
    break;
  ...
}
nobody
  • 19,814
  • 17
  • 56
  • 77
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
3

Technically, yes:

 for ( ;CodeBefore, Condition; ) {CodeAfter}
ima
  • 8,105
  • 3
  • 20
  • 19
1

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
  • 11,792
  • 3
  • 44
  • 56
0

A variation on Dahl's loop that does not involve a goto into the middle of a do-while() is to use a switch-default:

switch (0) do {
    // some more code
default:
    // some code
} while (expression);