Sign in to follow this  
chadsxe

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

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 this post


Link to post
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.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this