How to break out of nested loops?

前端 未结 15 2437
北荒
北荒 2020-11-27 11:21

If I use a break statement, it will only break inner loop and I need to use some flag to break the outer loop. But if there are many nested loops, the code will

相关标签:
15条回答
  • 2020-11-27 11:51
    for(int i = 0; i < 1000; i++) {
        for(int j = 0; j < 1000; i++) {
           if(condition) {
              func(para1, para2...);
              return;
           }
        }
    }
    
    func(para1, para2...) {
        stmt2;
    }
    
    0 讨论(0)
  • 2020-11-27 11:56

    No, don't spoil the fun with a break. This is the last remaining valid use of goto ;)

    If not this then you could use flags to break out of deep nested loops.

    Another approach to breaking out of a nested loop is to factor out both loops into a separate function, and return from that function when you want to exit.

    Summarized - to break out of nested loops:

    1. use goto
    2. use flags
    3. factor out loops into separate function calls

    Couldn't resist including xkcd here :)

    enter image description here

    source

    Goto's are considered harmful but as many people in the comments suggest it need not be. If used judiciously it can be a great tool. Anything used in moderation is fun.

    0 讨论(0)
  • 2020-11-27 11:58

    Use:

    if (condition) {
        i = j = 1000;
        break;
    }
    
    0 讨论(0)
  • 2020-11-27 11:58

    I think goto will solve the problem

    for(int i = 0; i < 1000; i++) {
        for(int j = 0; j < 1000; j++) {
            if (condition) {
                goto end;
            }
        }
    }
    
    end:
    stmt2 
    
     
    
    0 讨论(0)
  • 2020-11-27 12:03
    i = 0;
    
    do
    {
      for (int j = 0; j < 1000; j++) // by the way, your code uses i++ here!
      {
         if (condition)
         {
           break;
         }
      }
    
      ++i;
    
    } while ((i < 1000) && !condition);
    
    0 讨论(0)
  • 2020-11-27 12:06

    I note that the question is simply, "Is there any other way to break all of the loops?" I don't see any qualification but that it not be goto, in particular the OP didn't ask for a good way. So, how about we longjmp out of the inner loop? :-)

    #include <stdio.h>
    #include <setjmp.h>
    
    int main(int argc, char* argv[]) {
      int counter = 0;
      jmp_buf look_ma_no_goto;
      if (!setjmp(look_ma_no_goto)) {
        for (int i = 0; i < 1000; i++) {
          for (int j = 0; j < 1000; j++) {
            if (i == 500 && j == 500) {
              longjmp(look_ma_no_goto, 1);
            }
            counter++;
          }
        }
      }
      printf("counter=%d\n", counter);
    }
    

    The setjmp function returns twice. The first time, it returns 0 and the program executes the nested for loops. Then when the both i and j are 500, it executes longjmp, which causes setjmp to return again with the value 1, skipping over the loop.

    Not only will longjmp get you out of nested loops, it works with nested functions too!

    0 讨论(0)
提交回复
热议问题