Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Need help with a simple problem.

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am trying to answer this question:

"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"
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'exclusive' means the specified limits are not considered valid values
 
Mack Grill
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Great but how am should I keep them separate when I need to use || ?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sorry i meant to say how should i use the || in the code and how do I involve a negative number?

"f the number x is between 1 and 100 (both exclusive) OR the number is negative"
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mack Grill wrote:
sorry i meant to say how should i use the || in the code and how do I involve a negative number?

"f the number x is between 1 and 100 (both exclusive) OR the number is negative"


You've already done that. That part of the expression is fine.
 
lowercase baba
Posts: 13074
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another way of saying "between 1 and 100 (both exclusive)" is "the number is greater than 1 and less than 100".

In other words, 1 should return false, and 100 should return false.

The whole request could be re-written as

The number is greater than 1 and less than 100....OR the number is negative.
 
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

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
 
I was born with webbed fish toes. This tiny ad is my only friend:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic