Users Pricing

forum

Home Forums Why switch is faster than if – MindStick

Why switch is faster than if

Anonymous User 1822 04 Dec 2015
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
}
considering item and BREAD is constant int value

In the above example which is faster in action and why?

1 Answers

Anonymous User 04 Dec 2015 Accepted Answer
Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md