Boolean expressions are a key part of programming in AP Computer Science Principles. They are used to make decisions in code by evaluating to either true or false. This chapter covers Boolean variables, which can only hold true or false values, and explains how to use relational operators to compare values. It also discusses logical operators—NOT, AND, and OR—which help combine or change Boolean conditions. Understanding these concepts is important for controlling how programs make choices based on different situations.
When you use a relational operator to compare values, the result is a Boolean value (true or false).
This code prints True because 55 is greater than 5.
Besides relational operators, Boolean values work with logical operators: NOT, AND, and OR. These allow you to combine or manipulate conditions, unlike relational operators, which focus on single comparisons.
Logical operators process either Boolean expressions or individual Boolean values.
The NOT operator flips the Boolean result. If the condition is true, NOT makes it false, and vice versa.
Example in Python:
y = 5
x = 55
print(not y >= 5)
# Output: False
Here, y >= 5 is true because y equals 5. The NOT operator reverses this to false.
In pseudocode, the NOT operator is written as:
NOT condition
The AND operator evaluates two conditions and returns true only if both conditions are true.
Example in Python:
y = 5
x = 55
print(y >= 5 and x <= 44)
# Output: False
This prints false because, while y >= 5 is true, x <= 44 is false. Both must be true for AND to return true.
In pseudocode, the AND operator is written as:
condition1 AND condition2
The OR operator also evaluates two conditions but returns true if at least one condition is true.
Example in Python:
y = 5
x = 55
print(y >= 5 or x <= 44)
# Output: True
This prints true because y >= 5 is true, even though x <= 44 is false. Only one condition needs to be true for OR.
In pseudocode, the OR operator is written as:
condition1 OR condition2
1. What are Boolean variables and how are they used in programming? | ![]() |
2. Can you explain the NOT operator in Boolean logic? | ![]() |
3. How does the AND operator work in Boolean expressions? | ![]() |
4. What is the function of the OR operator in Boolean logic? | ![]() |
5. How can Boolean expressions be useful in real-life applications? | ![]() |