This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
"Write the correct logic expression (that could be used as a Java conditional expression) that evaluates to true if the number x is between 1 and 100 (both exclusive) or the number is negative."
I have this answer:
if ((x >=1 && x <= 100) || x < 0) { }
But I am being told this is wrong for this reason:
"this range was supposed be exclusive"
Stuart A. Burkett
Ranch Hand
Joined: May 30, 2012
Posts: 321
posted
0
'exclusive' means the specified limits are not considered valid values
Mack Grill
Greenhorn
Joined: Feb 27, 2012
Posts: 29
posted
0
Great but how am should I keep them separate when I need to use || ?
Stuart A. Burkett
Ranch Hand
Joined: May 30, 2012
Posts: 321
posted
0
Mack Grill wrote:
Great but how am should I keep them separate when I need to use || ?
I don't understand what you're asking.
The problem is in this part
The specification says
Mack Grill wrote:the number x is between 1 and 100 (both exclusive)
but your code allows 1 and 100 as valid values.
Akhilesh Trivedi
Ranch Hand
Joined: Jun 22, 2005
Posts: 1493
posted
0
a <= 10
means "a can have all the values below AND 10" i.e. "10 is also included"
a <10
means "a can have all the values below 10" i.e. "10 is not included"
Mack Grill wrote:I have this answer:
if ((x >=1 && x <= 100) || x < 0) { }
I hope I'm not muddying the waters here, but when you think about compound conditions, it's a good idea to try and eliminate the most likely quickest.
All other things being equal (which they often aren't)
x < 0 is true for a lot more values than
(x >=1 && x <= 100) and its also quicker to check, so
if (x < 0 || (x >=1 && x <= 100)) is likely to run faster than what you had. I actually find it easier to read too.
Winston
Isn't it funny how there's always time and money enough to do it WRONG?