3

how can i exit from nested while() or for() without goto?

for example if i use three loops like below in a function:

   void myfun(){
    for (;;)
    {
        while( true )
        {
            for (;;)
            {

          //what is the exit code of all loop()  from here?
            }
        }
     }
    }

using break; only can exit from one loop,
but how can i exit all loops ?
the loops can be limited by counter or unlimited.

3 Answers3

3

I personally would rewrite the code so that you don't have a nested loop in the first place. Something like this:

bool myFun2
{
    for (;;)
    {
        if(something) return true;
    }
    // If the loop isn't "forever", return false here?
}


bool myFun1()
{
    while( true )
    {
       if (myFun2()) return true;
    }
    // return false here if needed.
}

void myfun()
{
   for (;;)
   { 
      if(myFun1()) break;
   }
}

This becomes much easier to follow than trying to figure out which conditions some exitLoop variable gets set, for example.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • You could also use lambdas. That might be more convenient if inner loops need to access outer local variables. – Neil Kirk Nov 29 '15 at 12:01
  • I don't particularly like lambdas except for very simple cases. That's mostly because they are a pain to debug, since they don't have (obvious to the reader) names, so you can't set breakpoints there. But yes, possible, – Mats Petersson Nov 29 '15 at 12:10
  • @MatsPetersson: Why shouldn't you be able to set breakpoints in lambdas? After all, you set breakpoints in the source code, not in the generated binary, so it doesn't really matter what concrete names are generated for the lambdas. – Christian Hackl Nov 29 '15 at 12:11
  • I don't know what debugger you use, but none of the ones I use allow me to set breakpoints other than as "file + line-number" or "name of function". Which makes lambda's hard to set breakpoints in when they typically are at the same line as the one using the lambda, and don't have a (user-available) name. – Mats Petersson Nov 29 '15 at 12:15
1

You can't, you need another break at while context or change yours loops usign a variable as a exit flag:

      bool exit = false;
      for (;;){
       while (!exit){
            for (;;){
               exit = true; 
               break;
            }
       }
       if (exit) break;
      }

An so on for as many loop do you have in your code

Joan Esteban
  • 1,006
  • 12
  • 23
0

If you want to jump out of the function that is leave the function then you should use return. However if you want to just jump off the nested loops & not out of the function then you can throw an exception. This method will help you from breaking the code into several functions as some have done. However exceptions are meant for library designers & we should avoid using them too much. Personally speaking using goto is the best thing in this case but as you asked against it, hence I'm saying so. Well then your code will look like this :-

void myfun()
{
    try
    {
        for (;;)
    {
        while( true )
        {
            for (;;)
            {
                if (/*some condition*/)
                throw false;
            }
        }
    }
    }
    catch (bool)
    {
        cout<<"caught";
    }
    // do stuffs if your code is successful that is you don't break out
}
Ankit Acharya
  • 2,833
  • 3
  • 18
  • 29
  • That's an abuse of exception mechanism. – SomeWittyUsername Nov 29 '15 at 12:01
  • 1
    Exceptions will add a lot of overhead. – Neil Kirk Nov 29 '15 at 12:01
  • @NeilKirk I guess the overhead generally occurs when we are using exception classes derived from `std::exception`. This is a simple one throwing `bool` so overhead won't be significant. – Ankit Acharya Nov 29 '15 at 15:49
  • @SomeWittyUsername I agree to it & hence I myself have written in the answer that I don't prefer it because `exceptions` are meant generally for library developers – Ankit Acharya Nov 29 '15 at 15:50
  • 1
    Don't guess, look it up. Exceptions are slow and it's nothing to do with `std::exception`. Exceptions can be used by anybody, not just library developers. – Neil Kirk Nov 29 '15 at 16:26
  • @NeilKirk i didn't say that it is used only by library developers & no on else can use it, i just meant that exceptions are more useful in library development – Ankit Acharya Nov 30 '15 at 13:35