Boolean Logic
Boolean logic is the foundation of all decision-making in computing — and in everyday thinking. It deals with values that are either true or false, and combines them using three basic operations: AND, OR, and NOT.
You use Boolean logic every day, even if you have never heard the term. This chapter shows how it works in ordinary decisions and then connects it to technology.
The Basics: True & False
Boolean logic works with exactly two values: true and false. Every statement in Boolean logic evaluates to one or the other.
"The sky is blue." -> true (on a clear day)
"It is currently raining." -> true or false (depends on the weather)
"2 + 2 = 5." -> false
These true/false values become powerful when you combine them.
The Three Operations
AND
AND requires both conditions to be true for the result to be true.
"I'll go to the beach if it's sunny AND I'm free."
sunny = true, free = true -> go to beach (true)
sunny = true, free = false -> don't go (false)
sunny = false, free = true -> don't go (false)
sunny = false, free = false -> don't go (false)
Both conditions must be met. If either one is false, the whole thing is false.
Everyday examples of AND:
- "I'll buy this shirt if it's in my size AND under $30."
- "You can drive if you have a license AND insurance."
- "We'll launch the project if the budget is approved AND the team is ready."
OR
OR requires at least one condition to be true for the result to be true.
"I'll have tea OR coffee with breakfast."
tea = true, coffee = true -> satisfied (true)
tea = true, coffee = false -> satisfied (true)
tea = false, coffee = true -> satisfied (true)
tea = false, coffee = false -> nothing to drink (false)
Only when both are false does the result become false.
Everyday examples of OR:
- "I'll contact you by phone OR email."
- "We accept Visa OR Mastercard."
- "I'll go to the gym OR go for a run today."
Note: In everyday language, "or" sometimes means "one or the other but not both" (exclusive or). In Boolean logic, OR means "at least one," so both being true also counts as true.
NOT
NOT flips a value. True becomes false, and false becomes true.
"I will NOT go if it rains."
raining = true -> NOT true = false (don't go)
raining = false -> NOT false = true (go)
NOT is often used to express exceptions or exclusions:
- "Show me all items that are NOT sold out."
- "I'm free every day this week, NOT including Wednesday."
- "Everyone passed the test except — NOT — the students who scored below 60."
Combining Operations
The real power of Boolean logic comes from combining AND, OR, and NOT.
Shopping Filters
When you filter products on a shopping website, you are writing Boolean logic:
"Show me shoes that are (black OR brown) AND under $100"
Shoe: black, $80 -> (true OR false) AND true = true AND true = true (show)
Shoe: brown, $120 -> (false OR true) AND false = true AND false = false (hide)
Shoe: red, $60 -> (false OR false) AND true = false AND true = false (hide)
Shoe: brown, $75 -> (false OR true) AND true = true AND true = true (show)
The parentheses matter. Without them, the meaning could change — just like in math where 2 + 3 x 4 is different from (2 + 3) x 4.
Event Planning
"We'll hold the event outdoors if it's NOT raining AND the temperature
is above 60 AND the venue is available."
All three must be true. If any one is false, the event moves indoors.
Eligibility Rules
"You qualify for the discount if you are a student OR a senior citizen,
AND your purchase is over $25."
Translation:
(student OR senior) AND purchase > $25
Student, $30 purchase: (true OR false) AND true = true (discount)
Senior, $20 purchase: (false OR true) AND false = false (no discount)
Neither, $50 purchase: (false OR false) AND true = false (no discount)
Truth Tables
A truth table lists every possible combination of inputs and the resulting output. It is a complete description of how a Boolean expression behaves.
AND Truth Table:
A B A AND B
true true true
true false false
false true false
false false false
OR Truth Table:
A B A OR B
true true true
true false true
false true true
false false false
NOT Truth Table:
A NOT A
true false
false true
Truth tables are useful when you need to verify that your logic handles every case correctly. For complex expressions, they prevent you from missing a combination.
Boolean Logic in Technology
Conditionals in Code
Every "if" statement in software is Boolean logic:
if user is logged in AND has admin role:
show admin dashboard
else:
show regular dashboard
Database Queries
When you search a database, you use Boolean logic to filter results:
Find all customers where:
city = "Chicago" AND (age > 30 OR membership = "premium")
This returns customers in Chicago who are either over 30 or have a premium membership (or both).
Search Engines
Search engines use Boolean logic behind the scenes. Some support it directly:
Search: python AND tutorial NOT video
This finds pages about Python tutorials that are not videos.
Even when search engines hide the Boolean syntax, they still use it internally to combine and filter results.
Circuit Design
At the hardware level, computers are built from logic gates — physical implementations of AND, OR, and NOT. Every computation a computer performs is ultimately done through billions of these gates implemented in silicon.
Order of Operations
Just like math has an order of operations (multiplication before addition), Boolean logic does too:
1. NOT is evaluated first
2. AND is evaluated second
3. OR is evaluated last
This means:
A OR B AND NOT C
is evaluated as:
A OR (B AND (NOT C))
NOT: C is flipped first
AND: B AND (NOT C) is evaluated second
OR: A OR (result) is evaluated last
When in doubt, use parentheses to make your intent clear. Explicit parentheses prevent mistakes and make your logic easier for others to read.
Common Pitfalls
Confusing AND and OR
This is the most common mistake. "I want shoes that are black and brown" — do you mean shoes that are both colors at once (AND)? Or shoes that are either color (OR)? In Boolean terms, you almost certainly mean OR.
Forgetting about NOT edge cases
"Show me items that are NOT (in stock AND on sale)" is different from "Show me items that are NOT in stock AND NOT on sale." The first shows anything that fails to be both in stock and on sale. The second shows only items that are neither in stock nor on sale.
Ignoring order of operations
Without parentheses, Boolean expressions can be evaluated differently than you intended. Always use parentheses when combining AND and OR in the same expression.
Assuming everyday "or" matches Boolean OR
In everyday language, "Would you like tea or coffee?" usually means "pick one." In Boolean logic, OR includes the case where both are true. This mismatch catches people off guard.
Overcomplicating expressions
If a Boolean expression is hard to read, it is probably doing too much. Break it into named pieces:
Instead of:
(age > 18 AND status = "active" AND NOT suspended) OR role = "admin"
Try:
eligible_user = age > 18 AND status = "active" AND NOT suspended
result = eligible_user OR role = "admin"
Naming intermediate values makes the logic much clearer.
Key Takeaways
- Boolean logic works with two values — true and false — and three operations: AND, OR, and NOT.
- AND requires all conditions to be true. OR requires at least one. NOT flips the value.
- You use Boolean logic constantly in everyday decisions, shopping filters, eligibility rules, and planning.
- In technology, Boolean logic powers conditionals, database queries, search engines, and the physical circuits inside computers.
- Parentheses clarify the order of evaluation and prevent mistakes.
- When Boolean expressions get complex, break them into named pieces for clarity.