how to exit nested loops

后端 未结 6 1163
予麋鹿
予麋鹿 2021-01-21 20:11

i have something like

   while(playAgain==true)
   {
      cout<<\"new game\"<

        
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 20:54

    Don't cook spaghetti and extract your loops into the function:

    void foo(...) {
        while (...) {
            /* some code... */
            while (...) {
                if ( /* this loop should stop */ )
                    break;
                if ( /* both loops should stop */ )
                    return;
            }
            /* more code... */
        }
    }
    

    this decomposition will also yield cleaner code since instead of hundreds of lines of ugly procedural code, you will have neat functions at different levels of abstraction.

提交回复
热议问题