I have a switch where for particular cases I need to do some peculiar things but I still want them to pass through the default case of the switch statement because in there I have functionality that is common to all cases. How can I force the passage to the default case after I’ve handled the peculiar one?
Example:
switch(someVar) {
case "a":
....
//do something peculiar for a
break;
case "b":
...
//do something peculiar for b
break;
default:
...
//common things that all cases should do
}
EDIT: At the moment I’ve just copy-pasted the lines inside the default at the bottom of each peculiar case but that’s ugly.
>Solution :
If things must be executed unconditionally, they should be moved out of the condition:
switch(someVar) {
case "a":
....
//do something peculiar for a
break;
case "b":
...
//do something peculiar for b
break;
}
//common things that all cases should do
Alternatively, extract a method:
switch(someVar) {
case "a":
....
//do something peculiar for a
doCommonThings();
break;
case "b":
...
//do something peculiar for b
doCommonThings();
break;
}