Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i have something like

   while(playAgain==true)
   {
      cout<<"new game"<<endl; //i know 'using namespace std;' is looked down upon
      while(playerCard!=21)
      {
          *statements*
          if(decision=='n')
          {
              break
          }
       ...
      }
   }

but that break only breaks out of the first while loop when I want to break out of both of the loops

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share

548k questions

547k answers

4 comments

56.5k users

...