We all use if ..else if.. else.
But still I'm confused as to why we use else if. Where if does the same thing as else if.
So why are we using else if?
Any specific reasons behind this?
Is there any algorithm where it's mandatory to use else if?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityWe all use if ..else if.. else.
But still I'm confused as to why we use else if. Where if does the same thing as else if.
So why are we using else if?
Any specific reasons behind this?
Is there any algorithm where it's mandatory to use else if?
The main reason to use else if
is to avoid excessive indentation.
For example:
if(a) {
} else {
if(b) {
} else {
if(c) {
} else {
}
}
}
Can become:
if(a) {
} else if(b) {
} else if(c) {
}
Of course both of the pieces of code above are equivalent (which means it's impossible for the latter to be mandatory other than in style guides).
if (bool) { ... } else try { ... } catch { ... }
are entirely valid. It's probably not advisable, because most people aren't used to seeing it written that way, but the same principle applies.
– KChaloux
Jan 10 '14 at 13:41
the question asks, "why do you need else given that you can accomplish the same functionality using only if" (paraphrased). First of all, the two are only really equivalent in fairly simple programs; there are many situations where else if functionality cannot be easily replicated using only if statements.
that said, in such simple situations it is still usually better to use else if.
if (a==b){}
if (!(a==b) && a==c ){}
there are two problems with this that are solved by using else if. First, you perform the comparison between a and b twice, which is unnecessary work. Second, unless you memorize the rules or rely heavily on parenthesis, it's not obvious how the second boolean expression resolves.
if (a==b) {}
else if (a==c) {}
Is cleaner, clearer, and more efficient. It's basically the programming holy grail.
a
or b
, or c
changes within the first block (if (a==b)
), both blocks may get executed. if - else if
is insensitive to that. As for "unnecessary work" - this sort of performance hit is typically irrelevant, unless eg. a
and b
are methods that take long to evaluate. It's also a rather poor sign if a programmer hasn't memorized the rules required to understand how (!(a==b) && a==c )
evaluates.
– Konrad Morawski
Jan 10 '14 at 14:43
It's for efficiency. If you have a chain of if...else if...else if
clauses, then evaluation stops after one evaluates to true. If you simply have a list of if
clauses, then even after one evaluates to true, you'll still check the rest of them, even though they'll be false. If the conditions involve invoking functions or other "expensive" operations then you'll waste resources (especially if your conditions involve web service calls or database lookups). This is also why you should try to structure complicated conditional expressions like this so that the "cheapest" conditions are checked first.
If we think programmatically the main purpose is if we have to choose one thing among many things at that time we should use else if
.
Also there can be many ways of doing same things like switch..case
in that case we can consider it as another construct.
else
at all (not just theelse if
combo). After all, one can always writeif (a) ... if (!a)
instead. Well, it's just more expressive. – Konrad Morawski Jan 10 '14 at 14:48if (a) doSomething else doSomethingElse
ensures that ifdoSomething
was executed,doSomethingElse
won't.if (a) doSomething; if (!a) doSomethingElse
makes it possible (ifa
was true beforedoSomething
, butdoSomething
changed its value to false). – Konrad Morawski Jan 10 '14 at 14:54switch case
have been extended to handle mutually exclusive branching thus makingelse if
optional. In C and C++, this is not the case because of historical reasons. – rwong Jan 10 '14 at 20:25