Possible Duplicates:
is “else if” faster than “switch() case” ?
What is the relative performance of if/else vs. switch in Java?
Ive been coding-in-the-run again....when the debugger steps through a case statement it jumps to the item that matches the conditions immediately, however when the same logic is specified using if/else it steps through every if statement until it finds the winner. Is the case statement more efficient, or is my debugger just optimizing the step through? (don't worry about the syntax/errors, i typed this in SO, don't know if it will compile, its the principle i'm after, I didn't want to do them as ints cause i vaguely remember something about case using an offset with ints) I use C#, but im interested in a general answer across programming languages.
switch(myObject.GetType()){
case typeof(Car):
//do something
break;
case typeof(Bike):
//do something
break;
case typeof(Unicycle):
//do something
break;
case default:
break;
}
VS
Type myType = myObject.GetType();
if (myType == typeof(Car)){
//do something
}
else if (myType == typeof(Bike)){
//do something
}
else if (myType == typeof(Unicycle)){
//do something
}
else{
}