I have a general question about if-else statements in programming. What is the difference between using else statement versus else-if statement? Why use else-if? Isn't it the same as else statement? How are they different? If they both serve different purposes, or if there is a difference between the two, then when do I use else-if instead of just simply else statement?
Confusion is understandable, so lets clear this up. Basically there are only few situations you need to know.
Not sure if that explanation works for you, but this is what I managed to come up with in seconds. Check and tell us if that is clear.
has the exact same effect as the shorter
The first way is more commonly used only if there is something you need to do before testing the second/thjird options:
Timothy Han wrote:Hi Liutauras, thank you for your examples. However, it is still not clear to me as when do I use else versus else-if. Can you elaborate in words further?
if...else is used when you only have two situations governed by a single value or expression.
An alternative, in some situations, is the ternary operator.
if...else if...[else if...]else... is used when you have more than two mutually exclusive situations.
An alternative is the switch statement, which is clearer to most people, but not always possible to use.
if-else-if gives you finer control for situations beyond two conditions, as happens when possibilities extend beyond a simple 'black' / 'white', or 'yes' / 'no' situation.
A good thing for you to do would be to write some examples to see how they can work. Try writing if-else or if-else-if statements for these:-
print "ON" or "OFF" depending on a boolean variable called powerOn.
print "PASS", "FAIL" or "DISTINCTION" depending on an int variable called examMark (eg below 40 fail, 40 and above pass, 80 and above distinction)
If you have n possibilities, all mutually exclusive, then switch works nicely. If you have ranges, then else if is probably easier to use.Note you should finish with a plain simple else, otherwise the compiler may get confused and think there is a path through the method without assigning grade.
Winston Gutkowski wrote:. . . An alternative is the switch statement, . . .
- X 2
There is no way more than one of those conditions can be true. As soon as java finds a "true" condition, it executes that block, then jumps out - nothing else gets checked.
If it's possible that more than one condition is true and you need to do stuff for each, you'd use a bunch of if statements...
In the first case:
Either Line 3 or Line 7 is going to execute. There is no way for both of them to execute, nor is there any way for neither of them to execute. One of them will, and one of them won't, and that's certain.
In the second case:
Line 3 will execute if and only if x is 3. Line 7 will execute if and only if x is 4. If x is not 3 and x is not 4, neither Line 3 nor Line 7 will execute.
Note that it is not the mere presence of "else if" that changes the behavior of a conditional from "one will execute" to "at most, one will execute." It is the fact that the conditional expression ends with "else if." We can add an "else" clause to the previous example:
Here, one of Line 3, Line 7, or Line 11 is certain to execute.
As a side question, I've always been curious that "else if" is presented in the textbooks as an actual statement. That is, is there any difference between this