• Post Reply Bookmark Topic Watch Topic
  • New Topic

condition statements  RSS feed

 
Ana Reyes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!