# Conditionals with more than one condition.

• July 7th, 2013, 09:17 AM
mstabosz
Conditionals with more than one condition.
Okay this is kind of an elementary question that I feel like I should know the answer to. I'm trying to debug a database program and I think there's something I don't understand about if statements with multiple conditions separated by OR, and that's screwing up my code. The OR has screwed me up many times (usually because I use it in situations where AND is needed).

Not gonna go into detail with the code, so I'm just going to present some generic pseudo-code.

In the statement:

Code :

```if( (condition1) || (condition2) ) --statements executed under true conditions else --statements executed under false conditions.```

How is that if statement evaluated? Suppose condition1 is true and condition2 is false. Does it say "Okay, condition1 is true, let's check condition2. Okay, now I see condition2 is false. Let's jump to the body of the else statement."? Or does it automatically say "Okay, condition1 is true, let's jump to the body of the if statement."

Let's reverse the situation. Suppose condition1 is false and condition2 is true. Does it say "Okay, condition1 is false. Let's check condition2. Okay condition2 is true, so let's jump to the body of the if statement."? Or does it automatically jump straight to that else after it sees that condition1 is false?

Bottom line, is there a universe in which that overall statement resolves as true if just one of the conditions is true but any others are false? Is there an order of operations at play here?
• July 7th, 2013, 12:31 PM
GregBrannon
Re: Conditionals with more than one condition.
In order for the body of the if statement to execute, the two conditions OR'd together must be true. For OR, either statement being true will result in the result true:

OR truth table:
true || true = true
true || false = true
false || true = true
false || false = false

Let's consider first the AND combination in which both expressions have to be true for the combination to be true. Java continues to evaluate the entire conditional expression only while the outcome is uncertain. In a ( false && true ) situation, Java will not check the 'true' expression once the 'false' is determined.

Similarly for the ( true || don't-care ) combination, once the true is realized, the evaluation will stop.

Yes, I believe the universe you describe exists in a combination of OR'd statements. You can learn more about operator precedence here.
• July 7th, 2013, 12:44 PM
derekxec
Re: Conditionals with more than one condition.
when you are using && both statements have to be true...when you are using || only 1 of the statements has to be true which ever comes first

for || it only reads the conditions till it receives a true condition so if your first one is true it wont read the other conditions
• July 7th, 2013, 06:29 PM
jps
Re: Conditionals with more than one condition.
Quote:

Originally Posted by mstabosz
Bottom line, is there a universe in which that overall statement resolves as true if just one of the conditions is true but any others are false? Is there an order of operations at play here?

Bottom line, Yes, all of them.
Read this || as "either or" to make the sentence clearer in your mind. "if either c1 OR c2 is true, the whole statement is true"
Read this && as "both and" to make the sentence clearer in your mind. "if both c1 AND c2 are true, the whole statement is true"
• July 12th, 2013, 06:11 AM
mstabosz
Re: Conditionals with more than one condition.
Thanks all. It is as I suspected. And actually right after I made this thread I wrote up this short little test program which fleshed it all out. It prints out 1--TRUE 2--TRUE. I think that my problem with the original program isn't that it isn't going down the right road, but where it goes at the end of that road. There's a lot of nested if-else blocks and I think I may have left out a bracket somewhere so it's executing extra statements that I don't want it to. Having a devil of a time tracing it and think I might just have to print out a hard copy of the code just to double check all the placements of ifs, elses, and brackets.

Code :

```  public class scratchpad { public static void main(String[] args) { if((true) || (false)) System.out.println("1--TRUE"); else System.out.println("1--FALSE");   if((false) || (true)) System.out.println("2--TRUE"); else System.out.println("2--FALSE"); }   }```