6

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?

Haych
  • 932
  • 13
  • 36
  • 2
    If the conditions are exclusive, then use `else if`, this'll save multiple condition checking – Tushar Nov 22 '15 at 12:15
  • 3
    Draw a flowchart of your code with and without `else`. Then compare what parts may *not ever* get executed - and thus, that program will be faster, – Jongware Nov 22 '15 at 12:17
  • 1
    same as this question : http://stackoverflow.com/questions/9169249/why-we-use-if-else-if-instead-of-multiple-if-block-if-the-body-is-a-return-stat – Kasun Gamage Nov 22 '15 at 12:52
  • They're only equivalent if your statements break or return. – shmosel Jun 23 '16 at 02:56

3 Answers3

14

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.

Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
  • 1
    By the way, if the condition test is very simple, with or without else, the code will be very fast to execute. But if the test is more complex, with file i/o, database requests or other, you should use else to avoid unnecessary execution – Prim Nov 22 '15 at 12:24
2

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 - i.e. do you want more than one of the blocks accessed by the multiple conditions to be executed if more than one condition is true.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

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.
}
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
  • can you explain why it is better to use switch case statements over if else if chain? – Gurnard Mar 03 '21 at 12:15
  • 1
    @Gurnard The better I said has nothing to do with performance but to improve readability of code. However using `HashMap`s (wherever possible) can help a lot with performance since access each one is almost instant – Morteza Tourani Mar 07 '21 at 09:46