UNIT 2: SELECTION AND ITERATION · TOPIC 2.3
2.3 if Statements
The if statement runs a block when a condition is true; adding else gives an alternative. Braces and semicolons cause more errors here than logic does.
What you need to know
if (condition) { statements }— runs the block only when the condition is true. This is one-way selection.if (condition) { A } else { B }— runs exactly one of A or B. Two-way selection.- The condition must be a boolean.
if (x)where x is an int doesn't compile.if (x = 5)doesn't compile either (assignment isn't a boolean). - Braces are optional for a single statement, but omitting them is a classic trap: only the first statement is controlled by the if. Always use braces.
- A semicolon right after the condition —
if (x > 0);— ends the if with an empty body, so the next block always runs. - After the if/else completes, execution continues with the next statement regardless of which branch ran.
Worked example
int score = 72;
if (score >= 70)
System.out.println("Pass");
System.out.println("Congrats"); // NOT part of the if — always prints
if (score > 90)
{
System.out.println("Honors");
}
else
{
System.out.println("Standard");
}
// Output: Pass Congrats Standard
Exam tip: Indentation means nothing to Java. When braces are missing, exactly one statement belongs to the if. The exam writes misleadingly-indented code on purpose; read braces, not spacing.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- The if body without braces is exactly one statement. A second line, no matter how it's indented, is outside the if. The exam writes deliberately misleading indentation to test this. Always mentally add the braces.
if (cond);— a semicolon right after the condition — is an empty if. The block that follows runs unconditionally. It compiles fine, which is why it's dangerous.- The
elsepairs with the nearest unmatchedif. With braces, no ambiguity. Without, the dangling-else rule applies (2.4). - A condition that's a constant —
if (true)— always runs. Rarely written on purpose, but appears in "which of these is redundant" questions. - The if statement itself has no value.
x = if (…)isn't Java. To choose between values, use if/else with two assignments. - Two-way selection covers every input: one branch or the other. One-way selection has a "do nothing" case. That difference is what "how many lines are printed" questions probe.
Mistakes that cost points
- Attaching the second unbraced statement to the if. Only the first belongs. The indentation lies.
- Missing the stray semicolon.
if (x > 5);means the next block always runs. - Using a non-boolean condition.
if (count)doesn't compile. Java isn't C.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 What is printed by the following code?
int n = 4;
if (n > 5)
System.out.print("A");
System.out.print("B");
System.out.print("C");Show answer
Answer: B. Without braces, only print("A") is controlled by the if. "B" and "C" always print.
Q2 Which of the following will not compile?
Show answer
Answer: C. The condition must be a boolean; an int isn't. Option D is legal, just redundant.
Key vocabulary
- One-way selection
- an if statement with no else
- Two-way selection
- an if statement with an else