How to break out of nested loops?

前端 未结 15 2687
北荒
北荒 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 12:10

    One way is to put all the nested loops into a function and return from the inner most loop incase of a need to break out of all loops.

    function() 
    {    
      for(int i=0; i<1000; i++)
      {
       for(int j=0; j<1000;j++)
       {
          if (condition)
            return;
       }
      }    
    }
    

提交回复
热议问题