• 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

condition statements

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can there be two conditions that can be right or must the first one be false in order for another statement to work?
Basically can i use 2 "if " conditions?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ana,

To elaborate a bit on Joanne's answer, Java allows you to use multiple Logical Operators within a single if statement in conjunction with Conditional Operators, such as:

if (condition1 && condition2) ← requires both condition1 and condition2 to be true
if (condition1 || condition2) ← requires either condition1 or condition2 to be true

Of course, you are not limited to just two Logical Operators. You can have a bunch (and combinations) using inner parenthesis:
if ((condition1 || condition2) && (condition3 || condition4)) ← requires either condition1 or condition2 to be true and either condition3 or condition4 to be true

In addiiton, nested if statements are valid along with the else if and else keywords in combination. For more information, I encourage you to review the Java Tutorials on Flow Control.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Reyes wrote:Can there be two conditions that can be right or must the first one be false in order for another statement to work?
Basically can i use 2 "if " conditions?


As the others have said: yes; however, there is another point to consider: Readability.

Two is normally just fine, but if you find yourself needing to check lots of conditions at once; especially of the
((condition1 || condition2) && (condition3 || condition4))
variety, you might want to consider "consolidating" them before you write your "if" statement. Eg:Hopefully, it also illustrates how giving things good names can really help make stuff clearer.

HIH

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic