Introduction to Boolean Expressions
Welcome! Today we are diving into the "brain" of computer logic: Boolean Expressions. Have you ever wondered how a video game knows when you've run out of lives, or how a social media app knows you’ve entered the correct password? It all comes down to true-or-false questions.
In computer science, a Boolean value is a data type that has only two possible values: true or false. A Boolean expression is simply a piece of code that the computer evaluates to see if it is true or false. Think of it like a light switch—it’s either ON or OFF, with no middle ground!
1. Relational Operators: Comparing Values
The most common way to create a Boolean expression is by comparing two values using relational operators. These are the "math-like" symbols you likely recognize from algebra, but in programming, they always result in a true or false answer.
According to the AP CSP Exam Reference Sheet, here are the symbols you need to know:
- \( a = b \): Evaluates to true if \( a \) is equal to \( b \).
- \( a \neq b \): Evaluates to true if \( a \) is not equal to \( b \).
- \( a > b \): Evaluates to true if \( a \) is greater than \( b \).
- \( a < b \): Evaluates to true if \( a \) is less than \( b \).
- \( a \geq b \): Evaluates to true if \( a \) is greater than or equal to \( b \).
- \( a \leq b \): Evaluates to true if \( a \) is less than or equal to \( b \).
Examples in Action:
If we have a variable \( age \leftarrow 16 \):
\( age \geq 18 \) evaluates to false.
\( age < 21 \) evaluates to true.
\( age \neq 16 \) evaluates to false.
Quick Tip: Don't confuse the assignment operator (\( \leftarrow \)) with the equality operator (\( = \)). The arrow sets a value, while the equals sign asks if two things are the same.
2. Logical Operators: NOT, AND, and OR
Sometimes, a single comparison isn't enough. What if you need to check if a user's age is over 13 AND they have parental permission? We use Logical (Boolean) Operators to combine or modify expressions.
NOT
The NOT operator reverses the value of a Boolean expression. If something was true, NOT makes it false. If it was false, NOT makes it true.
Notation: \( \text{NOT condition} \)
Analogy: If you say, "I am NOT hungry," you are stating the opposite of your hunger status.
AND
The AND operator is very strict. The entire expression is only true if both parts are true. If even one part is false, the whole thing is false.
Notation: \( \text{condition1 AND condition2} \)
Analogy: To ride a roller coaster, you must be tall enough AND have a ticket. If you are tall but have no ticket, you can't ride (false).
OR
The OR operator is more relaxed. The expression is true if at least one of the parts is true. It is also true if both parts are true!
Notation: \( \text{condition1 OR condition2} \)
Analogy: You can go to the party if you are invited OR if you are the host’s best friend. If either is true, you're going!
The "Truth Table" Summary:
\( \text{true AND true} \rightarrow \text{true} \)
\( \text{true AND false} \rightarrow \text{false} \)
\( \text{true OR false} \rightarrow \text{true} \)
\( \text{false OR false} \rightarrow \text{false} \)
3. Evaluating Complex Expressions
Don't worry if expressions look long and scary! You can solve them step-by-step just like a math problem. Use parentheses to group parts together, as the computer evaluates the inside of parentheses first.
Step-by-Step Example:
Let's evaluate this expression where \( x \leftarrow 5 \) and \( y \leftarrow 10 \):
\( \text{NOT } (x > 2) \text{ AND } (y = 10) \)
- Evaluate the first parenthesis: \( (5 > 2) \) is true.
- Evaluate the second parenthesis: \( (10 = 10) \) is true.
- Now the expression looks like this: \( \text{NOT (true) AND (true)} \).
- Apply the NOT: \( \text{NOT true} \) becomes false.
- Final check: \( \text{false AND true} \) evaluates to false.
Key Takeaway: Always break big expressions into smaller "True/False" chunks before applying the AND/OR/NOT logic.
4. Common Mistakes to Avoid
- The Inclusive OR: In everyday English, "OR" sometimes means "one or the other, but not both" (like "Do you want cake or pie?"). In Computer Science, OR is inclusive. If both conditions are true, the OR expression is true.
- Order of Operations: Just like math has PEMDAS, Booleans have an order too. Generally, parentheses happen first, then NOT, then AND, then OR.
- Variable Types: Remember that you can only compare compatible types (like comparing a number to another number).
Quick Review Box
Boolean Value: Either true or false.
Relational Operators: \( =, \neq, >, <, \geq, \leq \)
NOT: Flips the logic.
AND: True only if all parts are true.
OR: True if at least one part is true.
Note: For more on how these expressions are used to make decisions in code, see the upcoming chapter on Conditionals!