Is there a way to assign two different case values to the same block of code without copy and pasting? For example, below 68 and 40 should execute the same code, while 30 is
case 68:
case 40:
// stuff
break;
Switch cases can be clubbed as shown in the dig.
Also, It is not limited to just two cases, you can extend it to any no. of cases.
You should use:
switch condition {
case 1,2,3:
// do something
case 4,5:
// do something
default:
// do something
}
Cases should be comma-separated.
Just put them right after each other without a break
switch (myVar) {
case 68:
case 40:
// Do stuff
break;
case 30:
// Do stuff
break;
}
Yes, you just put the related case
statements next to each other, like this:
case 40: // Fallthrough
case 68:
// Do something
break;
case 30:
// Do something different
break;
The Fallthrough
comment is there for two reasons: