Why do we use "else if", if we can do the same thing using "if" conditional statement ?? | SoloLearn: Learn to code for FREE!


Why do we use "else if", if we can do the same thing using "if" conditional statement ??

8/22/2019 4:01:54 AM


10 Answers

New Answer


If u use all the where if statement the compiler gonna check all the statements.. Whereas If u use if-else the compiler gonna stop when it found one statement true.. Moral of the story is..it saves executives time Thank you


They are not entirely the same. if (a) { } if (b) { } The above two statements will both execute if a and b are true. We say that these two statements are independent. if (a) { } else if (b) { } In this case, if a is true, the else statement will never execute although b is true. The else statement which evaluates b will only be executed if a is false.


Sometimes the code looks neater with else if, but Fermi mentioned the main reason.


Result will be the same, if you use 2 - if conditions or if-elseif are used. But only thing is that, if you use 2 - if's then both conditions needs to be evaluated during run-time. Which will affect performance in time critical systems. So, it preferred to use if-elseif whenever possible... Hope this helps...!!!


If we use if(statement) {do this;} if(statement) {do this;} It checks both the conditions even if the first one is true. This takes more time. if(statement){do this;} else if(statement){do this;} Here, control will go to the else if only if the first if is false.


You are right, every „else if“ condition could be rewritten as a nested sequence of if-conditions, but it would be much more complex.


The execution speed of "elif" statement is more than "if" statement


a=15; if(a>5){ print a; }else if(a>10){ print a; } Output: 5 a=15; if(a>5){ print a; }if(a>10){ print a; } Output: 5 5


To check the conditions of multiple statement in a easy manner and short program. Whereas in if we can't compare multiple statement with different different output and if we do, it's take too much time as well as lengthy program.


Yes you can but i think the else if statement is better