🔥 Advanced Class Notes: DeMorgan’s Laws (Java Boolean Logic)

High school / AP CSA-ready notes with clear rules, truth tables, and Java examples.

🎯 Learning Objectives

🧠 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.

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
TTTFF
TFFTT
FTFTT
FFFTT

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)

🧪 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:

  1. !(x < 5 && y > 10)
  2. !(grade >= 90 || extraCredit == true)
  3. !(loggedIn || isGuest)

🧱 Real AP CSA Strategy

When you see:

!( ... && ... )

When you see:

!( ... || ... )
🔥 Teacher Tip: NOT distributes across parentheses and flips AND/OR.