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 AND / OR Statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "AND / OR Statements" Watch "AND / OR Statements" New topic
Author

AND / OR Statements

LaTeef Lusk
Greenhorn

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
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Derek Szpik
Greenhorn

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
Greenhorn

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

example:
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: http://aspose.com/file-tools
 
subject: AND / OR Statements
 
Similar Threads
problem in jsp page
System.in.read()
regex code to recognize fully qualified Java class names in strings?
boolean x = (a = true) || (b = true) && (c = true);
I am getting ClassCastException