# Should I use a switch or a if/if-else...

This topic is 3644 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am writing things out and I noticed that when I use a switch there are instances were more then one "case" has the exact same body of code to be executed... switch(blah){ case 1: doA break; case 2: doB break; case 3: doA breakl } Alternative to this would be something like if(blah == 1 || blah == 2){ doA } else if ( blah == 3){ doB } Regards Chad

##### Share on other sites
switch(blah){  case 1:  case 3:    doA    break;  case 2:    doB    break;}

##### Share on other sites
Quote:
 Original post by SiCraneswitch(blah){ case 1: case 3: doA break; case 2: doB break;}

Ahhh...I see...use the break to my advantage

##### Share on other sites
Yep. This is the normal way of making use of the fact that you need to 'break' the cases. Other uses are possible, but generally considered fairly evil. Of course, they were intended to be possible: the syntax would be different, otherwise.

(As a language design issue: note that you can't just say "multiple case labels in a row refer to the same case-block, which ends at the next case label" - because then you can't make an empty case-block.)