using continue type behavior in switch

My question is about using something other than goto to go back to the beginning of operations from the default of a switch statement. I think it could be done by

1
2
3
4
5
6
7
8
9
10
while()
{
    (...)
    switch(response)
    {
        (...)
        default:
            continue;
     }
}


wait nvm because the default is at the end of the switch....But my question is what continue does in a switch?
Last edited on
it should go back to the beginning of the while loop >?

I'm not sure perhaps you should run a small test to see and post your results back here. you can do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while(true)
{
    int i=1;
    cout << "inside while " << i << "\n";
    switch( i )
    {
        case 1: 
        case 2:
        case 3:
                cout << "inside switch " << i << "\n";
                i++;
                continue;
        default: 
                cout << "inside default\n";
                break;
     }
     cout << "Bottom of while " << i << endl;
     break;
}

Last edited on
I did discover that

1
2
3
4
5
6
7
8
9
10
11
12
13
while(quit = false)
{
     (...)
     switch(response)
     { 
      case 'a'   :   (...) break;
      case 'b'   :   (...) break;
      case 'q'   :   cout << "quitting menu" << endl
            quit = true;
            break;
      default    :    (...)
      }
}


does go back to the beginning of the while until q is entered.

and the continue within a switch statement comes back as illegal error C2044: illegal use of a continue
thanks,
Last edited on
continue is used in the scope of loops.
A switch is not a loop.

If the the switch is inside in a loop then you can use continue - the continue command is associated with the loop - not the switch.
while(quit = false)
should be: while(quit == false)

The code I posted works perfectly fine, however for some reason i is never incremented because it's a switch and you can't change the value of the switch or some such....
so you get a never ending loop:

inside while 1
inside switch 1
inside while 1
inside switch 1
inside while 1
inside switch 1
inside while 1
inside switch 1
inside while 1
inside switch 1
inside while 1
inside switch 1
... etc
Last edited on
interesting, ok thanks for following up on that.
Topic archived. No new replies allowed.