🎯 Learning Objectives
- Understand DeMorgan’s Laws mathematically and logically
- Apply DeMorgan’s Laws to simplify complex Boolean expressions
- Convert negated compound conditions into equivalent forms
- Use DeMorgan’s Laws in real Java programs (
ifstatements, loops, validation) - Recognize common AP CSA test scenarios involving logical negation
🧠 Review: Boolean Logic Basics
In Java:
| Operator | Meaning |
|---|---|
&& |
AND |
|| |
OR |
! |
NOT (negation) |
Boolean expressions evaluate to:
true
false
⚡ What are DeMorgan’s Laws?
DeMorgan’s Laws allow us to move a NOT operator across logical expressions.
- NOT (AND) becomes OR
- NOT (OR) becomes AND
Key idea: When a NOT goes inside parentheses, it flips
&& ↔ || and negates each piece.
⭐ DeMorgan Law #1
!(A && B) == (!A || !B)
Meaning: NOT (A AND B) equals (NOT A OR NOT B)
Java Example
boolean result = !(x > 10 && y < 5);
// Equivalent:
boolean result2 = (x <= 10 || y >= 5);
⭐ DeMorgan Law #2
!(A || B) == (!A && !B)
Meaning: NOT (A OR B) equals (NOT A AND NOT B)
Java Example
boolean result = !(age < 18 || score < 70);
// Equivalent:
boolean result2 = (age >= 18 && score >= 70);
📊 Truth Table Verification
Example: !(A && B)
| A | B | A && B | !(A && B) | !A || !B |
|---|---|---|---|---|
| T | T | T | F | F |
| T | F | F | T | T |
| F | T | F | T | T |
| F | F | F | T | T |
Notice: !(A && B) and !A || !B always match.
🚀 Advanced Usage in Java Programming
1️⃣ Simplifying Complex Conditions
Students often write:
if(!(score >= 70 && attendance >= 90))
{
System.out.println("Not eligible");
}
Rewrite using DeMorgan:
if(score < 70 || attendance < 90)
{
System.out.println("Not eligible");
}
✅ Easier to read ✅ Easier to debug ✅ Less nesting
2️⃣ Validation Logic (Common AP CSA Pattern)
Original:
if(!(username.equals("admin") || password.equals("1234")))
{
// deny access
}
DeMorgan form:
if(!username.equals("admin") && !password.equals("1234"))
{
// deny access
}
3️⃣ Loop Control Conditions
Original:
while(!(input.equals("quit") || attempts > 3))
{
// keep looping
}
Rewrite:
while(!input.equals("quit") && attempts <= 3)
{
// keep looping
}
4️⃣ Nested Logic Simplification
Original:
if(!(x > 0 && (y > 10 || z == 5)))
{
// ...
}
Step-by-step:
// Step 1: !(A && B) -> !A || !B
// (x <= 0) || !(y > 10 || z == 5)
// Step 2: !(C || D) -> !C && !D
// (x <= 0) || (y <= 10 && z != 5)
🔥 Advanced Examples (Teacher-Level)
Example 1
// Original:
!(a == b && c != d)
// Result:
(a != b || c == d)
Example 2
// Original:
!(temperature > 90 || humidity > 80)
// Result:
(temperature <= 90 && humidity <= 80)
Example 3
// Original:
!(isLoggedIn && hasPermission)
// Result:
(!isLoggedIn || !hasPermission)
Example 4 (AP CSA Trap Question)
// Original:
if(!(num % 2 == 0 || num > 100))
// Rewrite:
if(num % 2 != 0 && num <= 100)
🎓 Why DeMorgan’s Laws Matter (Real Programming)
- Simplify complex Boolean expressions
- Reduce logical errors
- Improve readability
- Convert negative logic into positive logic
- Answer AP CSA logical reasoning questions
🧪 Common Student Mistakes
❌ Wrong:
!(A && B) == !A && !B
✅ Correct:
!(A && B) == !A || !B
❌ Forgetting to reverse comparison operators
| Original | Negated |
|---|---|
> | <= |
< | >= |
== | != |
💥 Advanced Practice (Teacher-Level)
Rewrite using DeMorgan:
-
!(x < 5 && y > 10) -
!(grade >= 90 || extraCredit == true) -
!(loggedIn || isGuest)
🧱 Real AP CSA Strategy
When you see:
!( ... && ... )
- Change AND → OR
- Negate each part
When you see:
!( ... || ... )
- Change OR → AND
- Negate each part
🔥 Teacher Tip: NOT distributes across parentheses and flips AND/OR.