TJF
TJF

Reputation: 45

Understanding if conditions - Java

What would the if condition be using this example.

A - if true, B or C must also be true; Pass
    if false, B and C do not matter; Fail
B - if true, A must also be true and C can be false or true
    if false, A must be true and C must be true; Pass, else Fail
C - if true, A must also be true and B can be false or true
    if false, A must be true and B must be true; Pass, else Fail

I am not sure how to set this up. Here is what I think the if may look like:

//Not sure if the "or" needs to be double or single bar.
if(A && B | C){
   //pass    
}else{//fail}

The broken apart code for this logic would be this:

if(A){
   if(B|C){
      //PASS
   }else{//fail}
}else{//fail}

Upvotes: 0

Views: 79

Answers (2)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

In Java, the difference btw single and double operators is that double operators (&& and ||) are short-circuit (i.e. if the result of the expression is pre-determined, the right side won't be executed).

e.g. in these cases, foo() would never be called:

false && foo(); // evaluates to false
true || foo(); // evaluates to true

but in these cases it would:

false & foo(); // still evaluates to false
true | foo(); // still evaluates to true

Upvotes: 2

Ousmane Traore
Ousmane Traore

Reputation: 663

Depending on the language you're working with it's mostly || double bars.

I think this will work:

if(A && (B || C)) {
     pass;
} else {
     fail;
}

This means if A is true and B or C is true pass, otherwise fail.

Upvotes: 3

Related Questions