AP Computer Science Principles · lesson 3 of 4 · 20 min
Boolean logic fundamentals
Boolean logic operates on values that are either
TRUE
or
FALSE
. Every conditional in every program — every
if
statement, every loop condition — evaluates to a Boolean value. The three fundamental Boolean operators are AND, OR, and NOT.
AND operator
AND returns TRUE only when both operands are TRUE. Think of it as a series circuit — both switches must be closed for current to flow.
OR operator
OR returns TRUE when at least one operand is TRUE. Think of it as a parallel circuit — either switch being closed allows current to flow.
NOT operator
NOT inverts a single Boolean value. NOT TRUE = FALSE; NOT FALSE = TRUE.
Compound conditions
Real programs combine operators. Operator precedence: NOT is evaluated first, then AND, then OR. Use parentheses to override precedence. // Is a user eligible for a discount? // Must be a student OR a senior, AND must not be banned
eligible ← (isStudent OR isSenior) AND (NOT isBanned)
// If isStudent=TRUE, isSenior=FALSE, isBanned=FALSE:
- // (TRUE OR FALSE) AND (NOT FALSE)
- // TRUE AND TRUE
- // TRUE → eligible
On the AP exam, trace compound Boolean expressions step by step. Write out each sub-expression before combining them.
Short-circuit evaluation
Most languages use short-circuit evaluation: for AND, if the first operand is FALSE the second is never evaluated (the result must be FALSE). For OR, if the first operand is TRUE the second is never evaluated (the result must be TRUE). This matters when the second operand has a side effect or could cause an error.
Knowledge check
- Question of
- Hide hint
- Show hint
✓ Correct! ✕ Not quite
- Try again
- See results
- Next question
- Check answer
Previous lesson: Binary numbers and data representation
Back to AP Computer Science Principles
Next lesson: How the Internet works: packets and protocols