🪴 GoDeep
Search
← Computer Science

Propositional logic: the algebra of true and false

How propositions and the connectives ¬, ∧, ∨, → and ↔ turn everyday reasoning into an algebra your code already runs.

14 cards · 8 quiz questions · 9 min read

Every if statement you have ever written is a small piece of mathematics. Propositional logic is the study of how statements that are definitely true or definitely false combine into larger statements, and of exactly what those combinations mean. George Boole made the case in 1854 that reasoning of this kind is an algebra, with its own operations and its own laws; a century later that algebra was etched into silicon. Learning it properly is not an academic detour, because the notation is already in your editor under different names.

Propositions: the atoms

A proposition is a declarative statement with a definite truth value: true or false, never both, never neither. “7 is prime” is a proposition (true). “Paris is in Spain” is a proposition (false). “Close the door” is not — a command cannot be true. Nor is “x is greater than 3”, because until you fix x there is nothing to evaluate; that is an open sentence, and it becomes a proposition only once x gets a value. The distinction matters in code: isAdmin holds a proposition, while age > 3 is a template that produces one when age is bound.

Propositional logic deliberately ignores what a proposition says. It cares only about truth values and how they flow through the connectives. That abstraction is what lets one truth table describe both a philosophy argument and a database query.

The five connectives

Five operators build every compound proposition.

  • ¬ (not) flips a truth value. ¬p is true exactly when p is false. In code: !p.
  • ∧ (and) is true only when both operands are true. In code: &&.
  • ∨ (or) is true when at least one operand is true — including when both are. In code: ||.
  • → (implies) is the conditional “if p then q”.
  • ↔ (if and only if) is true when both sides share the same truth value.

Written as a table, with T for true and F for false:

pq¬pp ∧ qp ∨ qp ⊕ qp → qp ↔ q
TTFTTFTT
TFFFTTFF
FTTFTTTF
FFTFFFTT

The extra column, ⊕ (exclusive-or), is the “one or the other but not both” sense that English usually means by or: soup or salad, not both. Mathematical ∨ is inclusive — it says nothing against both being true. That mismatch with everyday speech is the first place beginners stumble, and it is why a form validator written as “email or phone is required” almost never means ⊕.

A useful way to see ⊕: p ⊕ q is true exactly when p and q differ. Which is why it is the same operation as bitwise XOR, and why a ^ a is always zero.

Why → is true when the antecedent is false

Look again at the → column. It is false in exactly one row: p true, q false. Every row where p is false comes out true. This trips up nearly everyone, so it is worth getting straight.

Read the conditional as a promise: “if p happens, I guarantee q.” The promise is broken only if p happens and q does not. If p never happens, you have not broken anything, so the promise stands. A conditional with a false antecedent is called vacuously true.

“If this list contains any negative numbers, they are all even.” Said of an empty list, this is true — not because of some quirk, but because there is no counterexample to be found.

Your language agrees. In JavaScript, [].every(n => n < 0) returns true; in Python, all([]) is True. Both are answering a universally quantified conditional over an empty collection, and both give the vacuous answer, because any other choice would break the algebra.

One more rewrite makes → less mysterious: p → q is equivalent to ¬p ∨ q. Either the antecedent fails, or the consequent holds. Check it against the table — the two columns match row for row.

Tautology, contradiction, contingency

Classify a formula by the whole of its truth table, not one row.

  • A tautology is true in every row: p ∨ ¬p, the law of the excluded middle.
  • A contradiction is false in every row: p ∧ ¬p.
  • A contingency is neither — true sometimes, false sometimes. Almost every condition you write is a contingency; if a condition turns out to be a tautology, it is dead code, and if it is a contradiction, the branch never runs. Linters that flag if (x || !x) are doing propositional logic on your behalf.

Two formulas are logically equivalent, written p ≡ q, when their columns are identical in every row — equivalently, when their biconditional is a tautology. Equivalence is the licence to rewrite: anywhere one appears, the other can replace it without changing behaviour.

Where it lands in code

Short-circuit evaluation is the one place where code and logic gently part ways. Logically p ∧ q and q ∧ p are the same formula. In code, a && b never evaluates b when a is false, and a || b never evaluates b when a is true, because the answer is already fixed. The truth value is unchanged, but side effects and errors are not:

// Order matters: the guard must come first.
if (user && user.profile.name) { ... }

// Same truth table, but this one can throw.
if (user.profile.name && user) { ... }

The ternary is the conditional in expression form, and it is worth noticing that cond ? a : b is not → — it is a selection, closer to (cond ∧ a) ∨ (¬cond ∧ b). Meanwhile SQL adds a third truth value, NULL, which is why WHERE col = NULL matches nothing and you must write IS NULL instead. That is a genuine departure from two-valued logic, and it catches people precisely because they are reasoning with the truth tables above.

Get the five connectives and their tables into your fingers, and a surprising amount of debugging turns into checking a row.

Sources

  • Kenneth H. Rosen — Discrete Mathematics and Its Applications book Chapter 1 is the standard undergraduate treatment of propositional logic and truth tables.
  • Daniel J. Velleman — How to Prove It: A Structured Approach book Chapter 1 builds propositional connectives up carefully before using them in proofs.
  • Stanford Encyclopedia of Philosophy — Propositional Logic website Careful account of syntax, semantics and the material conditional.
  • George Boole — An Investigation of the Laws of Thought book The 1854 work that first treated logic as an algebra, and the reason booleans carry his name.