0
boolean a = true    
boolean b = true

<random code in here, booleans may or may not change>

if ((!a || !b) || (!a && !b)){
    doSomethingElse();
}

This code is not working for me, what is a simple solution to this problem?

To be clear: The if statement should work: if at least one of the booleans is false

3 cases:

a = False b = True
a = True b = False
a = False b = False

I could do this in one long if statement, but I was just wondering if there was a simple way to implement this.

4
  • are you setting the booleans to false at all? because it is checking if they are false and it looks like you have them set to true?
    – Savlon
    Jan 24, 2013 at 4:35
  • @JoseSwagKid : do you mean if either is false call doSomething() , else call doSomethingElse() ?????? Jan 24, 2013 at 4:35
  • Keeping it simple: if at least one of the booleans is false (so either one of them or both of them) - doSomethingElse should be called - I hope that makes it more clear. Jan 24, 2013 at 4:44
  • since all of the answers are either (!a || !b) or !(a && b), this seems like an apropos time to link to DeMorgan's Laws to explain why these two statements are equivalent Jan 24, 2013 at 4:49

7 Answers 7

4

This should work for you:

if( !a || !b ) {
  doSomething();
}
0
4
if(!(a && b)) {  
  ....Then do something 
}
3
  • 1
    This does not work. The if statement should work: if at least one of the booleans is false Jan 24, 2013 at 4:42
  • 2
    yes it does work. if one of the booleans is false, then (a && b) will be false. negating that makes it true. therefore the if statement checks if at least one of the booleans is false Jan 24, 2013 at 4:46
  • 1
    Sorry, I found another bug in my code, which was causing this code to not work. Good job! Jan 24, 2013 at 4:49
1

(Way too much time, breakdown of logic)

Start by applying De Morgan's to get the ! outside: (Although other boolean algebra could be applied at this stage to skip a few steps, I like to show this.)

That is, given:

  1. (!a || !b) (by DM) -> !(a && b) and
  2. (!a && !b) (by DM) -> !(a || b).

Then (!a || !b) || (!a && !b) (by substituion) -> !(a && b) || !(a || b).

Applying DM again:

!( (a && b) && (a || b) )

Now using "distributivity of ^ over v" (x = a && b; y = a; z = b):

!( ((a && b) && a) || ((a && b) && b) )

And by "associativity" and "communicativity":

!( (a && a && b) || (a && b && b) )

And by "idempotence":

!( (a && b) || (a && b) )
!( (a && b) )

Simplified:

!(a && b)

Back by DM:

!a || !b

Of course, a simple Truth Table may have been easier to show this ..

1

Your if statement will work just change your if Statement to this:

if (!a || !b) { } As you can see above it will work when any one of the boolean is false .

All the Best.

0

Your proposed solution can be simplified to...

if (!a || !b) { .... }
0

By checking whether one is false or both are false, you're making sure that both are true, or a && b. The opposite of a && b is !a || !b:

if (!a || !b) {
    doSomethingElse();
}
0
if (!(a && b)) {
    doSomethingElse();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.