Results 1 to 10 of 10
  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Multiple if statements

    I have come across this in a Java book. May i know why are there implementing multiple if statements instead of else if statements.
    It seems that using else if is more efficient and also the month is mutually exclusive. so i have no idea if there are implementing multiple if instead of else if or switch.

    Java Code:
        public static int getNumberOfDaysInMonth(int year, int month){
            if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                return 31;
            if(month == 4 || month == 6 || month == 9 || month == 11)
                return 30;
            if(month == 2) return isLeapYear(year) ? 29: 28;
            
            return 0;
        }
    Last edited by dojob; 08-01-2013 at 05:57 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Multiple if statements

    Less typing perhaps? Either way works in this particular case because the code returns in each if statement, there is no way that any of the ifs further down the line is reached once one of them resolves to true.

    And please, forget about efficiency when you're talking about snippets of code. Focus on code readability.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Multiple if statements

    In this example, it doesn't matter too much because they are returning the days inside the if statements, and once that happens, none of the other conditions will be evaluated. But you are correct, if/else would work as well.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Multiple if statements

    ... and if you're looking for both efficiency and readability, that really ought to be a switch statement with fall-through.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Multiple if statements

    Quote Originally Posted by DarrylBurke View Post
    ... and if you're looking for both efficiency and readability, that really ought to be a switch statement with fall-through.

    db
    THAT's a personal opinion not shared by everyone.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Multiple if statements

    Quote Originally Posted by gimbal2 View Post
    THAT's a personal opinion not shared by everyone.
    To an extent it is, but I think he is right. Switch statements in general are easier to read, and look cleaner. They provide well defined code boundaries so it can be easier to traverse the logic.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Multiple if statements

    Quote Originally Posted by gimbal2 View Post
    THAT's a personal opinion not shared by everyone.
    I don't know if it's so personal; have a look at those if statements and assume they're executed in a Harvard memory model; what happens is: fetch from RAM (month), fetch from ROM (the numeric literal) and over and over again; if there is a cache this scenario would kill its coherence. It doesn't matter much on a von Neumann memory model (i.e. ordinary PCs) but a switch statement or a lookup table would be better for a Harvard memory model ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Multiple if statements

    I respect your and Dedicated Beerdrinker's opinion. I just don't share it ;) Because of the fall-through 'programming mistake waiting to happen' aspect of it, should you be curious why I just have to be non-conformant. I also think it is needlessly verbose, but so is a large chain of if-else statements so in any case, I lose.

    edit: crud, I lost the quote. That was @sehudson
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Multiple if statements

    Quote Originally Posted by gimbal2 View Post
    I respect your and Dedicated Beerdrinker's opinion. I just don't share it ;) Because of the fall-through 'programming mistake waiting to happen' aspect of it, should you be curious why I just have to be non-conformant. I also think it is needlessly verbose, but so is a large chain of if-else statements so in any case, I lose.

    edit: crud, I lost the quote. That was @sehudson
    If someone doesn' know how a switch statement works in C/C++/Java/etc, that someone is not supposed to touch any code imho. I personally prefer a (dense) lookup table here; it doesn't hurt different memory models and it's just as concise as a switch statement (and it only takes 13 bytes of storage ;-).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Multiple if statements

    Thanks for all the reply. I thought that there is particular reason for author of the book using if in this case.

Similar Threads

  1. Multiple try & catch statements within a switch
    By Martyn in forum New To Java
    Replies: 21
    Last Post: 01-12-2012, 10:24 PM
  2. Multiple if statements
    By gunnerp420 in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 11:54 PM
  3. how to call multiple sql statements in java
    By sandeep43 in forum JDBC
    Replies: 5
    Last Post: 08-01-2011, 11:13 AM
  4. if statements for multiple variables
    By dookie1293 in forum New To Java
    Replies: 9
    Last Post: 06-17-2011, 10:30 AM
  5. Replies: 4
    Last Post: 12-08-2010, 09:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •