Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
  • The program above prints: 100 and then prints the next statement of case 200. Also if any value other than 200 is used in the second case, it still gets executed. I know there is no break after case 100. But why don't I get a compile-time error instead?

  • To be clearer, why will any other value inside the inner case also succeed? E.g.,

    case 70000:
    
like image 838
Alex24 Avatar asked Dec 31 '17 20:12

Alex24


People also ask

Can you put a switch case inside a switch case?

Nested-Switch Statement:Nested-Switch statements refers to Switch statements inside of another Switch Statements.

Can you have multiple cases in a switch statement?

A switch statement includes multiple cases that include code blocks to execute. A break keyword is used to stop the execution of case block. A switch case can be combined to execute same code block for multiple cases.

What Cannot be used in switch case statement?

The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value. The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.

What are the restrictions of switch case?

1) The switch expression must be of an integer or character type. 2) The case value must be an integer or character constant. 3) The case value can be used only inside the switch statement. 4) The break statement in switch case is not must.


1 Answers

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.

like image 53
StoryTeller - Unslander Monica Avatar answered Sep 28 '22 11:09

StoryTeller - Unslander Monica