De Morgan's Laws & Negating Conditions

Part of: Boolean Expressions & if Statements

Negating Compound Conditions Sometimes you need the opposite of a compound boolean. De Morgan's Laws tell you exactly how to distribute a ! across && and . They are two algebraic identities: - !(a && b) is equivalent to !a !b - !(a b) is equivalent to !a && !b The pattern: negate each operand and flip the operator (&& becomes , and becomes &&). Why This Matters Consider a rule: "reject if NOT (age = 18 AND citizen)". Applying De Morgan's Law: Notice !(age = 18) becomes age < 18 because the negation of = is <. Negating relational operators is part of the skill: - !(a < b) is a = b - !(a b) is a <= b - !(a == b) is a != b - !(a != b) is a == b A Full Example You could rewrite !open using De Morgan's Laws into a positive form, but the law guarantees the two versions behave identically for every input. Verifying Equivalence The safest way to confirm two boolean expressions are equivalent is a truth table : list every combination of the variables and check both expressions yield the same result. De Morgan's Laws are proven this way and never fail. They are a core AP CSA topic for simplifying and rewriting conditions cleanly.

Challenge: Outside the Range