• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AND / OR Statements

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Find an operator precedence chart for Java and it will tell you all that you need to know.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic