I have found lots of books in java saying switch statement is faster than if else statement. But I didnot find antwhere saying why switch is faster than if.
Example
I have a situation i have to choose any one item out of two i can use either of the following way
switch(item){
case BREAD:
//eat Bread
break;
default:
//leave the restaurant
}or using if statement like the following
if(item== BREAD){
//eat Bread
}else{
//leave the restaurant
}In the above example which is faster in action and why?
Can you answer this question?
Write Answer1 Answers