File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes AND / OR Statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "AND / OR Statements" Watch "AND / OR Statements" New topic

AND / OR Statements

LaTeef Lusk

Joined: Mar 28, 2009
Posts: 15
I am trying to do multiple AND/OR statements in an if statement. I am trying to see if parenthesis need to be around each individual AND/OR statement or can I just have one parenthesis around the entire if statement?
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
It depends on what you're trying to do. You need to put parenthesis around statements you want to be evaluated first, but it is possible to chain a bunch of AND/OR statements together. Just make sure you know exactly what you're doing (what order things will be evaluated in). You might be better off with nested if statements or maybe a switch statement.

Studying for SCJP 6
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Find an operator precedence chart for Java and it will tell you all that you need to know.

[Asking smart questions] [About Bear] [Books by Bear]
Derek Szpik

Joined: Feb 20, 2009
Posts: 20
most often, you can just do if(i<=MAX_SIZE&&i>=MIN_SIZE){...}, but as mentioned it does depend on what you're doing. Try posting a code segment, or finding the chart that Bear mentioned, and your problems should be solved quickly.
Juan Sifuentes

Joined: Sep 02, 2008
Posts: 6
Use parenthesis to "group" things together so they wont mix variables with both AND/OR

if ( (1 and 2) or ( 3 and 4 ) ) vs. if ( 1 and 2 or 3 and 4 )
if ( (1 or 2) and ( 3 or 4 ) ) vs. if ( 1 or 2 and 3 or 4 )

if you don't use too many parenthesis, it will make your statement easier to read and also make it a more secure from running the statement wrong.
I agree. Here's the link:
subject: AND / OR Statements
It's not a secret anymore!