Logical Operators and Boolean Algebra

College Depth 43 in the knowledge graph I know this Set as goal
Unlocks 612 downstream topics
operators logic boolean

Core Idea

Logical operators (AND, OR, NOT) combine boolean values. AND requires both operands true; OR requires at least one true; NOT negates. Short-circuit evaluation optimizes by not evaluating unnecessary sub-expressions.

How It's Best Learned

Build truth tables for logical expressions. Test short-circuit behavior with side effects.

Common Misconceptions

Explainer

From boolean types and comparison operators, you know that expressions like `x > 5` and `name == "Alice"` evaluate to `True` or `False`. But real-world conditions are rarely that simple. "Is the user logged in *and* is their subscription active?" "Is the input empty *or* does it contain invalid characters?" Logical operators — `AND`, `OR`, and `NOT` — let you combine multiple boolean values into a single compound condition, giving you the power to express complex decision rules in one line.

AND requires *both* operands to be true for the result to be true. `is_logged_in AND has_permission` is true only when both conditions hold — if either is false, the whole expression is false. OR requires *at least one* operand to be true. `is_admin OR is_owner` is true if either condition holds, and only false when both are false. NOT inverts a single boolean: `NOT is_locked` is true when `is_locked` is false. A useful way to internalize these rules is to build a truth table — a grid that lists every combination of inputs and the resulting output. For AND with two inputs, there are four rows (TT, TF, FT, FF), and only the first row produces true. For OR, only the last row (FF) produces false.

Most programming languages implement short-circuit evaluation, which is both an optimization and a feature you can exploit. With AND, if the first operand is false, the result *must* be false regardless of the second operand, so the second operand is never evaluated. With OR, if the first operand is true, the result *must* be true, so the second operand is skipped. This matters when the second operand has a side effect or could cause an error. The classic pattern is `if list is not empty AND list[0] == target:` — short-circuit evaluation ensures that `list[0]` is only accessed when the list is non-empty, preventing an index error.

When combining multiple logical operators, precedence determines the order of evaluation: NOT binds tightest, then AND, then OR. The expression `a OR b AND c` is evaluated as `a OR (b AND c)`, not `(a OR b) AND c`. This parallels arithmetic, where multiplication binds tighter than addition. When in doubt, use parentheses to make your intent explicit — `(a OR b) AND c` leaves no ambiguity. De Morgan's laws provide useful equivalences for simplifying or rewriting compound conditions: `NOT (a AND b)` equals `(NOT a) OR (NOT b)`, and `NOT (a OR b)` equals `(NOT a) AND (NOT b)`. These laws are especially helpful when you need to negate a complex condition and want to distribute the NOT across each part.

Practice Questions 5 questions

Prerequisite Chain

Longest path: 44 steps · 198 total prerequisite topics

Prerequisites (2)

Leads To (1)