Skip to main content

Unit 3 · Topic 3.5

3.5 Boolean Expressions

Boolean expressions are the yes-or-no questions a program asks. This topic covers the relational operators that compare values and the logical operators NOT, AND and OR that combine conditions, which control every decision and loop you'll write.

Key terms

  • Boolean value
  • relational operator
  • NOT
  • AND
  • OR

Boolean values and relational operators

A Boolean value is either true or false, nothing else.

Relational operators compare two values and give a Boolean result. The exam uses = (equal), ≠ (not equal), >, <, ≥ (greater than or equal) and ≤ (less than or equal). For example, 5 > 3 is true and 5 = 3 is false.

Remember that = here is a comparison, not assignment.

Logical operators

Logical operators work on Boolean values (or on expressions that produce them) and give a Boolean result:

  • NOT condition flips it: true becomes false, false becomes true.
  • condition1 AND condition2 is true only if both are true.
  • condition1 OR condition2 is true if at least one is true, including when both are.
aba AND ba OR bNOT a
truetruetruetruefalse
truefalsefalsetruefalse
falsetruefalsetruetrue
falsefalsefalsefalsetrue

Writing conditions for ranges

To check that x is between 1 and 10 inclusive, both things must be true: x ≥ 1 AND x ≤ 10.

To check that x is outside that range: x < 1 OR x > 10. It can't be both too small and too big, so OR is the right choice.

A classic mistake is x > 5 OR x < 10. Every number is either above 5 or below 10 (most are both), so this is always true.

Flipping a condition

The opposite of x > 5 is x ≤ 5, not x < 5. The value 5 itself has to land on one side.

To flip an AND or an OR, flip each part and switch the operator. NOT (a AND b) means the same as NOT a OR NOT b, and NOT (a OR b) means the same as NOT a AND NOT b. For example, "not (sunny and warm)" means "not sunny, or not warm, or both."

Boolean variables and where conditions go

A Boolean expression can be stored in a variable, just like a number. isTeen ← age ≥ 13 AND age ≤ 19 stores true or false, and later code can use isTeen as a condition. A clear Boolean name reads like a yes-or-no question, which makes code easier to follow.

Since a Boolean variable already holds true or false, IF (isTeen) works on its own; writing IF (isTeen = true) does the same thing with extra words.

Boolean expressions are what control IF statements (3.6) and REPEAT UNTIL loops (3.8), so being quick and accurate with them pays off across the whole exam.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Evaluating compound conditions

    What does this display?age ← 16 hasPermit ← true DISPLAY(age ≥ 16 AND hasPermit) DISPLAY(age > 16 OR NOT hasPermit) DISPLAY(NOT (age < 13 OR age > 19))

    Show the solution
    1. Step 1: Line 3: age ≥ 16 is 16 ≥ 16, which is true. hasPermit is true. true AND true is true.
    2. Step 2: Line 4: age > 16 is false (16 isn't greater than 16). NOT hasPermit is false. false OR false is false.
    3. Step 3: Line 5: age < 13 is false; age > 19 is false; false OR false is false. NOT false is true. In words, age is between 13 and 19.

    Answer: true false true

  2. Example 2

    Writing the condition

    A theme park ride allows riders who are at least 48 inches tall, unless they're under 6 years old, who can never ride. Variables height and age hold a rider's height and age. Write a Boolean expression that is true when the rider is allowed on.

    Show the solution
    1. Step 1: Both requirements must hold, so use AND.
    2. Step 2: Height requirement: height ≥ 48 ("at least" includes 48).
    3. Step 3: Age requirement: not under 6, which is age ≥ 6. (Writing NOT (age < 6) also works.)

    Answer: height ≥ 48 AND age ≥ 6

Common mistakes

  • Using OR where both conditions must hold. If a sentence says "and also," or describes a range, you usually need AND.
  • Getting the boundary wrong: "at least 48" is ≥ 48, and the opposite of > 5 is ≤ 5.
  • Thinking OR means "one or the other but not both." On the exam, OR is true when both are true.

On the exam

  • Expect to evaluate compound expressions with given values, and to pick the expression that matches a description. Test each answer choice with a value at the boundary.
  • Questions that ask which expressions are equivalent can be checked with a quick truth table: try every combination of true and false.

Connected topics

Videos

  • AP CSP Topic 3.5 - Boolean Expressions - Explanations and 5 MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • AP CS Principles Exam Review - Booleans and Conditionals

    Flavio KupermanWatch on YouTube (opens in a new tab)

  • AP CSP Reference Sheet - Relational and Boolean Operators

    Chris OzarkaWatch on YouTube (opens in a new tab)

  • CS Principles: Conditionals - Part 3 "And & Or" Operators

    CodeAIWatch on YouTube (opens in a new tab)

  • Evaluating compound boolean expressions | Intro to CS - Python | Khan Academy

    Khan AcademyWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 3.5 Boolean Expressions. Pick an answer to see if you got it, and why.

Question 1 of 4

Consider the Boolean expression (x > 10) OR (x < 5 AND y = 0). For which two pairs of values does the expression evaluate to true? Select two answers.

Select two answers. 0 of 2 chosen

Question 2 of 4

A program should set isTeen to true exactly when the integer age is from 13 to 19, including both 13 and 19. Which expression should be used?

Question 3 of 4

The variables a and b hold Boolean values. Which expression always has the same value as NOT (a AND b)?

Question 4 of 4

What is displayed when the following code segment is run? isRaining ← true hasUmbrella ← false getsWet ← isRaining AND NOT hasUmbrella DISPLAY(getsWet) DISPLAY(NOT (isRaining OR hasUmbrella))

0 of 4 answered