Switch-case VS if-else

Hi,
When to use Switch-case statements over if-else and vice-versa?
if i'm simply testing the value of integer(or enum) i use switch, else (ho ho...) i use if..else.

agree witch Mutexe and also - when you have to use more than 3 IFs, rather use SWITCH
Okay thanks.
Not really.
You use switch(case) and if(else) whenever you want, except that:
switch(case) CAN'T take non-constant arguments. What does it mean?
It means, that your variable value must be known at compile-time. if-else doesn't require that.
Check this:

http://stackoverflow.com/questions/8049834/case-expression-not-constant

Cheers!

PS. Oh, and switch-else can handle only integral types, if I'm not wrong(char is integral type). So you can't check for const char* , since string isn't integral.

More info:
http://msdn.microsoft.com/en-us/library/k0t5wee3.aspx
http://msdn.microsoft.com/en-us/library/cc953fe1.aspx
Last edited on
Topic archived. No new replies allowed.