Boolean expressions and truth tables
A boolean expression is a condition that comes out true or false. A truth table settles what it means. You list every combination of inputs, work the condition out for each row, and read the answers down one column. Done the same way each time, it beats guessing what an `if` statement will do.
For students and parents planning test prep or coursework: one hour with George Li, one to one by video, is $180, $200 or $160, by subject. You leave with a written plan within one working day.
Book your first session| A | B | A and B | A or B | not A | A != B |
|---|---|---|---|---|---|
| T | T | T | T | F | F |
| T | F | F | T | F | T |
| F | T | F | T | T | T |
| F | F | F | F | T | F |
T is true and F is false. The last column is exclusive or: true when exactly one input is true. Python writes it as != between two booleans.
Computed by George Li Tutoring: each operator was run on every pair of True and False values in Python 3; the operator definitions are in the Python documentation, Built-in Types
How many rows does a truth table need?
Every input can be true or false, so a condition with n inputs needs 2 to the power n rows. Two inputs give 4 rows, three give 8 and four give 16. If your table has fewer, a case is missing.
Fill the rows in a fixed pattern. The first input is true for the top half of the rows and false for the bottom half. The second input flips twice as often, and the third flips every row. For three inputs the rows run TTT, TTF, TFT, TFF, FTT, FTF, FFT, FFF. A fixed order means no row is skipped or repeated, and you can compare your table with someone else's one row at a time.
How do you build the table for a longer condition?
Give each piece of the condition its own column, and work from the inside out. Take a and (b or not c). The innermost piece is not c, then b or not c, then the whole condition.
| a | b | c | not c | b or not c | a and (b or not c) |
|---|---|---|---|---|---|
| T | T | T | F | T | T |
| T | T | F | T | T | T |
| T | F | T | F | F | F |
| T | F | F | T | T | T |
| F | T | T | F | T | F |
| F | T | F | T | T | F |
| F | F | T | F | F | F |
| F | F | F | T | T | F |
The whole condition is true in 3 of the 8 rows. Each of them has a true and then either b true or c false. The five false rows are easy to explain too. Four of them have a false, and the fifth has a true but b false and c true, so the bracket fails.
If you are building these skills for a class or an exam, the tutoring for high school courses page shows how sessions work, including help with AP® Computer Science A. The study planner turns your weeks and weekly hours into a schedule.
How do you test whether two conditions mean the same thing?
Build a column for each condition over the same rows. If the columns match in every row, the conditions are equivalent. If they differ in even one row, they are not.
| A | B | not (A or B) | (not A) and (not B) | not (A and B) | (not A) or (not B) |
|---|---|---|---|---|---|
| T | T | F | F | F | F |
| T | F | F | F | T | T |
| F | T | F | F | T | T |
| F | F | T | T | T | T |
Columns three and four match, and so do columns five and six. These are De Morgan's laws: negating an or gives an and of the negations, and negating an and gives an or of the negations. The table also shows a common slip. Columns five and four differ in the two middle rows, so not (A and B) does not equal (not A) and (not B). A not that moves inside brackets must also swap the operator.
Worked example: Check that a negated age condition matches its rewrite
Show that the two conditions are equivalent. Let P stand for age >= 13 and Q stand for has_form.
- The left condition is not (P and Q). For the rows PQ = TT, TF, FT, FF, P and Q gives T, F, F, F, so the left condition is F, T, T, T.
- The comparison
age < 13is exactly not P. So the right condition is (not P) or (not Q). - For the same four rows, (not P) is F, F, T, T and (not Q) is F, T, F, T, so the right condition is F, T, T, T.
- The two columns agree in all four rows.
- Spot check. Take age 12 with has_form true. The inner and is false because 12 is under 13, so its negation is true, and the rewrite
age < 13is also true. Take age 20 with has_form true. Now the inner and is true, so both sides come out false.
Answer: the conditions are equivalent. Negating >= gives <, and the and turns into an or.
Source: Original problem by George Li Tutoring; the rows were also checked in Python for every age from 0 to 29 with has_form true and false.
What order do and, or and not run in without brackets?
Python's documentation lists the three in ascending priority: or, then and, then not. Comparisons such as == and < bind tighter than all three. So a or b and c is read as a or (b and c). The documentation also notes that not a == b is read as not (a == b), and that a == not b is a syntax error.
The order changes answers. Let a be true, and b and c be false. Then a or b and c is true, because the and runs first and the or only needs a. But (a or b) and c is false. Across all 8 rows the two readings differ in two of them, the rows where a is true and c is false. When a reader would have to stop and recall the order, add brackets.
What do and and or return in Python?
They return one of their operands, not always True or False. The documentation gives the rule: x or y is x when x is true, and y otherwise. And x and y is x when x is false, and y otherwise. So 0 or "none" is "none", "hi" and 0 is 0, [] or "default" is "default" and 3 and 4 is 4.
That works because of what Python counts as false. The documentation lists None, False, zero of any numeric type, and empty sequences and collections such as '', (), [] and {}. Everything else counts as true unless its class says otherwise.
How does short-circuiting protect a condition?
Both operators stop as soon as the answer is known. In x or y, Python looks at y only when x is false. In x and y, it looks at y only when x is true. Oracle's tutorial says the same of Java's && and ||: the second operand is evaluated only if needed.
That makes the order of the operands a safety tool. With d equal to 0, d != 0 and 12 / d > 2 is False and the division never runs. Swap the operands and 12 / d > 2 and d != 0 raises a ZeroDivisionError, because the division runs first. The guard has to come first.
Where do these ideas come from?
George Boole published an algebra of logic in the mid-1800s, with variables that take only two values and with operations for and, or and not. In the late 1930s Claude Shannon showed that the same algebra describes electrical switching circuits, which is why a condition in a program and a gate in a chip follow the same tables. You do not need the history for a test, but it explains why the rules below are laws and not conventions.
How do you negate a comparison?
Every comparison has an opposite, so you can push a not onto it and remove the brackets. For ordinary numbers the opposites pair up as follows.
| Comparison | Its negation |
|---|---|
a < b | a >= b |
a > b | a <= b |
a == b | a != b |
Combine that with De Morgan's laws and you can rewrite a negated condition without a leading not. Take not (score < 60 or passed). De Morgan turns the or into an and of the negations, which is not (score < 60) and not passed, and the first part becomes score >= 60. The result is score >= 60 and not passed. For a spot check, take score 75 with passed false. In the original, 75 < 60 is false and passed is false, so the or is false and the not makes the whole condition true. The rewrite gives 75 >= 60 true and not passed true, so it is also true, and the two agree.
How do you simplify a condition with the Boolean laws?
Truth tables prove that two conditions match. The laws below let you get from one to the other by steps, which is the only practical way to simplify a condition with many inputs. Each law has a pair, found by swapping and with or and true with false.
| Law | In and, or, not | What it lets you do |
|---|---|---|
| Identity | x or False is x, and x and True is x | Drop a constant that changes nothing |
| Domination | x or True is True, and x and False is False | Cut a whole sub-condition once a constant settles it |
| Idempotent | x or x is x, and x and x is x | Remove repeats |
| Complement | x or not x is True, and x and not x is False | Spot a condition that is always true or never true |
| Double negation | not not x is x | Cancel two nots |
| Commutative and associative | Order and grouping do not matter within all-and or all-or | Reorder and regroup freely |
| Distributive | x and (y or z) is (x and y) or (x and z), and x or (y and z) is (x or y) and (x or z) | Multiply out or factor |
| Absorption | x or (x and y) is x, and x and (x or y) is x | Delete a clause that the other one already covers |
The second distributive law is the one with no match in ordinary number algebra: or also spreads over and. If you remember only one, remember absorption, because it removes whole clauses.
Worked example: Simplify a three-clause condition
Simplify (x and y) or (x and not y) or (not x and y).
- The first two clauses share x. Factor it out:
(x and y) or (x and not y)isx and (y or not y). - The complement law makes
y or not ytrue, so the pair reduces tox and True, and the identity law leaves justx. - The condition is now
x or (not x and y). - A direct check shows that
x or (not x and y)isx or y: if x is true both sides are true, and if x is false the left side isyand the right side isy. - Confirm with a table over all four rows of x and y. The original and
x or yboth give T, T, T, F.
Answer: the condition is equivalent to x or y. All four rows were checked in Python 3.
Source: Original problem by George Li Tutoring; the rows were run in Python 3.
There are three ways to prove a law: a truth table, a chain of named steps like the one above, or a spot check on one input, which can only disprove a claim, never prove it.
What other connectives does logic class use?
Logic courses add two connectives that programs write differently. The conditional, which reads "p implies q", fails in exactly one case: a true p paired with a false q. It is equivalent to (not p) or q. The biconditional, "p exactly when q", is true when both sides have the same truth value, which in Python is p == q between two booleans.
| p | q | if p then q | p exactly when q |
|---|---|---|---|
| T | T | T | T |
| T | F | F | F |
| F | T | T | F |
| F | F | T | T |
The last column of any truth table also sorts a condition into one of three kinds. A tautology is true in every row, such as p or not p. A contradiction is false in every row, such as p and not p. Everything else is a contingency, such as p and q, true only in some rows. A tautology or a contradiction inside an if means a branch that always or never runs, and that is worth a second look.
For three inputs, (p and q) -> r has 8 rows and is false in exactly one of them: p true, q true and r false.
How do you write a range check correctly?
State both ends and join them with the right word. A value is in the range 0 to 100 when it is at least 0 and at most 100, so the condition is 0 <= x and x <= 100. Python also accepts the chained form 0 <= x <= 100, but Java does not, so learn the joined form first. The outside of the range uses or, not and: x < 0 or x > 100.
The most common slip is writing and for the outside, as in x < 0 and x > 100, which no number can satisfy. Test the edges on a table of values.
| x | 0 <= x and x <= 100 | x < 0 or x > 100 | x < 0 and x > 100 |
|---|---|---|---|
| -5 | False | True | False |
| 0 | True | False | False |
| 50 | True | False | False |
| 100 | True | False | False |
| 105 | False | True | False |
The two correct conditions are opposites in every row, which is De Morgan's law again. The wrong one is false in every row.
How does this read in Java and in a course pseudocode?
The tables do not change, but the symbols do. Java writes and as &&, or as || and not as !. Both && and || short-circuit, so a guard like arr != null && arr.length > 0 is safe. Java's order, from tightest to loosest, is: !, then the comparisons <, >, <=, >=, then == and !=, then &&, then ||. When you meet pseudocode with AND, OR and NOT in an exam course, the same order applies, with NOT binding tightest, AND next and OR loosest.
One Java trap sits outside the tables. For objects such as strings, == asks whether two names point to the same object, not whether the text matches. Use .equals() to compare text. The logic of the surrounding condition is the same either way.
Original practice
These problems are our own, and each answer was run in Python 3. Work each one before you read the answer.
- What is
3 > 2 and 5 < 4? Answer:3 > 2is true and5 < 4is false, so the result isFalse. - How many rows does the truth table of a condition with four inputs have? Answer: 2 to the power 4, which is 16 rows.
- With x equal to 3 and y equal to 2, what is
not (x > 5 or y == 2), and what is the same condition rewritten with De Morgan's law? Answer:x > 5is false andy == 2is true, so the or is true and the not makes itFalse. The rewrite isx <= 5 and y != 2, which is alsoFalsefor these values. - What are
[] or "default"and3 and 4? Answer:"default"and4, because each operator returns one of its operands. - With a true and b and c false, what are
a or b and cand(a or b) and c? Answer:TrueandFalse. The and runs before the or when there are no brackets. - With d equal to 0, what does
d != 0 and 12 / d > 2give, and why is the order important? Answer:False. The first operand is false, so the division is skipped. With the operands swapped, the division runs and raises aZeroDivisionError. - Simplify
x or (x and y)and name the law. Answer:x, by absorption. Check the four rows: both columns read T, T, F, F. - Is
x < 0 or x > 100the negation of0 <= x and x <= 100? Answer: Yes. De Morgan's law turns the negation intox < 0 or x > 100, and the two columns in the range table are opposite in every row.
If you miss one, label the cause: wrong operator meaning, missed a short-circuit, wrong order without brackets, or a faulty rewrite. Two misses with the same label tell you what to practice next. For more on turning a missed problem into a plan, see what to do with a first diagnostic test. We teach the method and do not complete graded work.
Common questions
What is a boolean expression?
An expression whose value is true or false, such as the comparison x > 3, or several conditions joined with and, or and not.
How many rows does a truth table have?
Two to the power of the number of inputs. Two inputs give 4 rows, three give 8 and four give 16.
What does short-circuit evaluation mean?
The language stops as soon as the answer is known. A false first operand settles an and, and a true first operand settles an or.
What is a tautology?
A condition that is true in every row of its truth table, such as p or not p.
How do I simplify a long condition?
Use the Boolean laws one step at a time, naming each law, and then confirm the result with a truth table. Absorption and the complement law remove the most clauses.
Do Python and Java spell the operators the same way?
No. Python uses the words and, or and not. Java writes and and or as && and ||. The truth tables are the same in both.