AP® Computer Science A · Unit 2 · Topic 2.6
De Morgan's Laws and Negating a Condition
De Morgan's Laws say that negating an AND flips it into an OR of negated parts, and negating an OR flips it into an AND of negated parts. Each comparison inside also flips direction.
Negating a condition means writing the boolean expression that is true in exactly the opposite cases. The trap is that most people negate each piece but forget to flip the connector too. De Morgan's Laws fix that: not(a && b) equals (not a) || (not b), and not(a || b) equals (not a) && (not b). The AND and OR swap places whenever a not pushes through them.
The same push-through has to happen inside each comparison. Negating x > 5 does not give x < 5, because x < 5 skips the value 5 itself. The real opposite of greater than is less than or equal to, so not(x > 5) is x ≤ 5. Each comparison operator has its true opposite: > flips to ≤, < flips to ≥, == flips to ≠. Skipping this step is the single most common bug when students rewrite conditions by hand.
This topic sits right after compound boolean expressions with && and ||, and right before loop conditions, because while loops and do-while loops often get rewritten using a negated exit condition. Short-circuit evaluation also depends on getting these conditions right: if the negated condition is wrong, the branch taken or skipped in a trace table will be wrong too. Getting De Morgan's Laws solid here means fewer silent logic bugs later, especially in loop guards and validation checks where an off-by-one on the boundary value causes real failures.
Key terms
- De Morgan's Laws
- Rules stating that not(a && b) equals (not a) || (not b), and not(a || b) equals (not a) && (not b).
- Negation
- A boolean expression that is true in exactly the cases where the original expression is false.
- Short-circuit evaluation
- Java's practice of skipping the second operand of && or || when the result is already determined by the first.
- Comparison operator
- A symbol like >, <, ≥, ≤, ==, or ≠ that compares two values and produces a boolean result.
Watch it explained
- How it worksHow does NOT distribute across an AND and turn it into an OR?
- Worked exampleFind the negation of x > 5 && y < 3 and check it against every possible case.
- Worked exampleFind a simpler condition equivalent to !(score < 60 || score > 100).
- Common mistakeWhy isn't the opposite of x > 5 simply x < 5?
Check yourself
End of topic.