AP Computer Science Principles • Algorithms and Programming

Boolean Expressions

The logical foundation that enables every decision a computer program makes.

Computer science principles illustration with a network, database, and code file
Inside this lesson 10 sections 5 questions 9 figures

SECTION 1

Historical Context & Motivation

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.

1847 • Boole's First Work

George Boole publishes The Mathematical Analysis of Logic, introducing algebraic notation for logical propositions.

1854 • Laws of Thought

Boole's major treatise formalizes Boolean algebra with AND, OR, and NOT operations on binary values.

1937 • Shannon's Thesis

Claude Shannon proves that Boolean algebra can describe and optimize electrical switching circuits, bridging logic and engineering.

1945 • Von Neumann Architecture

John von Neumann's stored-program computer design relies on Boolean logic gates as its computational core, embedding Boolean expressions into hardware.

1970s–Present • High-Level Programming

Languages like C, Python, and JavaScript make Boolean expressions a first-class feature for conditionals, loops, and data filtering.

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.

SECTION 2

Core Principles & Definitions

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.

1

Boolean Values

Every Boolean expression resolves to exactly one of two values: true or false. There is no third possibility, no "maybe." This binary nature mirrors the 1s and 0s that underlie all digital computation.
2

Relational Operators

Operators such as =, ≠, <, >, ≤, and ≥ compare two values and produce a Boolean result. For example, score ≥ 90 yields true when the variable score is 90 or above.
3

Logical Operators

The three primary logical operators—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.
4

Compound Expressions

Multiple Boolean sub-expressions can be nested using logical operators to form compound expressions such as (age ≥ 18) AND (hasID = true). The order of evaluation matters and can be controlled with parentheses.
5

Short-Circuit Evaluation

Many languages evaluate compound Boolean expressions left to right and stop as soon as the result is determined. For AND, if the first operand is false, the result is false regardless. For OR, if the first is true, the result is true regardless.
✦

KEY TAKEAWAY

Think of a Boolean expression as a yes-or-no question posed to the computer. Just as an airport security gate checks two conditions—"Does this passenger have a valid boarding pass?" AND "Does this passenger have a valid ID?"—a compound Boolean expression combines individual checks into a single gate that either opens (true) or remains closed (false). Every IF statement in your code is essentially a gate controlled by a Boolean expression.

SECTION 3

Visual Explanation — Truth Tables & Logic Gates

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.

Truth Tables for AND, OR, and NOTANDABA AND BfalsefalsefalsefalsetruefalsetruefalsefalsetruetruetrueORABA OR BfalsefalsefalsefalsetruetruetruefalsetruetruetruetrueNOTANOT AfalsetruetruefalseKey Pattern SummaryAND is strictOnly 1 out of 4 rows is trueOR is generous3 out of 4 rows are trueNOT is a flipInverts true ↔ falseGreen = true result • Red = false result
The three truth tables above define the complete behavior of 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.

SECTION 4

How Boolean Expressions Work in Code

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.

Relational Expressions

RELATIONAL COMPARISON

aopb→Booleana op b → Boolean

Where a and b are values of the same type (numbers, strings) and op is one of: =, ≠, <, >, ≤, ≥. The result is always true or false.

Logical Operators

AND OPERATION

AANDB→trueonlyifbothAandBaretrueA AND B → true only if both A and B are true

In the AP pseudocode, this is written as condition1 AND condition2. If condition1 is false, the entire expression is false regardless of condition2.

OR OPERATION

AORB→trueifAistrue,Bistrue,orbotharetrueA OR B → true if A is true, B is true, or both are true

Written as condition1 OR condition2. If condition1 is true, the entire expression is true regardless of condition2.

NOT OPERATION

NOTA→trueifAisfalse;falseifAistrueNOT A → true if A is false; false if A is true

The unary operator NOT inverts the Boolean value of its operand. NOT true yields false, and NOT false yields true.

Operator Precedence

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.

💡

AP EXAM TIP

On the AP CSP exam, you will encounter Boolean expressions in both the AP pseudocode and in block-based code. Remember that the AP reference sheet defines AND, OR, and NOT as the logical operators. Always evaluate the innermost parentheses first, then apply the precedence rules: NOT → AND → OR.

SECTION 5

Types of Boolean Expressions & De Morgan's Laws

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.

De Morgan's Laws — Visual EquivalencesLAW 1NOT (A AND B)⟺(NOT A) OR (NOT B)Negate both sides, flip AND to ORLAW 2NOT (A OR B)⟺(NOT A) AND (NOT B)Negate both sides, flip OR to ANDVerification with Truth TablesABNOT(A AND B)(NOT A) OR (NOT B)NOT(A OR B)(NOT A) AND (NOT B)FFtruetruetruetrueFTtruetruefalsefalseTFtruetruefalsefalseTTfalsefalsefalsefalseColumns with the same color produce identical results — confirming each law.
De Morgan's Laws state that distributing 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 TypeExampleDescription
Simple Relationalx > 10Compares two values using a single relational operator.
Compound (AND)x > 0 AND x < 100Both 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.
NegatedNOT (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.

Classification of Boolean expression types from simple to complex

SECTION 6

Worked Example — Evaluating a Compound Boolean Expression

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.

Evaluating (age ≥ 18 AND hasTicket) OR isVIP

  1. 1

    Step 1 — Identify Variable Values

    From the assignment statements, we know: age = 20, hasTicket = true, and isVIP = false. We substitute these values into the expression.
    (20 ≥ 18 AND true) OR false
  2. 2

    Step 2 — Evaluate the Relational Sub-Expression

    Evaluate the relational comparison 20 ≥ 18 first. Since 20 is greater than or equal to 18, this evaluates to true.
    (true AND true) OR false
  3. 3

    Step 3 — Evaluate the AND Operation (Inside Parentheses)

    Parentheses take highest priority. Inside the parentheses we have true AND true. Both operands are true, so the AND operation yields true.
    true OR false
  4. 4

    Step 4 — Evaluate the OR Operation

    Finally, evaluate true OR false. Since OR requires only one operand to be true, and the left operand is true, the entire expression evaluates to true.
    Final Result: true
💡

STRATEGY NOTE

Always work from the inside out: substitute values, evaluate relational operators first, then resolve expressions within parentheses, and finally apply the remaining logical operators according to precedence (NOT → AND → OR). Writing out intermediate results, as shown above, prevents careless mistakes under timed conditions.

SECTION 7

Common Pitfalls & Comparisons

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.

PitfallIncorrect ExampleCorrect VersionExplanation
Confusing AND / ORx = 5 OR x = 10 when both must be truex ≥ 5 AND x ≤ 10Use AND for ranges; a value cannot be both 5 and 10 simultaneously.
Missing NOT distributionNOT (A AND B) → NOT A AND NOT BNOT (A AND B) → (NOT A) OR (NOT B)De Morgan's Law: you must also swap the operator.
Precedence errorNOT A OR B treated as NOT (A OR B)(NOT A) OR BNOT binds tighter than OR; use parentheses if you intend the alternative.
Redundant comparisonIF (x > 5) = trueIF (x > 5)The relational expression already evaluates to a Boolean; comparing it to true is redundant.

Common Boolean expression mistakes and their corrections

✦

KEY TAKEAWAY

The most common exam mistake is confusing when to use 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.

SECTION 8

Connection to Advanced Topics

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 ConceptAdvanced ExtensionHow 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/ELSEPredicate logic and formal specificationSoftware verification uses Boolean predicates to prove that programs meet their specifications before execution.
De Morgan's LawsCircuit 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 enginesDatabases use Boolean expressions to filter millions of records; search engines combine AND/OR/NOT to refine queries.

Connections from AP CSP Boolean concepts to advanced computer science

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.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

Which of the following best describes the result of the Boolean expression NOT (true AND false)?
  • A. true, because the AND evaluates to false, and NOT false is true.
  • B. false, because NOT is applied to true first, making it false AND false.
  • C. true, because any expression with NOT always evaluates to true.
  • D. false, because the AND evaluates to true, and NOT true is false.
Show answer (A)
Correct Answer: A. First, evaluate the inner expression: true AND false yields false because AND requires both operands to be true. Then, NOT false yields true. Option B is wrong because NOT applies to the entire parenthesized expression, not to the first operand alone. Option C is wrong because NOT does not always produce true—it depends on the input. Option D is wrong because true AND false is false, not true.

Question 2: BASIC CALCULATION

Consider the following code segment: x ← 7 y ← 15 What does the expression (x < 10) AND (y > 20) evaluate to?
  • A. true, because x < 10 is true
  • B. false, because y > 20 is false and AND requires both to be true
  • C. true, because at least one sub-expression is true
  • D. false, because both sub-expressions are false
Show answer (B)
Correct Answer: B. Evaluate each sub-expression: 7 < 10 is true, and 15 > 20 is false. Since AND requires both operands to be true, true AND false evaluates to false. Option A incorrectly ignores the second sub-expression. Option C confuses AND with OR—"at least one" describes OR behavior. Option D is wrong because x < 10 is true, not false.

Question 3: INTERMEDIATE

According to De Morgan's Laws, which TWO of the following expressions are logically equivalent to NOT (A OR B)? Select two answers.
  • A. (NOT A) AND (NOT B)
  • B. (NOT A) OR (NOT B)
  • C. NOT A AND NOT B
  • D. NOT (A AND B)
Show answer (A, C)
Correct Answers: A and C. By De Morgan's second law, NOT (A OR B) is equivalent to (NOT A) AND (NOT B). Option A states this with explicit parentheses, and Option C states the same thing without parentheses—since NOT has higher precedence than AND, NOT A AND NOT B is parsed as (NOT A) AND (NOT B), making them identical. Option B is the result of De Morgan's first law applied to NOT (A AND B), not to NOT (A OR B). Option D is the negation of AND, which is a different expression entirely.

Question 4: APPLIED

3 pts
A school's grading program assigns a letter grade of "A" if a student's average is at least 90 AND the student has no missing assignments. The variable 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).
Show answer
Rubric (3 points): Point 1 — Correct Boolean expression for part (a): The student writes (avg ≥ 90) AND (missing = 0) or an equivalent formulation. Both conditions must be connected with AND, the threshold must be ≥ 90 (not > 90), and missing must be 0. Point 2 — Correct De Morgan's application for part (b): By De Morgan's Law, NOT((avg ≥ 90) AND (missing = 0)) becomes (avg < 90) OR (missing ≠ 0). The student must negate each sub-expression (≥ becomes <, = 0 becomes ≠ 0) and swap AND to OR. Point 3 — Valid explanation for part (c): The student explains that De Morgan's Law guarantees equivalence: distributing NOT over an AND expression requires negating each operand and changing AND to OR. Both expressions produce identical truth values for all possible inputs, which can be verified by constructing a truth table or by recognizing the formal rule.

Question 5: CRITICAL THINKING

4 pts
A ride-sharing app determines whether a driver is eligible for a bonus using the following Boolean expression: 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.
Show answer
Rubric (4 points): Point 1 — Correct step-by-step evaluation for part (a): Step 1: 75 ≥ 50 → true. Step 2: 4.5 ≥ 4.8 → false. Step 3: true AND false → false. Step 4: 75 ≥ 100 → false. Step 5: false OR false → false. Therefore eligible ← false. The driver is NOT eligible for the bonus. Point 2 — Complete identification of eligible conditions for part (b): A driver is eligible if: (1) they have completed at least 50 trips AND have a rating of at least 4.8, OR (2) they have completed at least 100 trips regardless of rating. The second condition (trips ≥ 100) serves as an override—very active drivers qualify even without an exceptional rating. Point 3 — Correct rewritten expression for part (c): Since the violations condition must be true for ANY eligible driver, it should be applied to the entire original expression using AND: eligible ← ((trips ≥ 50 AND rating ≥ 4.8) OR (trips ≥ 100)) AND (violations = 0). The outer parentheses around the original expression ensure that both paths to eligibility require zero violations. Point 4 — Explanation of parentheses importance for part (d): Without proper parentheses—for example, writing (trips ≥ 50 AND rating ≥ 4.8) OR (trips ≥ 100) AND (violations = 0)—the expression would be parsed as (trips ≥ 50 AND rating ≥ 4.8) OR ((trips ≥ 100) AND (violations = 0)) due to AND having higher precedence than OR. This would mean drivers who meet the first condition (50+ trips and 4.8+ rating) could qualify even WITH safety violations, which contradicts the company's intent. Parentheses are essential to ensure the violations check applies universally.

SUMMARY

Summary — Boolean Expressions

Summary

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.

Varsity Tutors • AP Computer Science Principles • Boolean Expressions