Contents

Computer Science › Math for Programmers

Boolean Logic

AND, OR, NOT and De Morgan's laws.

Also known as: boolean algebra, De Morgan's laws, logic operators

Boolean logic is the algebra of true and false. Three operations build everything else.

OperationSymbolTrue when
ANDand, &&both are true
ORor, ||at least one is true
NOTnot, !the input is false

A truth table lists every case:

ABA and BA or B
FFFF
FTFT
TFFT
TTTT

Useful laws

De Morgan’s laws let you push a NOT through a condition:

not (A and B)  ==  (not A) or (not B)
not (A or B)   ==  (not A) and (not B)
# "not (is_member and has_coupon)" is the same as:
(not is_member) or (not has_coupon)

Other handy equivalences: not not A == A, A and True == A, A or False == A, and the distributive form A and (B or C) == (A and B) or (A and C).

In code

  • Simplify conditions. if not (x > 5 and y < 3) can read better as if x <= 5 or y >= 3.
  • Avoid double negatives (if not is_disabled). Name booleans positively.
  • Short-circuiting stops evaluation early (short-circuit evaluation).
  • Precedence: and binds tighter than or. Use parentheses (operator precedence).
  • Empty cases: all([]) is true, any([]) is false.

Beyond if statements

The same logic drives SQL WHERE clauses (with a third value, NULL, see NULL in SQL), search filters, access rules, feature flags, and digital circuits. Bitwise operators apply it to each bit of a number.