Unit 3: Programming · Logical equivalence

Article

Same meaning

6 min read

A shop has two signs. One says: "Discount if you are not an adult." The other says: "Discount if you are under 18." The words are different, but the same people get the discount. Programmers call this logical equivalence, and the AP exam asks about it often.

Same result in every case

A Boolean expression is an expression whose value is true or false, such as age < 18. Two Boolean expressions are logically equivalent when they give the same result in every possible case. "Every possible case" means every possible value of the variables in them.

Here are the two signs as expressions, tested with four ages:

ageage < 18NOT (age ≥ 18)
10truetrue
17truetrue
18falsefalse
40falsefalse

The two columns match in every row, so these expressions are equivalent. Here is the same test in AP pseudocode, then in Python. The exam shows AP pseudocode, and Python is what you can run. Python writes not where AP writes NOT, and it prints True where AP shows true.

AP pseudocode
age ← 17
DISPLAY(age < 18)
DISPLAY(NOT (age ≥ 18))
Runs in your browser

Change age to 18, then 40, then 5, and run again. The two lines always match.

Check your understanding

What does it mean for two Boolean expressions to be logically equivalent?

Checking with a truth table

You cannot test every number. But a Boolean variable (one that holds true or false) has only two values, so you can test them all. A truth table lists every combination of values and the result of each expression.

Two Booleans, a and b, make 4 combinations. Each new Boolean doubles the rows, just like each new bit doubles the values a binary number can hold. Three Booleans make 8 rows, and four make 16.

Is a AND b equivalent to b AND a?

aba AND bb AND a
truetruetruetrue
truefalsefalsefalse
falsetruefalsefalse
falsefalsefalsefalse

All four rows match, so yes. Now try a AND b against a OR b:

aba AND ba OR b
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

Two rows differ, so these are not equivalent. One row that differs is already enough to prove it. But to prove two expressions are equivalent, every row must match. Matching in two or three rows proves nothing.

Check your understanding

A truth table compares two expressions that use three Boolean variables, a, b and c. How many rows does the table need to show every case?

Worked example

Is NOT (a AND b) equivalent to (NOT a) AND (NOT b)? We write brackets so it is clear what each NOT applies to. Fill in a column for each part:

aba AND bNOT (a AND b)NOT aNOT b(NOT a) AND (NOT b)
truetruetruefalsefalsefalsefalse
truefalsefalsetruefalsetruefalse
falsetruefalsetruetruefalsefalse
falsefalsefalsetruetruetruetrue

In the second and third rows the fourth column is true and the last column is false. They are not equivalent. The next article shows the right partner for NOT (a AND b).

Common mistakes

  • Testing one or two cases and saying "equivalent". Matching in a few cases is not enough. You need every case.
  • Thinking "equivalent" means "both true". Two equivalent expressions can both be false. They only have to give the same answer as each other.
  • Thinking the same variables means the same meaning. a AND b and a OR b use the same variables and are very different.
  • Comparing the wrong rows. Always compare two expressions in the same row of the table.

On the exam

You will see "Which of the following is equivalent to…?" with four options. You do not need a full table. Pick values for the variables that make the original expression give true, then check which options also give true. Then pick values that give false. An option that gives a different answer even once is wrong.

Key words

Boolean expression: an expression whose value is true or false.

Logically equivalent: giving the same result in every possible case.

Truth table: a table that lists every combination of values of the variables and the result of each expression.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.