01 / 21

Boolean
Expressions

Master the logic that powers every decision a computer makes: TRUE, FALSE, and the operators that combine them.

AP Computer Science Principles

What is a Boolean?

A Boolean is a data type that holds exactly one of two possible values: TRUE or FALSE. Named after mathematician George Boole, Booleans are the foundation of all computer logic and decision-making.

TRUE
Represents "yes", "on", or the numeric value \(1\). A condition that holds.
FALSE
Represents "no", "off", or the numeric value \(0\). A condition that does not hold.

What is a Boolean Expression?

A Boolean expression is any expression that evaluates to either TRUE or FALSE. They are built using relational operators (comparisons) and logical operators (AND, OR, NOT).

// Simple Boolean expressions
\(age \ge 16\)           // evaluates to TRUE or FALSE
\(score == 100\)        // equality check
\(name \ne \text{"Alice"}\)    // inequality check
true               // literal Boolean value

Why Booleans Matter

Boolean expressions are the gatekeepers of program flow. Without them, programs would execute every line in order with no decisions, no repetition, and no intelligence.

Selection
if / else statements use Booleans to choose which code block runs.
Iteration
while / for loops use Booleans to decide when to stop repeating.
Filtering
Search algorithms use Booleans to include or exclude data items.

Relational Operators

Relational operators compare two values and produce a Boolean result. These are the building blocks of Boolean expressions.

Operator Meaning Example Result
\(==\)Equal to\(5 == 5\)TRUE
\(\ne\)Not equal to\(5 \ne 3\)TRUE
\(>\)Greater than\(7 > 10\)FALSE
\(<\)Less than\(3 < 8\)TRUE
\(\ge\)Greater or equal\(5 \ge 5\)TRUE
\(\le\)Less or equal\(9 \le 4\)FALSE

Logical Operators: AND, OR, NOT

Logical operators combine or modify Boolean values. They let you build compound Boolean expressions from simpler ones.

AND
Returns TRUE only when both operands are TRUE. Otherwise FALSE.
OR
Returns TRUE when at least one operand is TRUE. FALSE only when both are FALSE.
NOT
Flips a single Boolean: TRUE becomes FALSE, and vice versa.

Truth Tables: AND and OR

AND A B
\(A \text{ AND } B\)
T T T T F F F T F F F F OR A B
\(A \text{ OR } B\)
T T T T F T F T T F F F AND: both must be TRUE   |   OR: at least one must be TRUE

The NOT Operator

NOT is a unary operator: it takes a single Boolean value and returns its opposite. It is the simplest logical operator but one of the most powerful for controlling program flow.

A
\(\text{NOT } A\)
TRUE FALSE FALSE TRUE NOT flips the value
if (NOT raining) {
    goOutside()
}
// Goes outside when raining is FALSE

Assignment vs Comparison

One of the most important distinctions in programming: the difference between assigning a value and comparing values. Confusing them is a common source of bugs.

Feature \(=\) (Assignment) \(==\) (Comparison)
Purpose Stores a value in a variable Checks if two values are equal
Returns The stored value TRUE or FALSE
Example \(x = 5\) \(x == 5\)
Used in Variable declarations if statements, loops

Boolean Order of Operations

Just like arithmetic has PEMDAS, Boolean logic has its own precedence rules. Understanding these prevents unexpected results in compound expressions.

1
Parentheses are evaluated first, just like in math. Use them to make your intent clear.
2
NOT is evaluated next. It has the highest precedence among logical operators.
3
AND is evaluated before OR. Think of AND like multiplication and OR like addition.
4
OR is evaluated last. It has the lowest precedence among logical operators.

How to Evaluate a Boolean Expression

1
Substitute all variable values into the expression.
2
Evaluate parentheses first, working from the innermost outward.
3
Evaluate relational operators (comparisons like \(>\), \(<\), \(==\)) to get Boolean values.
4
Apply NOT operators, then AND, then OR in that order.
5
Arrive at a single TRUE or FALSE result.

Example 1: Simple Expression

1
Substitute: \((7 > 5) \text{ AND } (3 < 10)\)
2
Evaluate comparisons: TRUE AND TRUE
3
Apply AND: Both are TRUE, so result is TRUE
\((7 > 5)\) AND \((3 < 10)\)
\(\text{TRUE AND TRUE}\)
TRUE

Example 2: Compound Expression

1
Substitute: \((4 > 2) \text{ OR } (10 < 4) \text{ AND } (2 == 2)\)
2
Evaluate comparisons: TRUE OR FALSE AND TRUE
3
AND first (precedence!): \(\text{FALSE AND TRUE} = \)FALSE
4
Then OR: \(\text{TRUE OR FALSE} = \)TRUE

Example 3: Using NOT

1
Substitute: \(\text{NOT } (85 < 70) \text{ AND }\) TRUE
2
Parentheses first: \(\text{NOT } (\)FALSE\() \text{ AND }\) TRUE
3
Apply NOT: TRUE AND TRUE
4
Apply AND: TRUE

Common Mistakes

✕ Using \(=\) instead of \(==\)
Writing \(\text{if } (x = 5)\) assigns 5 to x instead of comparing. Use \(==\) for comparison.
✕ Ignoring Precedence
Forgetting that AND evaluates before OR. Use parentheses to make intent explicit.
✕ Confusing NOT Scope
\(\text{NOT}(A \text{ AND } B)\) is not the same as \((\text{NOT } A) \text{ AND } (\text{NOT } B)\). NOT distributes via De Morgan's Laws.

Summary: Boolean Logic at a Glance

Concept Symbol Returns TRUE when
AND AND Both operands are TRUE
OR OR At least one operand is TRUE
NOT NOT The operand is FALSE
Equal \(==\) Left equals right
Not equal \(\ne\) Left does not equal right
Greater than \(>\) Left is larger than right
Less than \(<\) Left is smaller than right

Question 1 of 5

If \(x = 8\) and \(y = 3\), what does the expression \((x > 5) \text{ AND } (y > 5)\) evaluate to?

Question 2 of 5

What is the value of \(\text{NOT } (\text{TRUE AND FALSE})\)?

Question 3 of 5

Given \(a = 5\), \(b = 12\), evaluate:
\((a < 10) \text{ OR } (b < 10) \text{ AND } (a == 5)\)

Question 4 of 5

Which of the following is equivalent to \(\text{NOT } (A \text{ OR } B)\) according to De Morgan's Laws?

Question 5 of 5

What will the code display?