Early compilers knew how to build microcode for switch statements that was much faster than if...else combinations.
However, modern compilers can reach similar speeds with both. So, then, it comes down to programming concepts:
If..Else usages like:
if ( x < 10 ) {
// ...
} else if ( joe.age == mary.age ) {
// ...
} else if ( null != reporter ) {
// ...
} else {
// something else
}
..if you look clsely, you'll see that each condition is dealing with a separate set of tests and test variables (x, joe, mary, reporter).
In a switch statement, you are using just 1 condition compared to a long set of values:
switch( x ) {
case 1:
// ....
break;
case 2:
// ....
break;
case 3:
// ....
break;
default:
//....
break;
}
In this case, the statement is dealing with just 1 test, so it performs the switch( expression ) just once, than jumps down a list of addresses based on the value you provide. For many languages using "switch" (or "select") all the values have to numeric, so that the compiler can build a lookup table. Without this, it would be no faster than a series of IF statements (IE, perform expression, then compare to each value as in "if (x==1)" etc)
Both are useful. The best part of IF statements is the short-circuit. If you use a compound IF, where there are many sub expressions in parentheses, then a compiler can speed things up a tiny bit bit by skipping some of the expressions. As in:
if ( ( x < 10) && ( y < 10 )) {
in the above, if x>=10, the compiler doesn't need to test y<10, since it knows that the "&&" means it HAS to fail the whole statement if that part fails. So, it just pops false to the top of the evaluator and jump to the else, or bottom of the if.
2006-11-16 03:53:03
·
answer #1
·
answered by WickedSmaht 3
·
0⤊
0⤋
the switch structure makes for more efficient code (final machine code) than the if-else. i would use it of there are lots of different conditions that need acted upon. for instance if i had a variable where i needed to do something based on the number (say 1- 100), i would use the switch. if its just a few conditions (say 1 -5) i would use if-else. if you use the if-else for the first example, then the code would go through each if-else until it found the right one (as many as 99), where the switch goes directly to the one you want (using a lookup table).
i could get more detailed, but it would require showing assembly code for the equivalent statements, and i dont have the time.
2006-11-17 10:02:47
·
answer #2
·
answered by justme 7
·
0⤊
0⤋
when you have to do different actions for different values of a variable
just an example
switch(userChoice){
case 1:
// call method to check bank account
break;
case 2:
// exit program
break;
default:
// display "incorrect choice"
break;
}
2006-11-16 11:48:25
·
answer #3
·
answered by Insignificant Other 1
·
0⤊
0⤋
if you have something that might fail the first if statement, then you also want to check it for something else.
if(price<10){
Console.writeline("It's ok go ahead and buy it!");
}
else{
Console.writeline("Don't do it, you'll be in debt!");
}
//Something like that.
2006-11-16 11:48:28
·
answer #4
·
answered by scott p 3
·
0⤊
0⤋