Introduction to Making Decisions
In our previous lessons, we learned how to tell a computer to perform tasks one after another in a straight line. But life isn't always a straight line! Imagine a self-driving car: it needs to decide whether to stop if the light is red or go if the light is green. In programming, we call this Selection. It is how we give our code a "brain" to make choices based on specific conditions.
In this chapter, we will focus on the most fundamental tool for selection: the if statement. Don't worry if logic feels a bit like a puzzle at first—once you see the patterns, you'll be able to control the flow of your programs like a pro!
Note: Before we dive in, remember that every "decision" in Java relies on a boolean expression—a statement that is either \(true\) or \(false\).
The One-Way "if" Statement
The simplest form of selection is the one-way if statement. Think of this like a "bonus" step. If a condition is met, the computer does something extra. If the condition is not met, the computer simply ignores that code and moves on.
How it looks in code:
\(if (condition)\)
\({\)
\(// code to execute if condition is true\)
\(}\)
How it works:
1. The computer evaluates the expression inside the parentheses \( ( ) \).
2. If the expression is \(true\), the code inside the curly braces \( \{ \} \) runs.
3. If the expression is \(false\), the computer skips everything inside the braces and continues with the rest of the program.
Example: Imagine a game where a player gets a message only if they have a high score.
\(if (score > 100)\)
\({\)
\(System.out.println("New High Score!");\)
\(}\)
Quick Tip: In the AP Java subset, if you only have one line of code after your \(if\), the curly braces are actually optional. However, it is a best practice to always use them to avoid mistakes! If you leave them out, only the very next line is controlled by the \(if\).
Key Takeaway: Use a simple \(if\) when you want something to happen only under a specific condition, but otherwise want the program to behave normally.
The Two-Way "if-else" Statement
Sometimes you need to choose between two different paths. This is the if-else statement. Think of it like a fork in the road: you must go either left or right; you can't do both, and you can't do neither.
How it looks in code:
\(if (condition)\)
\({\)
\(// runs if condition is true\)
\(}\)
\(else\)
\({\)
\(// runs if condition is false\)
\(}\)
Analogy: Imagine checking the weather. If it is raining, you take an umbrella. Else (otherwise), you wear sunglasses.
Common Mistake: Never put a condition in parentheses after the word \(else\). The \(else\) keyword is a "catch-all" for whenever the \(if\) condition fails.
Key Takeaway: The \(if-else\) structure ensures that exactly one of the two code blocks will execute.
Multi-Way Selection: The "if-else-if" Chain
What if you have more than two choices? For example, giving grades: An \(A\), \(B\), \(C\), \(D\), or \(F\). For this, we use an if-else-if structure.
How it works:
The computer checks conditions from top to bottom. As soon as it finds one that is \(true\), it runs that block of code and then skips the rest of the entire chain.
Example:
\(if (score >= 90)\)
\({\)
\(grade = "A";\)
\(}\)
\(else \ if (score >= 80)\)
\({\)
\(grade = "B";\)
\(}\)
\(else\)
\({\)
\(grade = "F";\)
\(}\)
Did you know? The order of your conditions matters! If we checked \(score >= 80\) before checking \(score >= 90\), a student with a \(95\) would get a \(B\) because the computer stops at the first true statement it finds.
Key Takeaway: An \(if-else-if\) chain is perfect for mutually exclusive categories where only one option should be picked.
Summary Checklist & Common Pitfalls
Quick Review:
- if: One-way street. Do it or skip it.
- if-else: Two-way street. Choice A or Choice B.
- if-else-if: Multi-way street. Pick the first one that fits.
Watch out for these "Gotchas":
- The Semicolon Error: Never put a semicolon directly after the \(if\) condition, like this: \(if (x > 0);\). This creates an "empty" statement, and your code inside the braces will run every single time regardless of the condition!
- Assignment vs. Comparison: Use \(==\) to compare values, not \(=\). Inside an \(if\) statement, \(x = 5\) will cause a compiler error because it's trying to change the value, not check it.
- Indentation: Java doesn't actually care about spaces or tabs, but humans do! Always indent the code inside your braces so you can easily see which lines belong to which "choice."
In the next chapter, we will look at Nested if Statements, which is what happens when we put one "choice" inside another choice!