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
With boolean a = true and boolean b = false, what is a && b?Show answerHide answer
It is
false, because&&needs both operands to betrue.For an int x, which comparison is true exactly when x > 5 is false?Show answerHide answer
x <= 5. Everyintis either greater than 5 or at most 5, never both.Is !(a && b) the same as !a && !b?Show answerHide answer
No. With
atrue andbfalse,!(a && b)istruebut!a && !bisfalse.
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
trueor alwaysfalse.
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
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.
a | b | a && b | !(a && b) | !a || !b |
|---|---|---|---|---|
true | true | true | false | false |
true | false | false | true | true |
false | true | false | true | true |
false | false | false | true | true |
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
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:
| Expression | Equivalent 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
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 || !ais alwaystrue, anda && !ais alwaysfalse.x > 5 || x <= 5is alwaystrue, because everyintlands on one side.x > 5 && x < 3is alwaysfalse, 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
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.
Step 1: Build the table.
ab!(a || b)!a || !bSame? truetruefalsefalseyes truefalsefalsetrueno falsetruefalsetrueno falsefalsetruetrueyes WhyAn equivalence is about every input, so no row can be skipped.
Step 2: Count the rows that differ.
The two middle rows differ.
WhyEach 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.
Step 1: Apply De Morgan's law.
!(age >= 13) || !(age <= 19).WhyThe
!moves onto each part, and&&becomes||.Step 2: Flip each comparison.
age < 13 || age > 19.WhyThe opposite of
>=is<, and the opposite of<=is>.Step 3: Trace
outsideTeens(19).19 < 13isfalseand19 > 19isfalse, so the result isfalse.Why19 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.
Step 1: Apply De Morgan's law.
!(x < 10) && !(y == 0).WhyThe
!moves onto each part, and||becomes&&.Step 2: Flip each comparison.
!(x < 10)isx >= 10, and!(y == 0)isy != 0, givingx >= 10 && y != 0.WhyThe opposite of
<is>=, so 10 itself must be included.Step 3: Name each wrong option's error.
A keeps
||. B flips<to>, which leaves out 10. D forgets to negatey == 0.WhyEach 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.
Step 1: Check the rows where
aistrue.Then
a || bistrue, soa && (a || b)istrue && true, which istrue, the same asa.Step 2: Your go: check the rows where
aisfalse, 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
- 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
pand keeps||.
- 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 > 5istrue && false, which isfalse, so its negation istrue.- The second line is the De Morgan rewrite of the first:
false || trueistrue. - The third line keeps
&&by mistake:false && trueisfalse.
- Apply
kis anint. Which expression isfalsefor every value ofk?
- 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
truefor 3, 4 and 5. - C is always
true: every number differs from 3 or from 5.
- Apply In how many rows of the truth table for
!a || bis the expressionfalse?
Show answer
Answer: 1
Marking points:
||isfalseonly when both sides arefalse:!aisfalse(soaistrue) andbisfalse.- That is one row out of four.
- Exam-style Which
ifstatement prints the same output as this one for everyintvalue ofn? Each option is followed by the sameprintlnblock.
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 istruefor everyn. - 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.
- Exam-style The method should return
truewhensis 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")givesfalse || true, which istrue, but it should befalse.- "Neither yes nor no" is
!(yes || no), which De Morgan's law turns into!yes && !no.
- 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 || !band!(a || b)≡!a && !b.- Negating a comparison:
>and<=are opposites, and so are<and>=, and==and!=. - A condition that is
trueorfalsefor every input makes anifalways or never run. - "Neither x nor y" is written with
&&, not||.