• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Else vs If-Else-If

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    Bartender
    Posts: 732
    10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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:

     
    Saloon Keeper
    Posts: 10705
    86
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 8857
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Ranch Hand
    Posts: 165
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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)
     
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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...

     
    Bartender
    Posts: 1464
    32
    Netbeans IDE C++ Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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

    ?
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic