2

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

cpp
  • 3,743
  • 3
  • 24
  • 38
gr33kbo1
  • 303
  • 4
  • 5
  • 15
  • 1
    set a flag, check it, if set break again.... – Mitch Wheat Sep 07 '13 at 05:24
  • The answer is here: http://stackoverflow.com/questions/1257744/can-i-use-break-to-exit-multiple-nested-for-loops – dcaswell Sep 07 '13 at 05:27
  • 3
    You could use a `goto` statement which jumps to a label outside the loop.... **Of course im joking :)**. The best solution is to avoid the nested if (The if with the `break`) and put the condition (`decission == 'n'`) in the two loops. – Manu343726 Sep 07 '13 at 06:35

6 Answers6

8

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.

LihO
  • 41,190
  • 11
  • 99
  • 167
  • 1
    It has a shortfall when too many variables are used but defined outside of the loop (in the original function), which means you have to pass those many variables via `foo()'s` arguments – Robin Hsu Sep 26 '17 at 08:15
3

There are basically two options to go.

  1. Add the condition check in outer loop.

    while ((playAgain==true) && (decision != '\n'))

  2. Simply use goto. People are often told never to use goto as if it's monster. But I'm not opposed to use it to exit multiple loops. It's clean and clear in this situation.

Eric Z
  • 14,327
  • 7
  • 45
  • 69
1
 while(playAgain==true && decision !='n' ){
                           ^^ add a condition
      cout<<"new game"<<endl; 
      while(playerCard!=21){
      *statements*
      if(decision=='n'){
          break
         }
     ...
      }
 }
P0W
  • 46,614
  • 9
  • 72
  • 119
1

Use goto:

   while(playAgain==true)
   {
      cout<<"new game"<<endl; //i know 'using namespace std;' is looked down upon
      while(playerCard!=21)
      {
          *statements*
          if(decision=='n')
          {
              goto label;
          }
       ...
      }
   }
   label: 
   ...    
cpp
  • 3,743
  • 3
  • 24
  • 38
0

This solution is specific to your case. When the user's decision is 'n', he doesn't want to play again. So just set playAgain to false and then break. Outer loop will be broken automatically.

while(playAgain==true){
   cout<<"new game"<<endl; //i know 'using namespace std;' is looked down upon
   while(playerCard!=21){
      *statements*
      if(decision=='n'){
         playAgain = false; // Set this to false, your outer loop will break automatically
         break;
      }
   }
}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

If you don't have to avoid goto statement, you can write

while (a) {
    while (b) {
        if (c) {
            goto LABEL;
        }
    }
}
LABEL:
qsona
  • 67
  • 6