+ 5
Why do we use "else if", if we can do the same thing using "if" conditional statement ??
10 Answers
+ 12
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
+ 15
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.
+ 6
Sometimes the code looks neater with else if, but Fermi mentioned the main reason.
+ 5
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...!!!
+ 3
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.
+ 2
You are right, every âelse ifâ condition could be rewritten as a nested sequence of if-conditions, but it would be much more complex.
+ 2
The execution speed of "elif" statement is more than "if" statement
+ 2
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
0
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.
0
Yes you can but i think the else if statement is better