Boolean Expressions
Ben Davis
6 min read
Listen to this study note
Study Guide Overview
This study guide covers Boolean logic for the AP Computer Science Principles exam. It focuses on Boolean variables, relational operators (==, !=, >, <, >=, <=), logical operators (NOT, AND, OR), and truth tables. It also explains common mistakes, operator precedence, and provides practice questions involving conditional statements and combining Boolean logic with other programming concepts.
#AP Computer Science Principles: Boolean Logic - The Night Before
Hey! Let's get you prepped for the exam. We're going to break down Boolean logic, which is super important for both multiple-choice and free-response questions. Think of this as your quick-scan guide for tonight. Let's dive in!
#Boolean Variables and Relational Operators
#What are Boolean Variables?
Boolean variables are like light switches: they're either true or false. That's it! No in-between. They are fundamental for decision-making in code.
Boolean values are the backbone of conditional statements and loops. Understanding them is crucial for controlling the flow of your programs.
#Relational Operators
These are the tools we use to create Boolean values. They compare two values and return either true or false based on the relationship. Here’s a quick rundown:
| Operator | Meaning | Example | Result (if x=10, y=5) |
|---|---|---|---|
== | Equal to | x == y | false |
!= | Not equal to | x != y | true |
> | Greater than | x > y | true |
< | Less than | x < y | false |
>= | Greater than or equal to | x >= y | true |
<= | Less than or equal to | x <= y | false |

Example:
python
y = 5
x = 55
print(x > y) # Output: True
Pay close attention to the difference between = (assignment) and == (equality). A common mistake is using the wrong one, which can lead to unexpected results.
#Logical Operators: NOT, AND, OR
These operators let us combine or modify Boolean expressions. Think of them as the glue that holds complex conditions together.
#NOT Operator
The NOT operator flips the Boolean value. true becomes false, and false becomes true. It's like the opposite button.

Example:
python
y = 5
print(not y >= 5) # Output: False
NOT has the highest precedence among the logical operators. It's evaluated first.
#AND Operator
The AND operator combines two conditions. It only returns true if both conditions are true. If even one is false, the whole thing is false.

Example:
python
y = 5
x = 55
print(y >= 5 and x <= 44) # Output: False
Think of AND like a strict parent: both conditions must be met to get a true result.
#OR Operator
The OR operator also combines two conditions. It returns true if at least one condition is true. It only returns false if both conditions are false.

Example:
python
y = 5
x = 55
print(y >= 5 or x <= 44) # Output: True
Think of OR like a lenient parent: if either condition is met, you get a true result.
#
Truth Tables
Truth tables are a great way to visualize how logical operators work:
| A | B | NOT A | A AND B | A OR B |
|---|---|---|---|---|
| true | true | false | true | true |
| true | false | false | false | true |
| false | true | true | false | true |
| false | false | true | false | false |
Memorize these! They can save you time on the exam.
#
Common Pitfalls
- Mixing up
=and==: Remember,=is for assignment, and==is for equality. - Incorrect operator precedence:
NOTis evaluated beforeANDandOR. - Forgetting parentheses: Use parentheses to clarify the order of operations in complex expressions.
- Misinterpreting complex conditions: Break down complex expressions into smaller parts to evaluate them correctly.
#Final Exam Focus
Okay, here’s what to really focus on:
- Boolean Logic: Understand how relational and logical operators evaluate to
trueorfalse. This is foundational for many other concepts. - Truth Tables: Be able to quickly evaluate logical expressions using truth tables.
- Operator Precedence: Remember the order:
NOT,AND, thenOR. - Conditional Statements: Practice writing
if,else if, andelsestatements using Boolean conditions. This is a major application of Boolean logic. - Combining Concepts: Many questions will combine Boolean logic with other topics like loops and lists. Be prepared to apply your knowledge in different contexts.
#
Practice Question
Practice Questions
#Multiple Choice
-
Given
x = 10andy = 5, what is the result of the expression(x > 5) AND (y < 10)? a)trueb)falsec) Error d) None of the above -
What is the result of the expression
NOT (true OR false)? a)trueb)falsec) Error d) None of the above -
Which of the following expressions will evaluate to
trueifa = 7andb = 3? a)(a < 5) OR (b > 5)b)(a > 5) AND (b < 5)c)(a == 7) AND (b == 3)d)(a != 7) OR (b != 3)
#Free Response
Scenario: A program needs to check if a student is eligible for a scholarship. The student must have a GPA of 3.5 or higher AND a SAT score of 1200 or higher. Write a code segment (in pseudocode or Python) that takes GPA and SAT score as input and prints "Eligible" if the student meets the criteria, or "Not Eligible" otherwise.
Scoring:
- Correct input: 1 point
- Correct Boolean expression: 2 points
- Correct conditional statement: 2 points
- Correct output: 1 point
Example Solution (Python):
python
gpa = float(input("Enter GPA: "))
sat_score = int(input("Enter SAT score: "))
if (gpa >= 3.5 and sat_score >= 1200):
print("Eligible")
else:
print("Not Eligible")
Let's get this exam done! You've got this! 💪
Continue your learning journey

How are we doing?
Give us your feedback and let us know how we can improve





