• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if statement, 2 conditions

 
James Hewitt
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to run an if statement if an integer is between 2 values. I can do this by using two if statements, but can I avoid this repetition? I want something like this:
if(x > 1 AND x < 7){
out.println("x is between 1 and 7");
}
Clearly "and" isn't right, but what is? I note that you can seperate expressions in a "for" statement with ";", but this doesn't work in an if statement. Any ideas?
Thanks,
James
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to get a book on Java. There are some free ones online. Here you go...

if( (x > 1) && (x < 7) ){
Do whatever

Bosun
 
James Hewitt
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that. Got a java book. It really sucks.
Thanks,
James
 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey James, when I first started posting here I made the grande faux pas of asking who Simon Roberts, Philip Heller and Michael Ernest were. I think I'm still receiving hate mail over that! Anyway, the point is, they wrote a really good book on Java. I'm not sure if I can post the title here, I have no idea what the "rules" on promoting products are, but you should be able to find it easily, it's a study guide.
p.s. I also made the mistake of questioning whether the second edition is as good as the first. Apparently it is. I haven't compared the two, I just accepted everyone's opinion (including Mr. Roberts) and purchased it... and since reading it, I've recommended it to everyone that asks.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We talk about authors and their books ALOT here, seems techies just LIKE books . Anyway we have a whole section called the BunkHouse devoted to Book Reviews as well as a Book Review forum for conversations about books. The comment above should probably be added to the thread for the RHE book here:
http://www.javaranch.com/ubb/Forum49/HTML/000081.html
 
Roy Tock
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James, there's no other way to do it. You've got to repeat x twice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic