AP Computer Science Principles · Unit 3: Algorithms & Programming
3.5 Boolean Expressions
Boolean expressions evaluate to true/false
A Boolean expression evaluates to exactly one of two values: true or false. They're commonly built using relational operators (=, ≠, >, <, ≥, ≤) to compare values, such as `score > 90`. Boolean expressions are used to control program flow, especially in selection (if) and iteration (loop) statements.
Logical operators combine conditions
Logical operators — NOT, AND, OR — combine or invert Boolean expressions. `AND` is true only if both conditions are true; `OR` is true if at least one is true; `NOT` reverses a value. Example: `(age >= 13) AND (age <= 19)` checks if someone is a teenager. These let programs test complex, multi-part conditions.
Boolean expressions drive decisions
In selection statements, the Boolean expression determines which branch executes; in loops, it determines whether iteration continues. Truth tables can be used to trace all possible outcomes of a compound Boolean expression given different input combinations of true/false, helping predict program behavior.
Key terms
- Boolean expression
- An expression that evaluates to exactly one of two values: true or false.
- Relational operator
- Operators like =, ≠, >, <, ≥, ≤ used to compare two values.
- AND operator
- Logical operator that is true only if both connected conditions are true.
- OR operator
- Logical operator that is true if at least one connected condition is true.
- NOT operator
- Logical operator that reverses a Boolean value (true becomes false, and vice versa).
Try an AP-style question
Code
x ← 7 r ← (x > 5) AND (x < 10)
What is r?
- 7
- false
- An error
- true
Show the answer
D. Both are true.
Practice 8 AP-style questions with instant explanations, flashcards and spaced review.
Study 3.5 free in APrep