Equivalent Boolean Expressions
When two different-looking conditions always give the same answer: proving equivalence with truth tables, rewriting negated conditions with De Morgan's law, and negating comparisons correctly.
Two Boolean expressions are equivalent if they evaluate to the same value in all cases. Truth tables can be used to prove Boolean expressions are equivalent by checking every combination of inputs. De Morgan's law can be applied to Boolean expressions to create equivalent expressions: !(a && b) is equivalent to !a || !b, and !(a || b) is equivalent to !a && !b. These tools let you rewrite conditions into clearer forms with confidence that their meaning has not changed.
1What "Equivalent" Means
The same condition can often be written in several ways. "Not a teenager" might be written !(age >= 13 && age <= 19) or age < 13 || age > 19. Two Boolean expressions are equivalent if they evaluate to the same value in all cases: for every possible combination of values of their variables, they give the same true or false.
"In all cases" is the important part. Two expressions that agree for a few test values might still disagree for others, so a handful of examples cannot prove equivalence. A single case where they differ, however, is enough to prove they are not equivalent. Such a case is called a counterexample.
Recognizing equivalent expressions matters in practice. Programmers rewrite conditions to make them easier to read, to remove confusing negations, or to match the way a rule was stated, and they need to be sure the rewritten version behaves identically.
Equivalence is about values, not appearance. x > 5 and 5 < x look different but always agree; x > 5 and x >= 6 also agree when x is an int, though not for a double such as 5.5. When judging equivalence, keep the types and the full range of possible values in mind.
| Expression | Equivalent form | Rule |
|---|---|---|
!(a && b) | !a || !b | De Morgan's law |
!(a || b) | !a && !b | De Morgan's law |
!(!a) | a | Double negation |
a || (a && b) | a | Proved by truth table |
2Proving Equivalence With Truth Tables
A truth table lists every possible combination of values for the Boolean variables involved and evaluates each expression for every combination. With two variables there are four rows; with three variables, eight. If the columns for the two expressions match in every row, the expressions are equivalent; if any row differs, they are not.
Building a truth table is mechanical, which is its strength. Add intermediate columns for parts of each expression, such as a && b before !(a && b), so that each step is a single, simple operation. The same check can be run in code by evaluating both expressions for every combination and comparing the results.
When an expression contains comparisons rather than plain boolean variables, treat each comparison as a single true-or-false input. In !(age >= 13 && age <= 19), let a stand for age >= 13 and b for age <= 19. The expression becomes !(a && b), and the truth table for that shape applies directly.
Testing an Equivalence in Code
The method prints both sides of De Morgan's first law for one pair of values. Call it for all four combinations and compare.
public static void compare(boolean a, boolean b)
{
boolean left = !(a && b);
boolean right = !a || !b;
System.out.println(a + " " + b + ": " + left + " " + right);
}
true true: false false
true false: true true
false true: true true
false false: true true
- 1Every combination. Two Boolean variables have exactly four combinations, and each is tested.
- 2Compare the last two values on each line. They agree in all four rows:
false false, thentrue truethree times. - 3Conclusion. Agreement in every case is exactly what "equivalent" means.
!(a && b) and !a || !b are equivalent. Testing only some combinations would not prove it.| a | b | !(a || b) | !a && !b |
|---|---|---|---|
| true | true | false | false |
| true | false | false | false |
| false | true | false | false |
| false | false | true | true |
Proving a Simplification
Show with a truth table that a || (a && b) is equivalent to just a.
- 1Row a = T, b = T.
a && bis T;a || Tis T. Matchesa(T). - 2Row a = T, b = F.
a && bis F;a || Fis T. Matchesa(T). - 3Row a = F, b = T.
a && bis F;F || Fis F. Matchesa(F). - 4Row a = F, b = F.
a && bis F;F || Fis F. Matchesa(F).
a in all four rows, so a || (a && b) is equivalent to a. Whenever a is true the whole expression is true, and whenever it is false the && part is false too.Agreement on two or three combinations does not prove equivalence. A truth table must include every combination of the variables, four rows for two variables and eight for three. Equivalence is a claim about all cases.
3De Morgan's Law
De Morgan's law gives two equivalences that rewrite a negated compound expression. Under De Morgan's law, the Boolean expression !(a && b) is equivalent to !a || !b, and the Boolean expression !(a || b) is equivalent to !a && !b. Both follow one pattern: move the ! inside the parentheses, negate each part, and swap && with ||.
The laws make sense in everyday terms. "It is not true that it is both sunny and warm" means "it is not sunny, or it is not warm." "It is not true that the store sells bread or milk" means "it sells no bread and no milk." The swap from "and" to "or," and from "or" to "and," is the step people most often forget.
The laws also work in reverse. An expression such as !a && !b can be rewritten as !(a || b), which is sometimes the clearer way to state a rule: "neither raining nor snowing" reads naturally as "not (raining or snowing)." Choose whichever form matches the way the rule is described, since both behave identically.
!(a && b) is equivalent to !a || !b, and !(a || b) is equivalent to !a && !b.Negating an or
A loop should keep running while it is not the case that the player is out of lives or out of time. Rewrite !(lives == 0 || seconds == 0) and test it.
int lives = 2;
int seconds = 0;
System.out.println(!(lives == 0 || seconds == 0));
System.out.println(lives != 0 && seconds != 0);
seconds = 45;
System.out.println(!(lives == 0 || seconds == 0));
System.out.println(lives != 0 && seconds != 0);
false
false
true
true
- 1De Morgan's law.
!(P || Q)becomes!P && !Q. - 2Negate each part.
!(lives == 0)islives != 0;!(seconds == 0)isseconds != 0. - 3Result.
lives != 0 && seconds != 0: keep going only if there are lives and time left. - 4Check. With 0 seconds both forms give
false; with 45 seconds both givetrue.
!(lives == 0 || seconds == 0) is equivalent to lives != 0 && seconds != 0, and the second form reads more naturally.!(a && b) is not equivalent to !a && !b. With a true and b false, the first is true and the second is false. De Morgan's law always swaps && and ||.
4Negating Comparisons
De Morgan's law produces negated parts such as !(age >= 13). Each negated comparison can be rewritten with the opposite relational operator: !(x < y) is x >= y, !(x > y) is x <= y, and !(x == y) is x != y, along with the reverse of each.
The opposite of < is >=, not >. Every value must satisfy exactly one of a comparison and its negation, so the boundary value has to switch sides. If x < 10 excludes 10, then its negation must include 10, which is x >= 10.
! by using the opposite operator.| Comparison | Its negation | Example with x = 7 |
|---|---|---|
x < y | x >= y | !(x < 7) ≡ x >= 7: true |
x > y | x <= y | !(x > 7) ≡ x <= 7: true |
x <= y | x > y | !(x <= 6) ≡ x > 6: true |
x == y | x != y | !(x == 7) ≡ x != 7: false |
Removing Double Negatives
Each pair of lines prints the same value. Explain why.
boolean done = false;
int x = 0;
System.out.println(!(!done));
System.out.println(done);
System.out.println(!(x != 0));
System.out.println(x == 0);
false
false
true
true
- 1
!(!done). Negating twice returns the original value, so it is equivalent todone. - 2
!(x != 0). The negation of "not equal" is "equal," so it is equivalent tox == 0.
!(score < 60) is score >= 60. Writing score > 60 drops the value 60 from both the original and its negation, so a score of exactly 60 would be handled incorrectly.
5Rewriting Conditions
Combining De Morgan's law with comparison negation turns awkward conditions into direct ones. !(age >= 13 && age <= 19) becomes age < 13 || age > 19 in two steps: apply De Morgan's law to get !(age >= 13) || !(age <= 19), then negate each comparison. The result describes the values outside the range, which is exactly what "not inside" means.
Rewriting is especially useful for loop conditions, which are often stated in terms of when to stop. "Repeat until out of lives or out of time" means the loop continues while !(lives == 0 || seconds == 0), which De Morgan's law turns into the clearer lives != 0 && seconds != 0. Whenever a rewrite is done, test a few values on each side of every boundary to confirm it.
&& becomes ||.Rewriting "Not a Teen"
Rewrite !(age >= 13 && age <= 19) without the outer !, then check it on several ages.
public static void check(int age)
{
boolean original = !(age >= 13 && age <= 19);
boolean rewritten = age < 13 || age > 19;
System.out.println(age + ": " + original + " " + rewritten);
}
12: true true
13: false false
16: false false
19: false false
20: true true
- 1De Morgan's law.
!(P && Q)becomes!P || !Q. - 2Negate each comparison.
!(age >= 13)isage < 13;!(age <= 19)isage > 19. - 3Result.
age < 13 || age > 19. - 4Check. Both forms agree for every age tested, including the boundaries 13 and 19.
age < 13 || age > 19 matches the original for every age. Negating an "inside" test produces an "outside" test.Two Structures, One Behavior
A nested if whose outer statement has no else is equivalent to a single if joined with &&. Predict both outputs.
boolean hasKey = true;
boolean doorLocked = false;
if (hasKey)
{
if (!doorLocked)
{
System.out.println("walk in");
}
}
if (hasKey && !doorLocked)
{
System.out.println("walk in");
}
walk in
walk in
- 1Nested version. The inner condition is checked only when
hasKeyistrue; both are true here, so the message prints. - 2Combined version.
hasKey && !doorLockedistrue && true. - 3Why they match in every case. Each version prints exactly when both conditions are true. Short-circuiting even mirrors the nesting: the second condition is evaluated only when the first is true.
if without an else is equivalent to one if whose condition joins the two tests with &&.6Spotting Expressions That Are Not Equivalent
To show that two expressions are not equivalent, a single counterexample is enough: one combination of values where they evaluate differently. The fastest way to find one is to try the combinations most likely to expose a difference, such as one variable true and the other false, or values exactly at a boundary.
Common non-equivalences include negating each part without swapping the operator, flipping < into > instead of >=, and dropping parentheses so that precedence changes the grouping. When a rewrite looks suspicious, build the truth table or test boundary values in code; the answer is never a matter of opinion.
Forgetting to Swap the Operator
A student claims !(a && b) is equivalent to !a && !b. Find a case that proves the claim wrong.
boolean a = true;
boolean b = false;
System.out.println(!(a && b));
System.out.println(!a && !b);
true
false
- 1Try
atrue andbfalse.a && bisfalse, so!(a && b)istrue. - 2The claimed version.
!aisfalse, so!a && !bisfalse. - 3One disagreement is enough. Equivalent expressions must match in every case; this counterexample shows they do not.
! to each part without swapping && and || does not give an equivalent expression.!(a && b) || c is not the same as !(a && b || c). The ! applies only to what its parentheses enclose. Keep track of exactly which part is being negated before applying De Morgan's law.
Four Habits for Equivalent Expressions
1. Every Case, or None
Equivalence needs every row; one counterexample disproves it.
2. Swap && and ||
De Morgan's law: move ! inside, negate each part, swap.
3. Opposite of < Is >=
The boundary always switches sides.
4. Test the Boundaries
Check rewritten conditions at and around each boundary.
Two Boolean expressions are equivalent if they evaluate to the same value in all cases, and truth tables can be used to prove Boolean expressions are equivalent. De Morgan's law can be applied to Boolean expressions to create equivalent Boolean expressions: under De Morgan's law, the Boolean expression !(a && b) is equivalent to !a || !b, and the Boolean expression !(a || b) is equivalent to !a && !b. Negated comparisons can be rewritten with the opposite relational operator.