Boolean Logic

⏱ 8 min✏️ Quiz at the end

Thinking in True and False

"Is it raining?" "Is the door locked?" "Has the player run out of lives?" Each question has exactly two possible answers: true or false. Logic built from these two values is called Boolean logic, after George Boole, the 19th-century mathematician who worked out its rules β€” a full century before electronic computers existed to use them.

You have already met Boolean values without the name: every condition in an if statement, and every test that keeps a loop running, boils down to a single true or false. Boolean logic is what lets you build bigger questions out of smaller ones.

AND β€” Everything Must Be True

A AND B is true only when both parts are true.

"You may watch a film if it is the weekend AND your homework is done." Saturday with unfinished homework? No film. Homework done on a Tuesday? Still no film. Only both together open the door.

Truth table β€” a table listing every possible combination:

ABA AND B
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

One false anywhere makes the whole thing false. AND is the strict parent of the logic world.

OR β€” At Least One Must Be True

A OR B is true when at least one part is true.

"Free entry if you are under 13 OR over 60." Either qualifies. Note the fine print: in Boolean logic, both being true still counts as true β€” OR means "at least one", not "exactly one".

ABA OR B
falsefalsefalse
falsetruetrue
truefalsetrue
truetruetrue

NOT β€” The Flipper

NOT A simply inverts a value: NOT true is false, NOT false is true. Unlike AND and OR, it takes just one input.

ANOT A
falsetrue
truefalse

NOT lets you express the opposite of what you can easily test. A game might keep running while NOT game-over β€” the natural way to say "while the game is still going".

Building Bigger Expressions

The real power arrives when you combine operators. Brackets show what to work out first, exactly as in arithmetic:

allowed to ride = (height over 120cm) AND (NOT pregnant) AND ((age over 10) OR (adult present))

Work from the inside out with actual values β€” height 130cm, not pregnant, age 9, adult present:

  1. height over 120cm β†’ true
  2. NOT pregnant β†’ NOT false β†’ true
  3. (age over 10) OR (adult present) β†’ false OR true β†’ true
  4. true AND true AND true β†’ allowed to ride

Evaluating step by step like this is the same discipline as tracing a sequence of instructions β€” slow, mechanical, and reliable. When a program makes a wrong decision, debugging usually means evaluating its Boolean expression by hand to find the part that does not say what you meant.

Watch Out: Everyday English Lies

Human language is sloppy with these words, and that causes real bugs:

  • "Would you like tea or coffee?" β€” English expects one; Boolean OR happily accepts both.
  • "The pool is closed on Saturday and Sunday." β€” English means closed on each day; a literal day is Saturday AND day is Sunday is always false, because no day is both! The correct condition is day is Saturday OR day is Sunday.

That second mistake is one of the most common logic errors in beginners' programs. When translating English rules into Boolean logic, ask: which combinations of true and false do I actually want? β€” then check your expression against a truth table.

Boolean logic is also the bridge to hardware: computer circuits implement AND, OR, and NOT physically as logic gates, and every decision your computer makes is billions of these tiny true/false operations combined. And because true/false is a two-state value, it fits perfectly in one bit of binary.

Worked Example β€” A Full Truth Table for a Combined Expression

Building a complete truth table β€” every possible combination of inputs, worked out systematically β€” is the most reliable way to fully understand a tricky Boolean expression, rather than checking just one or two cases. Take the vending machine expression from earlier: moneyInserted AND (itemSelected OR NOT lidClosed). With three separate true/false inputs, there are 2Γ—2Γ—2 = 8 possible combinations to check:

moneyInserteditemSelectedlidClosedNOT lidCloseditemSelected OR NOT lidClosedFinal result
falsefalsefalsetruetruefalse
falsefalsetruefalsefalsefalse
falsetruefalsetruetruefalse
falsetruetruefalsetruefalse
truefalsefalsetruetruetrue
truefalsetruefalsefalsefalse
truetruefalsetruetruetrue
truetruetruefalsetruetrue

Building the table one column at a time β€” computing NOT lidClosed first, then the OR, then the final AND β€” is far less error-prone than trying to evaluate the whole expression in your head for all eight rows at once. This systematic, column-by-column construction is exactly how professional testing verifies that complex conditions behave correctly on every combination, not just the ones a programmer happened to think of first.

Short-Circuit Evaluation: Sometimes the Computer Skips Work

One efficiency trick is worth knowing, because it occasionally causes surprising behaviour. Consider hasPassword AND checkPasswordCorrect(enteredPassword). If hasPassword is false, many programming languages will never even evaluate the second part β€” because AND already knows the overall result must be false the moment one side is false, so checking the other side would be wasted work. This is called short-circuit evaluation. It usually just saves a little time, but it can matter: if the second part of an expression has some other effect beyond returning true/false (like recording an attempted login), that effect might never happen if the first part was already false. Understanding this is part of the deeper habit this whole course builds: not just knowing what a condition means, but knowing exactly how and when a computer actually evaluates it.

Key Words

  • Boolean β€” a value that is either true or false
  • AND β€” true only if all parts are true
  • OR β€” true if at least one part is true
  • NOT β€” flips true to false and false to true
  • Truth table β€” a table showing an expression's result for every combination of inputs