7.4 Boolean Expressions, Truth Tables, and Boolean Algebra
Key Takeaways
- and is true only when both operands are true; or is true when at least one is true; not reverses a value; a truth table for n variables has 2ⁿ rows.
- De Morgan's laws: not ( A and B ) equals ( not A ) or ( not B ), and not ( A or B ) equals ( not A ) and ( not B ).
- Negating a comparison flips it: not ( x > 10 ) is x ≤ 10, and not ( x == y ) is x ≠ y.
- Nested if statements that both must pass are equivalent to one condition joined with and; alternative conditions that lead to the same action are joined with or.
- Short-circuit evaluation skips the right operand of and when the left is false, and the right operand of or when the left is true, so ( i < n ) and ( a[i] > 0 ) never reads a[n].
What this competency asks
Within the operators competency, ETS lists two Boolean skills:
- Use Boolean algebra to identify equivalent Boolean expressions.
- Write a Boolean expression equivalent to given code, or identify code equivalent to a given Boolean expression or English description.
ETS's sample question on this topic gives ( age > 10 ) and ( height > 36 ) and asks for an equivalent expression. The answer is not ( ( age ≤ 10 ) or ( height ≤ 36 ) ), which uses De Morgan's law and flips each comparison correctly. The trap choices negate > as < instead of ≤, or keep and where De Morgan's law requires or.
Operators and truth tables
| A | B | not A | A and B | A or B | A xor B |
|---|---|---|---|---|---|
| false | false | true | false | false | false |
| false | true | true | false | true | true |
| true | false | false | false | true | true |
| true | true | false | true | true | false |
ETS pseudocode uses the words and, or, and not. Other languages write &&, ||, and !. XOR (exclusive or) is true when exactly one operand is true. It is not an ETS keyword, but it can be written as ( A or B ) and not ( A and B ).
A truth table for n variables has 2ⁿ rows. Two expressions are equivalent exactly when their truth-table columns match in every row. For three variables that is 8 rows. For example, ( A and not B ) or ( not A and C ) is true in rows (A, B, C) = (F, F, T), (F, T, T), (T, F, F), and (T, F, T), and false in the other four.
Precedence
not is evaluated first, then and, then or. So A or B and C means A or ( B and C ), and not A and B means ( not A ) and B. With A = false, B = true, and C = true:
not A and B or not C and A=( true and true ) or ( false and false )= true or false = true.
Laws of Boolean algebra
| Law | And form | Or form |
|---|---|---|
| Identity | A and true = A | A or false = A |
| Domination | A and false = false | A or true = true |
| Idempotent | A and A = A | A or A = A |
| Complement | A and not A = false | A or not A = true |
| Double negation | not ( not A ) = A | |
| Commutative | A and B = B and A | A or B = B or A |
| Distributive | A and ( B or C ) = ( A and B ) or ( A and C ) | A or ( B and C ) = ( A or B ) and ( A or C ) |
| Absorption | A and ( A or B ) = A | A or ( A and B ) = A |
| De Morgan | not ( A and B ) = not A or not B | not ( A or B ) = not A and not B |
Worked simplification
Simplify not ( not A or B ) or ( A and B ):
- De Morgan on the first part:
not ( not A or B )=not not A and not B=A and not B. - The expression is now
( A and not B ) or ( A and B ). - Factor out A:
A and ( not B or B ). - Complement law:
not B or B= true, so the expression isA and true. - Identity law:
A.
Negating comparisons
To apply De Morgan to real conditions, you must negate each comparison exactly:
| Condition | Its negation |
|---|---|
x > 10 | x ≤ 10 |
x ≥ 10 | x < 10 |
x == y | x ≠ y |
( x ≥ 10 ) and ( x ≤ 20 ) (in the range) | ( x < 10 ) or ( x > 20 ) (outside the range) |
Example: the loop while ( ( loggedIn ) and ( tries < 5 ) ) stops when not ( loggedIn and ( tries < 5 ) ) becomes true, which is ( not loggedIn ) or ( tries ≥ 5 ).
From code to a Boolean expression, and back
Nested ifs mean "and"
if ( x > 0 )
if ( y > 0 )
print "both positive"
end if
end if
This prints exactly when ( x > 0 ) and ( y > 0 ).
Separate conditions with the same action mean "or"
boolean discount ← false
if ( age < 13 )
discount ← true
end if
if ( age ≥ 65 )
discount ← true
end if
This is equivalent to discount ← ( age < 13 ) or ( age ≥ 65 ).
If/else that returns a Boolean
if ( score ≥ 70 ) return true else return false end if is simply return score ≥ 70.
From English
| Description | Expression |
|---|---|
| "x is between 10 and 20, inclusive" | ( x ≥ 10 ) and ( x ≤ 20 ) |
| "x is outside that range" | ( x < 10 ) or ( x > 20 ) |
| "neither a nor b is true" | not a and not b, which is not ( a or b ) |
| "not both a and b" | not ( a and b ), which is not a or not b |
| "exactly one of a and b" | ( a or b ) and not ( a and b ) |
A frequent error is writing ( x < 10 ) and ( x > 20 ) for "outside the range." No number is both below 10 and above 20, so that condition is always false.
Short-circuit evaluation
Most languages stop evaluating as soon as the result is known:
- In
A and B, if A is false, B is not evaluated (the result must be false). - In
A or B, if A is true, B is not evaluated (the result must be true).
Programmers use this as a guard:
if ( ( i < n ) and ( list[i] == target ) )
When i = n, the left side is false, so list[n] is never read. Reversing the order would read past the end. The same idea guards against division by zero, as in ( count ≠ 0 ) and ( total / count > 50 ). Because the right side might not run, avoid putting required actions, such as a procedure call that updates a counter, inside a condition.
Boolean logic in hardware
The same algebra describes logic gates. AND, OR, and NOT gates, built from transistors, compute these functions on voltages. NAND (not-and) and NOR (not-or) are each functionally complete: any Boolean function can be built using only NAND gates, or only NOR gates. For example, NOT A = A NAND A. This is why logic gates sit near the bottom of the abstraction hierarchy in Section 4.1.
Which expression is equivalent to not ( ( temp ≥ 32 ) and ( weather ≠ "snow" ) )?
Which single condition makes the following code print "both" in exactly the same situations?
if ( x > 0 )
if ( y > 0 )
print "both"
end if
end if
Which condition is true exactly when x is outside the range 10 to 20, inclusive?
An array list has valid indexes 0 through n − 1. Which condition safely checks whether the element at index i equals target when i might equal n, assuming short-circuit evaluation?