<br /> Well no, I don't know that one. The final "else" can't have any condition: <br /> <br /> I'm not sure what would make it a bad coding practice. Well, people will be happier if you use braces around each "then" section. But that's not what you're asking about. <br /> <br /> The compiler speed won't be an issue, really. More relevant are readability, minimizing possible programmer error, and execution speed. <br /> <br /> In a case like this, two things that jump out to me about the list of conditions are that it's exclusive (we can't have more than one condition true) and exhaustive (there must always be one true). In such cases, I see no reason not to use a simple else for the last case, since there's nothing new to test. If you've already tested for a > 0 and a < 0, the only remaining possibility (for int at least) is a == 0, no need to actually test it. But some people may find it more readably to say it explicitly. I would usually comment it: <br /> <br /> On the other hand, sometimes you may think you have an exhaustive list of the possibilities, but you don't. If variable a had been a double, then it could possibly be NaN, not a number. In which case a > 0, a < 0, a==0 are all false. So it may be best to explicitly state the condition each time, just in case one of your assumptions is faulty.Miles Jiang wrote:We all know the standard if-else structure is like if -> else if -> else:
Paul Clapham wrote:1. Not really. When you see "else" you know it has to be associated with the "if" which precedes it structurally.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
elseif
The "else if" keyword is spelled elsif in Perl. There's no elif or else if either. It does parse elseif, but only to warn you about not using it.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
It is an age old problem in programming languages.
https://en.wikipedia.org/wiki/Dangling_else
Specifically, for Java: https://stackoverflow.com/questions/32076762/java-statements-processing-precedence-dangling-else
Miles Jiang wrote:
It is an age old problem in programming languages.
https://en.wikipedia.org/wiki/Dangling_else
Specifically, for Java: https://stackoverflow.com/questions/32076762/java-statements-processing-precedence-dangling-else
I guess this illustrates why using curly braces is always a good idea, especially for rookie developers.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
(I would note that the introduction of lambdas has made me much more tempted to put braces on the same line for short statements.)
Then too, I am a big fan of using ternary operators like this:
Which may be less "readable" to people not used to seeing it. But I find readability is often subjective, and based on what you are used to seeing. I encourage people to expand their notion of readability to include chained ternary operators, mimicking the else if structure we already accept.
I like that construct, and the old Sun style guide recommends it too, but you can improve its legibility even more by adding a little space so the operators or operator parts align vertically:-I presume you meant to use >= throught rather than >? Surely D doesn't start at 61, or 60.000000000001? In which case I would reverse the whole statement structure:-I don't think there is a standard indentation convention for nested ?:s. Unfortunately ?: is one of those things beginners aren't made to use ugh, so they neer gain familiarity with it.Mike Simmons wrote:. . . Then too, I am a big fan of using ternary operators like this:
Which may be less "readable" to people not used to seeing it. . . .
That looks strange. I don't think line 6 will compile; if it does, that code won't run correctly.Miles Jiang wrote:We all know the standard if-else structure is like if -> else if -> else:
. . .
Consider Paul's rocket mass heater. |