Test a single variable for multiple possible values, allowing for the value of the variable to control program flow through one or more blocks of code.
switch x
{
case 1:
do_something_if_1();
break;
case 2:
do_something_if_2();
case 3:
do_something_if_2_or_3();
break;
case 4:
do_something_if_4();
break;
}
Note the use of the break statement in the above example. The break statement immediately jumps program execution to the very end of the switch statement. Switch statements exhibit a fall through behavior where the code in each case following a “true” case is executed unless a break statement is encountered.