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.
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)
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With