This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes if statement, 2 conditions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if statement, 2 conditions" Watch "if statement, 2 conditions" New topic

if statement, 2 conditions

James Hewitt

Joined: Jul 09, 2001
Posts: 27
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?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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


So much trouble in the world -- Bob Marley
James Hewitt

Joined: Jul 09, 2001
Posts: 27
Thanks for that. Got a java book. It really sucks.
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
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"

Joined: Sep 29, 2000
Posts: 8521
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:

"JavaRanch, where the deer and the Certified play" - David O'Meara
Roy Tock
Ranch Hand

Joined: Jul 16, 2001
Posts: 83
James, there's no other way to do it. You've got to repeat x twice.
I agree. Here's the link:
subject: if statement, 2 conditions
It's not a secret anymore!