Home

Tutoring

Subjects

Live Classes

Study Coach

Essay Review

On-Demand Courses

Colleges

Games

Opening subject page...

Loading your content

  1. AP Computer Science Principles
  2. Boolean Expressions

TRUE AND FALSENOT (A OR B)x > 5 AND x < 10IF condition THEN
AP COMPUTER SCIENCE PRINCIPLES • ALGORITHMS AND PROGRAMMING

Boolean Expressions

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

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
a 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
A 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
A 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
NOT 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.

Classification of Boolean expression types from simple to complex
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.
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

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

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

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

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.

Common Boolean expression mistakes and 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.
✦ 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.

Connections from AP CSP Boolean concepts to advanced computer science
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.

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

PROBLEM 1 — CONCEPTUAL
Which of the following best describes the result of the Boolean expression NOT (true AND false)?
PROBLEM 2 — BASIC CALCULATION
Consider the following code segment: x ← 7 y ← 15 What does the expression (x < 10) AND (y > 20) evaluate to?
PROBLEM 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.
PROBLEM 4 — APPLIED
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).
PROBLEM 5 — CRITICAL THINKING
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.
SUMMARY

Summary — Boolean Expressions

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