AP® Computer Science A · Unit 2 · Topic 2.6
Truth Tables and Equivalent Conditions
A truth table lists every combination of true/false inputs and checks the output of an expression for each one. Two expressions are equivalent if their outputs match on every single row.
Boolean expressions built from the same variables can look completely different and still compute the same thing. The only way to know for sure is to check every possible input combination, because a condition that agrees with another on nine out of ten cases is not equivalent at all; it just hides its disagreement well. A truth table forces you to check all of them, systematically, so there's nowhere for a mismatch to hide.
To build one, list every variable once per column, then list every combination of true and false values as rows. With two variables you get four rows: TT, TF, FT, FF. With each variable you add, the table doubles in height, since every existing row now needs to pair with both a true and a false for the new variable. Three variables means eight rows, four variables means sixteen. For each row, plug the values into both expressions and work out the result using the usual rules: && is true only when both sides are true, || is true when at least one side is true, and ! flips a value. If the output columns for the two expressions match on every row, the expressions are equivalent, no matter how different they look written out.
This matters directly for writing conditions in code: an if statement's condition can often be rewritten in a cleaner or faster form, and a truth table is how you verify the rewrite didn't change behavior. It also connects to De Morgan's laws, which give shortcuts for rewriting negated && and || expressions without building a table from scratch every time.
Key terms
- Truth table
- A table listing every combination of true/false values for a set of variables alongside the resulting output of an expression.
- Equivalent expressions
- Two boolean expressions that produce the same output on every row of a truth table.
- De Morgan's law
- The rule that !(a && b) equals !a || !b, and !(a || b) equals !a && !b.
- Short-circuit
- The behavior where && or || stops evaluating as soon as the overall result is already determined.
Watch it explained
- How it worksHow does a truth table prove two boolean expressions match in every single case?
- Worked exampleBuild the truth table for a && (b || c) and set it beside (a && b) || (a && c).
- Worked exampleFind whether !a || b behaves the same as a && !b across all four rows.
- Going furtherWhat happens to the height of a truth table when a third and fourth variable join in?
Check yourself
End of topic.