What is the benefit/downside to using a switch
statement vs. an if/else
in C#. I can\'t imagine there being that big of a difference, other than m
The compiler is going to optimize pretty much everything into the same code with minor differences (Knuth, anyone?).
The difference is that a switch statement is cleaner than fifteen if else statements strung together.
Friends don't let friends stack if-else statements.
Actually, a switch statement is more efficient. The compiler will optimize it to a look up table where with if/else statements it cannot. The down side is that a switch statement can't be used with variable values.
You can't do:
switch(variable)
{
case someVariable
break;
default:
break;
}
it has to be
switch(variable)
{
case CONSTANT_VALUE;
break;
default:
break;
}
often it will look better - ie will be easier to understand what's going on. Considering the performance benefit will be extremely minimal at best, the view of the code is the most important difference.
So, if the if/else looks better, use it, otherwise use a switch statement.
This doesn't actually answer your question, but given there will be little difference between the compiled versions, I would urge you to write your code in a way that best describes your intentions. Not only is there a better chance of the compiler doing what you expect, but it will make it easier for others to maintain your code.
If your intention is to branch your program based on the value of one variable/attribute, then a switch statement best represents that intention.
If your intention is to branch your program based on different variables/attributes/conditions, then a if/else if chain best represents that intention.
I will grant that cody is right about people forgetting the break command, but almost as frequently I see people doing complicated if blocks where they get the { } wrong, so lines that should be in the conditional statement are not. It's one of the reasons I always include { } on my if statements even if there's one line in it. Not only is it easier to read, but if I need to add another line in the conditional, I can't forget to add it.
I Think Switch Is More Faster Than If Conditions like see if There is a program like :
Write a Program to enter any number (between 1 – 99) and check it is in which slot a) 1 – 9 then slot one b) 11 – 19 then slot two c) 21-29 then slot three and so on till 89-99
Then On If You Have Have To Make Many Conditions But Son Switch Case You Have TO Just Type
Switch ( no /10 )
and on case 0 = 1-9 ,case 1 = 11-19 and so on
it will Be So Easy
There Are Many More Such Examples Also!
One possible downside of switch statements is its lack of multiple conditions. You can have multiple conditions for the if (else) but not multiple cases statements with different conditions in a switch.
Switch statements are not suitable for logic operations beyond the scope of simple Boolean equations/expressions. For those Boolean equations/expressions, it is eminently suitable but not for other logic operations.
You have much more freedom with the logic available in If statements but the readability can suffer if the If statement becomes unwieldy or is handled poorly.
Both have there place depending on the context of what you are faced with.