samedi 10 octobre 2015

is there any efficiency difference between using if or switch in java

i really wonder that if there is any efficiency difference between using if statement or switch. For example:

if(){
\\code
}
else if(){
\\code
}
else{
\\code
}

I believe that program needs to go and check all of the if statement even if the first if statement was true.

switch(i){

case 1:
//code
break;
case 2:
\\code
break;

but in the switch, there is a break command. is my approaching right? if not, could you explain the efficiency difference between them?

Aucun commentaire:

Enregistrer un commentaire