Opening subject page...
Loading your content
The logical foundation that enables every decision a computer program makes.
Long before electronic computers existed, mathematicians grappled with a fundamental question: could the principles of logical reasoning be reduced to a symbolic, calculable system? The answer arrived in the mid-nineteenth century when George Boole, a self-taught English mathematician, published An Investigation of the Laws of Thought (1854). In this landmark work, Boole demonstrated that logical propositions could be expressed using algebraic equations in which variables take on only two values: true and false. This seemingly simple insight laid the groundwork for an entire mathematical discipline and, eventually, for digital computing itself.
For decades, Boolean algebra remained a purely theoretical pursuit within mathematical logic. It was not until 1937 that a young MIT graduate student named Claude Shannon recognized that Boole's two-valued algebra mapped perfectly onto the behavior of electrical relay circuits, in which a switch is either open or closed. Shannon's master's thesis, widely regarded as one of the most important theses of the twentieth century, demonstrated that any Boolean expression could be implemented as a circuit and, conversely, that any switching circuit could be described by a Boolean expression. This equivalence is the reason every modern computer operates on binary logic, and it is why understanding Boolean expressions is essential for every computer science student.
The central question this lesson addresses is straightforward yet profound: how do we construct, evaluate, and combine expressions that resolve to true or false, and how do these expressions drive the decision-making logic within algorithms and programs? Mastering Boolean expressions equips you to write correct conditionals, design efficient search filters, and reason about program behavior—skills tested directly on the AP Computer Science Principles exam.
A Boolean expression is any expression that evaluates to one of exactly two values: true or false. These two values constitute the Boolean data type, and they form the basis for every conditional and loop structure in programming. Boolean expressions can be as simple as a single variable that stores true, or they can be complex compositions involving relational operators and logical connectives. The fundamental ideas can be organized into several core principles.
true or false. There is no third possibility, no "maybe." This binary nature mirrors the 1s and 0s that underlie all digital computation.=, ≠, <, >, ≤, and ≥ compare two values and produce a Boolean result. For example, score ≥ 90 yields true when the variable score is 90 or above.AND, OR, and NOT—combine or invert Boolean values. AND requires both operands to be true; OR requires at least one; NOT flips the value.(age ≥ 18) AND (hasID = true). The order of evaluation matters and can be controlled with parentheses.AND, if the first operand is false, the result is false regardless. For OR, if the first is true, the result is true regardless.true) or remains closed (false). Every IF statement in your code is essentially a gate controlled by a Boolean expression.The behavior of logical operators can be completely described by truth tables, which enumerate every possible combination of input values and the corresponding output. The diagram below presents the truth tables for AND, OR, and NOT side by side, along with their standard logic-gate symbols. Study the pattern: AND yields true only when both inputs are true, while OR yields true when at least one input is true.
AND, OR, and NOT. Notice that AND yields true in only one of four rows, OR yields true in three of four rows, and NOT simply inverts its input.As shown in the diagram, the AND operator is the most restrictive of the three: it evaluates to true only when both operands are true. In contrast, OR is inclusive—it evaluates to true whenever at least one operand is true. The NOT operator is unary (it takes a single operand) and simply flips the value. These three operators, combined with relational comparisons, give you the full expressive power needed to model any logical condition in a program.
The AP Computer Science Principles exam uses a pseudocode language with specific syntax for Boolean expressions. Understanding how this pseudocode maps to general programming concepts is essential for answering exam questions correctly. Below we formalize the key operations and their precedence rules.
a and b are values of the same type (numbers, strings) and op is one of: =, ≠, <, >, ≤, ≥. The result is always true or false.condition1 AND condition2. If condition1 is false, the entire expression is false regardless of condition2.condition1 OR condition2. If condition1 is true, the entire expression is true regardless of condition2.NOT inverts the Boolean value of its operand. NOT true yields false, and NOT false yields true.When multiple operators appear in a single expression, the evaluation follows a defined order of precedence. NOT is evaluated first (highest precedence), followed by AND, and then OR (lowest precedence). Parentheses override this default order, so you should use them liberally to make your intent explicit and to avoid subtle bugs. For instance, the expression NOT A OR B is evaluated as (NOT A) OR B, not as NOT (A OR B)—and these two interpretations can produce different results.
AND, OR, and NOT as the logical operators. Always evaluate the innermost parentheses first, then apply the precedence rules: NOT → AND → OR.Boolean expressions can be classified by their complexity and by the patterns they follow. One of the most important patterns in Boolean logic is captured by De Morgan's Laws, a pair of equivalence rules that describe how the NOT operator distributes over AND and OR. These laws are tested on the AP exam and are invaluable for simplifying and reasoning about complex conditions.
NOT over a compound expression requires flipping the logical operator (AND becomes OR and vice versa) and negating each operand. The truth table verification confirms that both forms produce identical results for every input combination.De Morgan's Laws are not merely theoretical curiosities—they appear frequently on the AP exam in questions that ask you to identify equivalent Boolean expressions. The mnemonic is simple: when you push a NOT through parentheses, you must negate each operand and swap the operator (AND ↔ OR). Forgetting either step is a common source of errors.
| Expression Type | Example | Description |
|---|---|---|
| Simple Relational | x > 10 | Compares two values using a single relational operator. |
| Compound (AND) | x > 0 AND x < 100 | Both sub-expressions must be true; defines a range. |
| Compound (OR) | day = "Sat" OR day = "Sun" | At least one sub-expression must be true; models alternatives. |
| Negated | NOT (score < 60) | Equivalent to score ≥ 60; inverts the condition. |
| Nested Compound | (a OR b) AND (NOT c) | Combines multiple operators with explicit parentheses for clarity. |
Let us work through a complete evaluation of a compound Boolean expression, exactly as you would on the AP exam. Suppose we have three variables: age ← 20, hasTicket ← true, and isVIP ← false. We want to evaluate the expression: (age ≥ 18 AND hasTicket) OR isVIP.
age = 20, hasTicket = true, and isVIP = false. We substitute these values into the expression.(20 ≥ 18 AND true) OR false20 ≥ 18 first. Since 20 is greater than or equal to 18, this evaluates to true.(true AND true) OR falsetrue AND true. Both operands are true, so the AND operation yields true.true OR falsetrue OR false. Since OR requires only one operand to be true, and the left operand is true, the entire expression evaluates to true.Students frequently encounter a handful of recurring mistakes when constructing and evaluating Boolean expressions. Understanding these pitfalls before the exam can save valuable points. The table below contrasts common errors with their corrections.
| Pitfall | Incorrect Example | Correct Version | Explanation |
|---|---|---|---|
| Confusing AND / OR | x = 5 OR x = 10 when both must be true | x ≥ 5 AND x ≤ 10 | Use AND for ranges; a value cannot be both 5 and 10 simultaneously. |
| Missing NOT distribution | NOT (A AND B) → NOT A AND NOT B | NOT (A AND B) → (NOT A) OR (NOT B) | De Morgan's Law: you must also swap the operator. |
| Precedence error | NOT A OR B treated as NOT (A OR B) | (NOT A) OR B | NOT binds tighter than OR; use parentheses if you intend the alternative. |
| Redundant comparison | IF (x > 5) = true | IF (x > 5) | The relational expression already evaluates to a Boolean; comparing it to true is redundant. |
AND versus OR. A useful mental model: think of AND as a series circuit—both switches must be closed for current to flow—and OR as a parallel circuit—current flows if either switch is closed. If the English description uses words like "both" or "between," you likely need AND. If it uses "either" or "or," you likely need OR.Boolean expressions form the conceptual backbone that extends into several advanced areas of computer science. Within the AP CSP curriculum, they connect directly to conditionals (IF / ELSE), iteration (REPEAT UNTIL), and robot navigation logic. Beyond the AP course, Boolean logic is foundational to hardware design, database querying, formal verification, and artificial intelligence. The table below illustrates how the concepts you have learned scale to more complex domains.
| AP CSP Concept | Advanced Extension | How They Connect |
|---|---|---|
| Boolean variable (true/false) | Bit manipulation (0/1) | At the hardware level, true and false are represented as 1 and 0; Boolean operations become bitwise operations on binary data. |
| Compound conditions in IF/ELSE | Predicate logic and formal specification | Software verification uses Boolean predicates to prove that programs meet their specifications before execution. |
| De Morgan's Laws | Circuit optimization (NAND/NOR) | Hardware engineers apply De Morgan's Laws to transform circuits into equivalent forms that use fewer transistors. |
| Boolean filtering (REPEAT UNTIL) | SQL WHERE clauses and search engines | Databases use Boolean expressions to filter millions of records; search engines combine AND/OR/NOT to refine queries. |
Mastering Boolean expressions at the AP level gives you a firm foundation for any further study in computer science. Whether you move into data structures, systems programming, or machine learning, the ability to construct, evaluate, and simplify Boolean logic will remain one of the most frequently used skills in your toolkit.
NOT (true AND false)?x ← 7
y ← 15
What does the expression (x < 10) AND (y > 20) evaluate to?NOT (A OR B)? Select two answers.avg holds the student's numerical average, and the variable missing holds the number of missing assignments.
(a) Write a Boolean expression in AP pseudocode that evaluates to true when the student qualifies for an "A".
(b) Using De Morgan's Laws, write an equivalent expression for when the student does NOT qualify for an "A" (without using the NOT operator on the entire original expression).
(c) Explain why your answer to part (b) is equivalent to NOT applied to your answer in part (a).eligible ← (trips ≥ 50 AND rating ≥ 4.8) OR (trips ≥ 100)
(a) For the values trips ← 75 and rating ← 4.5, evaluate the expression step by step and state the final value of eligible.
(b) Identify ALL combinations of trips and rating values that make eligible evaluate to true. Describe the conditions in plain English.
(c) The company decides to add a third condition: the driver must also have zero safety violations (violations = 0) to be eligible, regardless of trips or rating. Rewrite the full Boolean expression to incorporate this new requirement.
(d) Explain why the placement of parentheses in your answer to part (c) is critical. Show what would happen if the parentheses were removed or placed incorrectly.Boolean expressions are the decision-making currency of every program, evaluating to exactly true or false. They are constructed from relational operators (=, ≠, <, >, ≤, ≥) that compare values, and logical operators (AND, OR, NOT) that combine or invert Boolean values. The operator precedence rule—NOT first, then AND, then OR—determines the evaluation order when parentheses are absent, and using explicit parentheses is always recommended for clarity.
De Morgan's Laws provide the essential tool for transforming negated compound expressions: distribute the NOT by negating each operand and swapping AND ↔ OR. On the AP CSP exam, you will encounter Boolean expressions in conditionals, loops, and algorithm tracing questions. Master the truth tables, practice step-by-step evaluation, and always verify your reasoning by substituting concrete values.