Mastering Conditional Statements in Java
An empty statement is a statement that consists simply of a semicolon and tells the computer to do nothing.
A Semicolon That Changes the Result
Most Java statements do visible or measurable work. They assign a value, call a method, or test a condition. Some of the most damaging bugs in conditional code come from a statement that does none of these things: a lone semicolon. Java treats that semicolon as a real statement, and it is easy to place one without noticing.
What do you think happens?
Read the code below. What does the program print when x is -3?
Reveal answer
positive is printed
The semicolon after the if condition is an empty statement, and it becomes the body of the if. The braces that follow are a separate block, not the body of the if, so they run every time, even though x is negative.
positive- The semicolon after the condition ends the if statement immediately
- The braces form a separate block that is not controlled by the if
The code looks like it should print nothing, because x is not greater than zero. The behavior is determined by where the semicolon sits. To see exactly why, we need to trace what Java does with each part of the line.
Tracing the Stray Semicolon Step by Step
Trace the program one statement at a time. The goal is to track which lines the if statement controls and which lines run regardless of its condition.
Following the Program With x Equal to -3
Trace the program that uses a stray semicolon after the if condition, with x set to -3. Determine what is printed and which statement is controlled by the if.
- 1Step 1: Assign the value
The first line stores -3 in x. Nothing is printed yet.
- 2Step 2: Test the condition
Java evaluates x > 0. Since x is -3, the test is false. A false test would normally skip the if body, but the body here is not the braces.
- 3Step 3: Locate the if body
The semicolon directly after the condition is a complete statement, the empty statement. Because it follows the if condition, it becomes the body of the if. The if's body is now this empty statement, which does nothing.
- 4Step 4: Run the block
The braces with the println call are not part of the if. They are a standalone block that follows the if statement, so Java runs them unconditionally.
- 5Step 5: Read the output
The println call runs and prints positive, even though x is negative.
The program prints positive. The conditional had no effect because its body was the empty statement.
The key observation from this trace is that the if statement did not control anything that mattered. Its condition was tested, but nothing it governed was ever affected by the result. The text that appears to belong to the if is actually outside of it.
What an Empty Statement Is
An empty statement is a statement that consists simply of a semicolon and tells the computer to do nothing.
The definition does not say that the semicolon is a mistake, and it does not say the compiler ignores it. It describes the semicolon as a statement in its own right, whose entire meaning is no action. Because it is a real statement, it can appear anywhere Java expects a statement, even when there is nothing useful for it to do.
Locating the empty statement in a line is the first skill to build. In the line below, the if keyword begins the statement, the parenthesized expression is the condition, and the lone semicolon at the end is the empty statement. That semicolon is the body of the if, which splits the line into an if header and its body.
Why Java Accepts a Lone Semicolon
Java programmers normally write an if statement with a block in braces and then move on, with no semicolon after the closing brace. The source points out a case that looks unusual: a semicolon after a closing brace is legal. In that case, the trailing semicolon is an empty statement that follows the if statement and contributes no action.
5- The if statement ends at the closing brace of its block
- The trailing semicolon is a separate empty statement after the if
- Removing that semicolon leaves the meaning of the program unchanged
How a Semicolon Changes Control Flow
When a semicolon is placed directly after an if condition, it becomes the body of that if. This is the rule that turns a harmless trailing semicolon into a serious bug. The code that was meant to run conditionally now sits outside the if, and it runs every time.
if (x > 0) {
System.out.println("positive");
}The println call is the body of the if and runs only when x is greater than zero.
if (x > 0);
{
System.out.println("positive");
}The empty statement is the body of the if. The braces run on every pass, regardless of x.
The diagram shows the consequence. Whether the condition is true or false, control reaches the block. The condition still gets evaluated, but its result has no effect on the block, because the only statement the if controls is the empty one.
Mistakes With Stray Semicolons
Common mistake Placing a semicolon right after an if condition
What it looks likeif (x > 0); then a block on the next linesWhy it failsThe semicolon is a complete statement, so it becomes the body of the if. The following block is then no longer controlled by the condition.
Do this insteadRemove the semicolon so the block directly follows the condition: if (x > 0) { ... }Common mistake Assuming the compiler will flag the semicolon
What it looks likeCompiling if (x > 0); with a block after it and expecting an error
Why it failsThe empty statement is a valid Java statement, so the code compiles. The problem is a logic error, not a syntax error.
Do this insteadRead the line structure carefully, and check what the if body is after compiling.
Common mistake Treating the trailing semicolon after a closing brace as dangerous
What it looks likeif (x < 0) { x = -x; }; with the semicolon after the braceWhy it failsThis trailing semicolon is an empty statement that follows the if and contributes no action. It does not change which code the if controls.
Do this insteadIt is harmless, but it is unnecessary. Remove it for cleaner code, and be aware that the semicolon after the brace is not the if body.
Common mistake Forgetting that the empty statement is still a statement, so it can silently replace the intended body
When a conditional behaves oddly, locate the if body first. Find the semicolon or statement that immediately follows the condition, and confirm that it is the code you intended to control.
Practice Locating Empty Statements
In the code below, identify the empty statement, state which code the if body is, and predict whether the println runs when x is 10.
int x = 10;
if (x > 20);
{
System.out.println("big");
}Hints
- Look for a lone semicolon that directly follows the if condition.
- A block on the next lines is a separate statement unless it sits inside the if body.
- The condition is false here, but the block still runs if it is outside the if.
Fix the stray semicolon in this program so that big prints only when x is greater than 20, then explain in one sentence what changed in the if body.
Hints
- Remove the semicolon directly after the condition.
- Once the block follows the condition, it becomes the body of the if.
Key Takeaways
- An empty statement is a statement that consists simply of a semicolon, and it tells the computer to do nothing.
- Java accepts the empty statement as a real statement, so a semicolon after a closing brace in an if statement is legal even though it does nothing.
- A semicolon placed directly after an if condition becomes the body of that if, which means the code meant to run conditionally runs every time.
- To debug a conditional, locate the if body first and confirm that it is the statement you intended to control.