简体   繁体   中英

If Else VS Multiple If Statements

I always use multiple if statements when coding:

if(logicalCheck){
  ...
}

if(secondLogicalCheck){
  ...
}

and rarely use If Else . I understand that using my way means that more than one of my logical checks can be fulfilled and only one in an if else chain can occur.

My question is, is there any performance benefits in using one method over the other in C, Java or JavaScript? Is there anything particular wrong with using multiple if statements?

Using only if , interpreter or whatever will test all conditions but if you use else if (if possible) then on passing any condition , next else if will not be tested or checked.

if (age < 10){
   // do something
}
if (age < 20 && age > 10){
   // do something
}
if (age < 30 && age > 20){
   // do something
}

All conditions will be tested/compared

but in this scenario

if (age < 10){
   // do something
}
else if (age < 20 && age > 10){
   // do something
}
else if (age < 30 && age > 20){
   // do something
}

if age is 5, only first condition will be tested.

If at most one of the conditions is expected to be true, using if else-if will save the evaluation of some of the conditions. Since the conditions may be expensive to evaluate, evaluating multiple conditions without an actual need to do so may cost you performance wise.

If more than one condition can be true at the same time, the decision whether to use multiple if statements or a single if - else-if .. else construct depends on the required logic - ie do you want more than one of the blocks accessed by the multiple conditions to be executed if more than one condition is true.

The term of use is this:

If you have multiple independent logics which is used for non-related restrictions or actions upon the condition then you can use multiple if statement separately:

if(conditionA){
    // do what suppose to do
}

if(conditionB){
    // do what suppose to do
}

. . .


If you want one of the conditions you made to apply then you should use if else or if else if statemnets:

if(conditionA) {
        // do what suppose to do on conditionA
} else {
    // do what suppose to do if the conditionA doesn't satisfied.
}

if(conditionA) {
        // do what suppose to do on conditionA
} else if(conditionb) {
        // do what suppose to do on conditionB
}  else {
    // do what suppose to do if non of the conditions were satisfied.
}

By the way if you want to use if else if chain it's better to use switch case statements:

switch(true){
    case conditionA:
        // do what suppose to do on conditionA
        break;
    case conditionB:
        // do what suppose to do on conditionB
        break;
    default:
    // do what suppose to do if non of the conditions were satisfied.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM