1

Possible Duplicate:
When to use If-else if-else over switch statments and vice versa

I'm sure they're fundamentally very different things, but in practical use I've never found a case where there's been any difference between

switch (value){
    case 1:
        //Do stuff
        break;
    case 2:
        //Do other stuff
        break;
}

and

if (value == true){
    //Do stuff
}
else{
    //Do other stuff
}

What are some example scenarios where one is more appropriate than the other? How, conceptually, are the different? Are there performance of semantics advantages?

Community
  • 1
  • 1
lavelle
  • 1,446
  • 1
  • 17
  • 30
  • I'm pretty sure that this is a duplicate of http://stackoverflow.com/questions/427760/when-to-use-if-else-if-else-over-switch-statments-and-vice-versa. – quanticle Feb 28 '11 at 21:27
  • Switch statements allow you to fall through in a much more fluent manner. – corsiKa Feb 28 '11 at 21:33

1 Answers1

0

They are analogous, but not equivalent. The switch/case statement is generally used when deciding what routine to invoke given a particular integer, and is commonly employed for checking an enum. For these cases, it may be more expressive and more readable to use a switch.

The if/else evaluates a boolean expression.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109