Results 1 to 10 of 10
Thread: Multiple if statements
- 08-01-2013, 05:53 PM #1
Member
- Join Date
- Jul 2013
- Posts
- 52
- Rep Power
- 0
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.
- 08-01-2013, 06:02 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 08-01-2013, 06:03 PM #3
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.
- 08-01-2013, 07:38 PM #4
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-01-2013, 08:32 PM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
- 08-01-2013, 09:07 PM #6
- 08-01-2013, 09:59 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Multiple if statements
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-01-2013, 11:07 PM #8
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 08-02-2013, 10:30 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Multiple if statements
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-02-2013, 06:00 PM #10
Member
- Join Date
- Jul 2013
- Posts
- 52
- Rep Power
- 0
Similar Threads
-
Multiple try & catch statements within a switch
By Martyn in forum New To JavaReplies: 21Last Post: 01-12-2012, 10:24 PM -
Multiple if statements
By gunnerp420 in forum New To JavaReplies: 2Last Post: 09-28-2011, 11:54 PM -
how to call multiple sql statements in java
By sandeep43 in forum JDBCReplies: 5Last Post: 08-01-2011, 11:13 AM -
if statements for multiple variables
By dookie1293 in forum New To JavaReplies: 9Last Post: 06-17-2011, 10:30 AM -
Effect of Multiple Condtitions in if() statements to run time
By kimhonoridez in forum New To JavaReplies: 4Last Post: 12-08-2010, 09:40 AM
Bookmarks