AP · Computer Science A · Boolean Expressions and if Statements

Equivalent Expressions and De Morgan's Laws

12 min readUpdated December 1, 2026FreeAP Computer Science A, Selection and Iteration

Two Boolean expressions are equivalent when they give the same value for every possible input. De Morgan's laws let you move a ! inside parentheses: !(a && b) is equivalent to !a || !b, and !(a || b) is equivalent to !a && !b. You prove an equivalence with a truth table or by simplifying step by step, and you disprove one with a single input where the two expressions differ.

Quick check

  1. With boolean a = true and boolean b = false, what is a && b?Show answerHide answer

    It is false, because && needs both operands to be true.

  2. For an int x, which comparison is true exactly when x > 5 is false?Show answerHide answer

    x <= 5. Every int is either greater than 5 or at most 5, never both.

  3. Is !(a && b) the same as !a && !b?Show answerHide answer

    No. With a true and b false, !(a && b) is true but !a && !b is false.

Know these already? Jump to the practice questions

What you'll be able to do

  • State De Morgan's laws for && and ||.
  • Rewrite a negated condition without ! by flipping each comparison.
  • Use a truth table to show that two expressions are equivalent, or find an input where they differ.
  • Identify an expression that is always true or always false.

Key terms

Equivalent expressions

Two Boolean expressions that have the same value for every combination of input values.

In plain English
Different wording, same answer every time.
Don't write
"They are equivalent because they agree for one input."

De Morgan's laws

The rules !(a && b) ≡ !a || !b and !(a || b) ≡ !a && !b, where ≡ means "is equivalent to".

In plain English
Push the ! onto each part and swap && with ||.
Don't write
"!(a && b) is the same as !a && !b."

Counterexample

One set of input values for which two expressions give different results, which proves they are not equivalent.

In plain English
A single case that breaks the claim.
Don't write
"One matching row proves the expressions are equivalent."

What makes two expressions equivalent?

Two expressions are equivalent only if they agree in every row of their truth table. With two boolean inputs that means all 4 rows; with three, all 8.

So there are two different jobs:

  • To prove an equivalence, check every row, or turn one expression into the other with rules that are already known to be safe.
  • To disprove it, one row is enough. That row is a counterexample.

For example, !(a && b) and !a && !b look alike. Try a true and b false: a && b is false, so !(a && b) is true; !a && !b is false && true, which is false. One row disagrees, so they are not equivalent.

In code, equivalent conditions are interchangeable: an if behaves the same with either one, so you can pick the clearer form.

Check yourself

Two expressions agree in 3 of the 4 rows of their truth table. Are they equivalent?
Show answerHide answer

No. Equivalent means they agree in all 4 rows, and the disagreeing row is a counterexample.

What do De Morgan's laws say?

De Morgan's laws say how to remove ! from in front of parentheses. Negate each part, and swap the operator:

  • !(a && b) is equivalent to !a || !b.
  • !(a || b) is equivalent to !a && !b.

The truth table proves the first law: the last two columns agree in every row.

aba && b!(a && b)!a || !b
truetruetruefalsefalse
truefalsefalsetruetrue
falsetruefalsetruetrue
falsefalsefalsetruetrue

The idea in words: "not both" means "at least one is missing", and "not either" means "neither". These laws are not on the Java Quick Reference: Learn this.

Key idea

When ! moves inside the parentheses, every part is negated and && and || swap.

Check yourself

Rewrite !(done || late) with the ! moved inside.
Show answerHide answer

!done && !late. Each part is negated and || becomes &&.

How do you negate a comparison?

To remove ! from a single comparison of int values, swap in the opposite operator:

ExpressionEquivalent without !
!(x > 5)x <= 5
!(x >= 5)x < 5
!(x < 5)x >= 5
!(x <= 5)x > 5
!(x == 5)x != 5

The opposite of > is <=, not <, because the value 5 itself must land on one side or the other.

Combine this with De Morgan's laws to rewrite a whole condition. !(x > 0 && x < 10) becomes !(x > 0) || !(x < 10), which becomes x <= 0 || x >= 10: "outside the range 1 to 9".

Check yourself

Rewrite !(score >= 50) without the !.
Show answerHide answer

score < 50.

How do you spot an always-true or always-false condition?

Some conditions have the same value for every input, so an if that uses one either always runs or never runs, which is usually a bug.

  • a || !a is always true, and a && !a is always false.
  • x > 5 || x <= 5 is always true, because every int lands on one side.
  • x > 5 && x < 3 is always false, because no number is both above 5 and below 3.

The common bug is n != 1 || n != 2, written to mean "n is neither 1 nor 2". Every int differs from at least one of 1 and 2, so it is always true. The intended condition is !(n == 1 || n == 2), which De Morgan's laws turn into n != 1 && n != 2.

To test a suspicious condition, try a value that should make it false. If none does, it is always true.

Check yourself

Is x < 0 && x > 0 always true, always false, or neither?
Show answerHide answer

Always false: no number is both below 0 and above 0.

Worked example 1: Disprove an equivalence with a truth table

Question: A student claims that !(a || b) is equivalent to !a || !b. In how many of the four rows of the truth table do the two expressions differ?

What tells you the method

A claim about equivalence with two boolean inputs, so fill in all four rows and compare.

  1. Step 1: Build the table.

    ab!(a || b)!a || !bSame?
    truetruefalsefalseyes
    truefalsefalsetrueno
    falsetruefalsetrueno
    falsefalsetruetrueyes
    Why

    An equivalence is about every input, so no row can be skipped.

  2. Step 2: Count the rows that differ.

    The two middle rows differ.

    Why

    Each differing row is a counterexample, so the claim is false.

Check it

De Morgan's law gives !(a || b) ≡ !a && !b; the student kept ||, which is why the rows with exactly one true input fail.

Answer

2

Worked example 2: Rewrite a condition without !

Question: A method returns !(age >= 13 && age <= 19). Rewrite this without !, then state what System.out.println(outsideTeens(19)); prints.

public static boolean outsideTeens(int age)
{
    return !(age >= 13 && age <= 19);
}

What tells you the method

A ! in front of a bracket joined by &&, so apply De Morgan's law, then flip each comparison.

  1. Step 1: Apply De Morgan's law.

    !(age >= 13) || !(age <= 19).

    Why

    The ! moves onto each part, and && becomes ||.

  2. Step 2: Flip each comparison.

    age < 13 || age > 19.

    Why

    The opposite of >= is <, and the opposite of <= is >.

  3. Step 3: Trace outsideTeens(19).

    19 < 13 is false and 19 > 19 is false, so the result is false.

    Why

    19 is inside the teen range, so "outside the teens" must be false.

Check it

The original gives !(true && true), which is false, the same result. At 20, both forms give true.

Answer

false

Worked example 3: Choose the equivalent expression

Question: x and y are int variables. Which expression is equivalent to !(x < 10 || y == 0)?

  • A) x >= 10 || y != 0
  • B) x > 10 && y != 0
  • C) x >= 10 && y != 0
  • D) x >= 10 && y == 0

What tells you the method

A negated bracket and four close rewrites, so do the rewrite yourself, then match it.

  1. Step 1: Apply De Morgan's law.

    !(x < 10) && !(y == 0).

    Why

    The ! moves onto each part, and || becomes &&.

  2. Step 2: Flip each comparison.

    !(x < 10) is x >= 10, and !(y == 0) is y != 0, giving x >= 10 && y != 0.

    Why

    The opposite of < is >=, so 10 itself must be included.

  3. Step 3: Name each wrong option's error.

    A keeps ||. B flips < to >, which leaves out 10. D forgets to negate y == 0.

    Why

    Each distractor makes exactly one rewriting mistake.

Check it

Try x = 10, y = 3. The original is !(false || false), which is true; C gives true && true, also true, but B gives false.

Answer

C

Your turn

Question: Is a && (a || b) equivalent to a? Answer yes or no.

  1. Step 1: Check the rows where a is true.

    Then a || b is true, so a && (a || b) is true && true, which is true, the same as a.

  2. Step 2: Your go: check the rows where a is false, then decide.

Show the full solutionHide the solution

Step 2. When a is false, a && (a || b) is false whatever b is, because && with a false left operand is false. That matches a in both rows, so all four rows agree.

Answer: Yes

Traps

Trap

!(a && b) can be rewritten as !a && !b.

It seems right because the ! looks as if it multiplies out like a minus sign. But De Morgan's laws also swap the operator: !(a && b) is equivalent to !a || !b.

With a = true and b = false, what are !(a && b) and !a && !b?

!(a && b) is true and !a && !b is false, so the two are not equivalent.

Trap

The opposite of x > 5 is x < 5.

It seems right because < looks like the reverse of >. But when x is 5, both x > 5 and x < 5 are false. The negation must cover 5, so it is x <= 5.

What is the negation of count < 3 without using !?

count >= 3.

Trap

n != 1 || n != 2 is true only when n is neither 1 nor 2.

It seems right because it reads like "n is not 1 or 2". But every int differs from at least one of the two, so the condition is always true. Neither 1 nor 2 is n != 1 && n != 2.

With n = 1, what is n != 1 || n != 2?

true: n != 1 is false, but n != 2 is true.

What's examined

On the AP® Computer Science A exam (Unit 2: Selection and Iteration), multiple-choice questions show a condition and ask which other expression or code segment is equivalent, or ask for the input that makes two versions behave differently. In free response, the same rewriting lets you state a loop's or an if's condition directly instead of negating it, which keeps your code easier to check.

Practice questions

  1. Recall Which expression is equivalent to !(p || q)?
  • A) !p || !q
  • B) !p && !q
  • C) p && q
  • D) !p || q
Show answer

Answer: B. By De Morgan's law, the ! moves onto both parts and || becomes &&.

Why the others are wrong:

  • A negates both parts but keeps ||.
  • C swaps the operator but drops the negation.
  • D negates only p and keeps ||.
  1. Apply What is printed when this code segment runs?
int x = 7;
int y = 4;
System.out.println(!(x > 5 && y > 5));
System.out.println(x <= 5 || y <= 5);
System.out.println(!(x > 5) && !(y > 5));
Show answer

Answer: true then true then false, one per line.

Marking points:

  • x > 5 && y > 5 is true && false, which is false, so its negation is true.
  • The second line is the De Morgan rewrite of the first: false || true is true.
  • The third line keeps && by mistake: false && true is false.
  1. Apply k is an int. Which expression is false for every value of k?
  • A) k > 3 || k < 5
  • B) k >= 3 && k <= 5
  • C) k != 3 || k != 5
  • D) k < 3 && k > 5
Show answer

Answer: D. No number is both below 3 and above 5.

Why the others are wrong:

  • A is always true: every number is above 3 or below 5.
  • B is true for 3, 4 and 5.
  • C is always true: every number differs from 3 or from 5.
  1. Apply In how many rows of the truth table for !a || b is the expression false?
Show answer

Answer: 1

Marking points:

  • || is false only when both sides are false: !a is false (so a is true) and b is false.
  • That is one row out of four.
  1. Exam-style Which if statement prints the same output as this one for every int value of n? Each option is followed by the same println block.
if (!(n < 0 || n > 9))
{
    System.out.println("digit");
}
  • A) if (n >= 0 && n <= 9)
  • B) if (n >= 0 || n <= 9)
  • C) if (n > 0 && n < 9)
  • D) if (n < 0 && n > 9)
Show answer

Answer: A. De Morgan's law gives !(n < 0) && !(n > 9), which is n >= 0 && n <= 9.

Why the others are wrong:

  • B keeps ||, so it is true for every n.
  • C flips < to > and > to <, which leaves out 0 and 9.
  • D drops the ! but swaps the operator, and no number is both below 0 and above 9.
  1. Exam-style The method should return true when s is neither "yes" nor "no". Give the number of the line that stops it working.
1: public static boolean isOther(String s)
2: {
3:     boolean yes = s.equals("yes");
4:     boolean no = s.equals("no");
5:     return !yes || !no;
6: }
Show answer

Answer: Line 5.

Marking points:

  • isOther("yes") gives false || true, which is true, but it should be false.
  • "Neither yes nor no" is !(yes || no), which De Morgan's law turns into !yes && !no.
  1. Recall Name the pair of rules that turns !(a && b) into !a || !b.
Show answer

Answer: De Morgan's laws

Marking points:

  • They also turn !(a || b) into !a && !b.

Key points

Recall, then reveal

Say the main points from memory first, then check them.

Show key pointsHide key points
  • Equivalent means equal in every row; one disagreeing row disproves it.
  • !(a && b) ≡ !a || !b and !(a || b) ≡ !a && !b.
  • Negating a comparison: > and <= are opposites, and so are < and >=, and == and !=.
  • A condition that is true or false for every input makes an if always or never run.
  • "Neither x nor y" is written with &&, not ||.

Keep learning

Explore AP Computer Science A tutoring →

View the full Computer Science A syllabus →

More in this unit

Selection and Iteration

All Boolean Expressions and if Statements notes

Part of Novark's AP Computer Science A notes