Alex24
Alex24

Reputation: 610

Why can I place a case inside another case in a switch statement?

Here is an example of a switch statement. I don't get why it works this way:

int main(){

    int number1 = 100, number2 = 200;

    switch(number1){
        case 100:{
            cout << " outer switch number is: " << number1 << endl;
        case 200:{ // any value other than 100
                cout << "inner switch number is: " << number2 << endl;
            }
            break;            
        }
        break;    
    }

    return 0;
}

Upvotes: 9

Views: 2505

Answers (3)

But why don't I get a compile-time error instead?

Because you didn't do anything illegal. A case statement is just a labeled statement. The expression in the switch is evaluated to figure which label to jump to (note the "jump" and "label") and then that statement starts executing. It's just a goto in disguise, to be honest. It's a bit more constrained, but still a jump.

The only constraint, since this is C++, is that you may not skip the initialization of an object by jumping ahead of it.

This feature was famously used in Duff's Device (albeit in C).

To be more clear, why any other value inside the inner case will also succeed?

Because after the jump is performed, those labels don't matter. They just mark specific statements. After the jump, execution proceeds normally. And normal execution doesn't avoid specific statements just because they are labeled.

Upvotes: 8

Raindrop7
Raindrop7

Reputation: 3911

A switch statement works like if statement but it is preferred to it when dealing with more than 2 or 3 choices.

switch evaluates conditions in case statements. So whenever it finds a case it consider it a condition so it continue executes until a break found as a sign of end of the statement. In your case case 100 begins but another case is inside so as long as there's no break for 100 ye, everything after it is executed.

case 100:
    // do some stuff 
    case 0:
       // do another stuff
    case 5779:
       // do ....
 break;

Al the above statements will be executed if only and if case 100 succeeds.

Remember in C++ you can write:

void foo(){
    cout << "foo" << endl:
}

int main(){

    b:
        foo();        

    return 0;
}

It like that label but only with the keyword case. you can say a case inside case without its switch or preceded by a break will work the same way a label works.

Upvotes: 0

Amiri
Amiri

Reputation: 2536

Why you don't see any compilation error? It's because you used the switch expression correctly. But, you might not see the results as you want. First let's try a simple switch expression:

switch(number1){
    case 100:
        cout<<"the number is 100"<<endl;
        break;
    case 200:
        cout<<"the number is 200"<<endl;
        break;
    default:
        cout<<"the number is neither 100 nor 200"<<endl;
        break;
} 

If you want to use other numbers, you can add to the switch-case block. If you don't use a break that means the next case is the target. Till you don't use the break the program will continue. The default says if non of the above happens I want to do this part.

Let's go to your program and what is going on:

switch(number1){
    case 100:{//if the number is 100 it will start from here
        cout << " outer switch number is: " << number1 << endl;
        //it will break after the first break expresion
    case 200:{ // any value other than 100// if the number is 200 it will start from here
            cout << "inner switch number is: " << number2 << endl;
        }
        break; // I meant her is the first break    
    }
    break;    
}

Upvotes: 1

Related Questions