Switches are jumping tables with the cases being the labels.
On hitting a switch, the PC jumps to the case with the correct value and starts executing every line from this point onwards. Using break you can jump to the end of the switch structure and therefore ommit executing everything behind the break.
If you for example like to execute the same code for 3 different values you can solve it like this:
Code:
switch (x) {
case 1:
case 2:
case 3:
Foo();
break;
default: Bar();
}
As no instruction is between the three cases, they all label the same instruction (Foo), so in case of 1 2 or 3 the program flow will jump to Foo.
Another example
Code:
switch (x) {
case 1
Foo();
case 2:
Bar();
break;
case 3: FooBar();
}
In this example entering with 1 will execute foo and then jump to bar and then break out of the switch. On 2 it will jump over the Foo call directly to Bar call, will continue to break and exit the switch.
In case 3 the execution jumps directly to the case 3 label skipping the foo and the bar call as well as the break, executing FooBar() and then reaching the end of the switch and exit it.
Because of the implementation as jumping tables switches are more efficient than ifs, but can be used for ordinal types only