File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to write: IF statement with range of values (e.g. if 5 > x > 10, then ... ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to write: IF statement with range of values (e.g. if 5 > x > 10, then ... )" Watch "how to write: IF statement with range of values (e.g. if 5 > x > 10, then ... )" New topic
Author

how to write: IF statement with range of values (e.g. if 5 > x > 10, then ... )

Zak Tacc
Greenhorn

Joined: Feb 01, 2010
Posts: 25
Hey, got a question for you guys...

How do I write an if statement for a variable in java with two constraints? Like if i wanted my program to check a variable to see if it's greater than some number and less than another?

For example:

If X>24 and <30, then ...


Thanks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Welcome to JavaRanch.

The && operator means "and", so you write:



Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Zak Tacc
Greenhorn

Joined: Feb 01, 2010
Posts: 25
thank you
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
You can also use single ampersand towards the same end. Like

if (x > 24 & x < 30)

However && is more efficient as it doesn't calculate the expression after && if the first expression has failed the condition. Whilst single & calculates both the expressions.


Rajeev Trikha (SCJP 6)
Zak Tacc
Greenhorn

Joined: Feb 01, 2010
Posts: 25
that's good to know thank you
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Probably best to avoid the single ampersand for "AND" unless you need to maintain precedence with the ^ operator.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to write: IF statement with range of values (e.g. if 5 > x > 10, then ... )