Topic 2.3 Notes – if Statements
1. How If Statements Control Program Flow
By default, Java runs code sequentially from top to bottom. An if statement interrupts that flow.
It checks a Boolean expression that evaluates to true or false. Based on that result, Java either executes a block of code or skips it.
Here’s the idea visually:
Flowchart of an if-else selection statement
What’s happening under the hood:
- Evaluate the Boolean condition.
- If it’s
true, run the associated block. - If it’s
false, skip that block (or run theelseblock, if there is one). - Continue with the next statement after the block.
This is called a selection statement because the program selects which path to follow.
2. Types of Selection Statements
There are two forms you need to know for AP CSA.
a. One-way selection
Used when something should happen only if a condition is true.
int temperature = 85;
if (temperature > 80) {
System.out.println("It's hot outside.");
}
System.out.println("Check complete.");
If temperature > 80 is:
true→ the message printsfalse→ nothing happens inside the block
Important: the program always continues after the block.
This shows up a lot in tracing problems where a variable only changes under certain conditions. If the condition is false, that change never happens.
b. Two-way selection
Used when you want one action if true and a different action if false.
int score = 72;
if (score >= 70) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
Exactly one branch runs.
Here’s the structure visually:
If-else flowchart structure
The program reaches a condition, follows either the True path or the False path, and then both paths rejoin before continuing.
Key distinction:
- One-way → maybe something runs
- Two-way → something always runs
On quizzes, students often assume both blocks might run. That never happens in an if-else.
3. How Conditions Are Evaluated
The condition must be a Boolean expression.
Common forms:
- Numeric comparisons:
>,<,>=,<=,==,!= - Boolean variables:
if (isOpen) - String comparison:
Never useif (name.equals("Alex"))==for Strings. - Compound conditions:
&&(and)||(or)!(not)
Nested if statements are allowed:
if (score >= 60) {
if (score >= 90) {
System.out.println("Excellent");
}
}
The inner if only runs if the outer condition is true. When tracing, evaluate one condition at a time. Don’t skip steps.
4. Common Mistakes That Lose Points
Missing curly braces
Without braces, only the next line belongs to the if.
if (score > 90)
System.out.println("A");
System.out.println("Great job"); // always runs
On FRQs, always use braces. It avoids logic errors and graders like clarity.
Using = instead of ==
if (x = 5) // compile error
=assigns==compares
This is a frequent multiple-choice trap.
Null issues with objects
Calling a method on null causes a crash.
Safer pattern:
if (name != null && name.equals("Mia")) {
System.out.println("Hello Mia");
}
Because of short-circuit evaluation, name.equals(...) only runs if name != null is true.
Separate if statements vs if-else
These are different:
if (score >= 80)
System.out.println("B or higher");
if (score >= 90)
System.out.println("A");
If score is 95, both lines print.
That’s correct behavior, but students often expect only one to run. Use if-else if you want mutual exclusivity.
5. How This Shows Up on Tests
In multiple-choice, you’ll trace nested or compound conditions and determine:
- What prints
- Final variable values
- Which blocks execute
Slow down and evaluate each condition in order. Most wrong answers come from assuming a block runs without checking its condition carefully.
On FRQs, you’ll write methods that:
- Validate input
- Return different values based on conditions
- Update variables depending on criteria
Clear logic and correct Boolean expressions matter more than fancy structure.