Introduction: Making Choices with Logic
In computer science, programs often need to make decisions. Should the game end? Is the player's score high enough for a bonus? Does the user have the right password? These decisions are made using selection. At the heart of selection is the Boolean Expression.
Think of a boolean expression as a "Yes or No" question that you ask the computer. In Java, there are no "maybe" answers—only true or false. Mastering these expressions is the first step toward writing smart, responsive code. Don't worry if it feels abstract at first; once you see the symbols, it’s just like basic math!
1. The Boolean Data Type
Before we compare things, we need to remember the boolean primitive type. A boolean variable can only hold one of two values: true or false. These are reserved keywords in Java.
Example:
boolean \(isGameOver = false;\)
boolean \(hasPassedClass = true;\)
Quick Note: Unlike some other languages, Java does not use numbers like \(0\) or \(1\) to represent true or false. It strictly uses the words true and false.
2. Relational Operators: The "Comparison Tools"
To create a boolean expression, we usually compare two values using relational operators. These operators look at the values on the left and right and decide if the relationship is true or false.
Here are the six relational operators you need to know for the AP exam:
1. Equality \( (==) \): Returns true if the two values are exactly the same.
2. Inequality \( (!=) \): Returns true if the two values are not the same.
3. Less Than \( (<) \): Returns true if the left value is smaller than the right.
4. Greater Than \( (>) \): Returns true if the left value is larger than the right.
5. Less Than or Equal To \( (<=) \): Returns true if the left is smaller than or equal to the right.
6. Greater Than or Equal To \( (>=) \): Returns true if the left is larger than or equal to the right.
Key Takeaway:
Every time you use one of these operators, the "result" of that line of code is a single boolean value (\(true\) or \(false\)).
3. Evaluating Expressions
Let's look at some examples of how Java evaluates these expressions. Assume we have two variables: int \(a = 5;\) and int \(b = 10;\).
- \(a == b\) evaluates to false (because \(5\) is not equal to \(10\)).
- \(a != b\) evaluates to true (because \(5\) is indeed not equal to \(10\)).
- \(a < b\) evaluates to true (because \(5\) is less than \(10\)).
- \(a >= 7\) evaluates to false (because \(5\) is not greater than or equal to \(7\)).
4. Comparing Doubles
You can also compare double values (decimal numbers). However, be careful! Because of how computers store decimals, sometimes two numbers that look equal might be off by a tiny fraction. While the AP Exam usually focuses on simple comparisons, always remember that \(5.0 == 5\) is true because Java will promote the integer to a double before comparing.
5. Common Pitfalls (The "Oops" Moments)
Even experienced programmers make these mistakes. Keep an eye out for them!
The Single Equals vs. Double Equals:
- A single equals sign \( (=) \) is the assignment operator. It is used to give a variable a value (e.g., \(x = 10;\)).
- A double equals sign \( (==) \) is the comparison operator. It is used to ask if two things are equal.
Memory Trick: Think of \( == \) as "Is it equal?" and \( = \) as "Make it equal."
The Inequality "Bang":
- In programming, the exclamation point \( ! \) is often called a "bang." It represents "NOT." So, \( != \) literally means "not equal."
6. Real-World Analogy
Imagine you are a bouncer at a movie theater. Your rule is: "You must be at least 13 years old to see this movie."
If a kid comes up, you ask: \(age >= 13\)?
- If the kid is 15: \(15 >= 13\) is true (Enter!).
- If the kid is 12: \(12 >= 13\) is false (Sorry, come back next year!).
Quick Review Box
- Boolean values: Only \(true\) or \(false\).
- Relational operators: \(==, !=, <, >, <=, >=\).
- Result: A boolean expression always simplifies to a single boolean value.
- Comparison: Use \(==\) to compare, not \(=\).
Note: This chapter covers basic boolean expressions. To see how to combine multiple expressions (like "Are you 13 AND do you have a ticket?"), see the upcoming chapter on Compound Boolean Expressions.