Reputation: 100010
I have this:
if(!A or (A and B)) //pseudocode
I want to negate that if-statement:
This should work:
if(!(!A or (A and B))) //pseudocode
but I believe there is a way to simplify it and it's escaping me at the moment.
Upvotes: 4
Views: 132
Reputation: 36703
If you break it down to a truth table...
A B !A (A and B) A! or (A and B)
0 0 1 0 1
0 1 1 0 1
1 0 0 0 0
1 1 0 1 1
You can see the result is true in all cases except A and !B
without having to know/remember any Boolean algebra rules. "Except" is just "not" so... !(A and !B)
...
However if writing !A or (A and B)
aligns better to the real-world problem you're trying to code leave it like that, unless it's insanely performance critical...
Upvotes: 4
Reputation: 31878
Welcome to the world of de-Morgan for boolean algebra followed by simple distribution :
if(!(!A or (A and B))
=> if(!(!A) and !(A and B))
=> if(A and (!A or !B))
=> if((A and !A) or (A and !B))
=> if(A and !B)
Upvotes: 6