switch ( expression) {Notes: Expression should be an int or a char type. e.g.
case a: statement1; break;
case b: statement2;
case d: statement3; break;
default: statement4;
}
switch (character) {If the character is 'B' or 'b' then statement1 is executed, but if the character is 'c' then statement2 and statement3 are both executed. With 'd' though, only statement3 is executed. For all other characters statement4 is executed.
case 'B':
case 'b': statement1; break;
case 'c': statement2;
case 'd': statement3; break;
default: statement4;
}
When a case matches, all statements from that case on, including those that follow are executed until a break is found. This is known as 'Fall Through'.
switch (value) {In this example, I've exploited 'fall through' so that for any number 1-5, statements n; n-1 down to 1 are executed. Eg if value ==3, statement3, statement2 and statement1 are executed (in that order).
case 3:statement3;
case 2:statement2;
case 1:statement1;
default: break;
}
Notes about Switch
- Each case can have one or more statements without needing to be enclosed by curly brackets.
- The default case can occur anywhere.
- Good Practice to use default, perhaps to trap errors.

