Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Else vs If-Else-If  RSS feed

 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Timothy

Confusion is understandable, so lets clear this up. Basically there are only few situations you need to know.

  • Only one of those being executed, can cover 2 cases only

  • Only one of those being executed, can cover more than 2 cases

  • Each of those if's being checked no matter if top if has been true

  • 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.
     
    Timothy Han
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    Fred Kleinschmidt
    Bartender
    Posts: 560
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another way to look at it:
    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:

     
    Carey Brown
    Bartender
    Posts: 2991
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    IF (some condition is met) do-this, ELSE (this means the condition was not met) do-something-else.

    Note that you can write this


    This is considered poor style and it becomes very difficult to follow if the indentation gets too deep. It is better written like this:


     
    Liutauras Vilda
    Marshal
    Posts: 4634
    316
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Timothy Han wrote:However, it is still not clear to me as when do I use else versus else-if

    An example when not to use
    Example when to use else-if
    Now when use if
    When not to use else-if
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    HIH

    Winston
     
    Steffe Wilson
    Ranch Hand
    Posts: 165
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another perspective on it...

    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)
     
    Campbell Ritchie
    Marshal
    Posts: 55681
    162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:. . . An alternative is the switch statement, . . .
    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.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12542
    48
    Chrome Java Linux
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You would use if-else if - else when there can only be one desired outcome.



    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...

     
    Stevens Miller
    Bartender
    Posts: 1444
    30
    C++ Java Netbeans IDE Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    At the risk of confusing this discussion, one should also keep in mind that any conditional expression that ends with "else" is certain to execute one of its conditional blocks, while any conditional expression that ends with "else if" is certain to execute at most one of its conditional blocks (but it may not execute any of them).

    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

    and this

    ?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!