Sub topic 4
Nest Decisions and Use Braces to Clarify Which else Belongs Where
Sometimes you need to answer one question before another question matters. Say you want to enter a study room. First, check whether the room is open. If it is, check whether you have a pass. If the room is closed, there is no need to check your pass.
In Java, you can put one if inside another. This is called nesting. The first if is the outer if. The one inside it is the inner if. A block is a group of statements between braces, the curly marks { and }. Put the inner if inside the outer if’s block so that Java reaches it only when the room is open.
Work Through a Nested Decision
A condition is a test that gives true or false. In this example, the conditions use boolean variables. A boolean variable stores one of those two values. Save the program as RoomEntry.java, then compile and run it as you did with earlier Java programs.
public class RoomEntry {
public static void main(String[] args) {
boolean roomOpen = true;
boolean hasPass = false;
if (roomOpen) {
if (hasPass) {
System.out.println("Come in.");
} else {
System.out.println("You need a pass.");
}
} else {
System.out.println("The room is closed.");
}
}
}Follow the path Java takes with the values shown:
- Java checks roomOpen. It is true, so Java enters the outer if’s block.
- Inside that block, Java checks hasPass. It is false, so Java runs the inner else and prints “You need a pass.”
- Java skips the outer else. That block runs only when roomOpen is false.
Each path through the decision is called a branch. Notice that reaching the inner else does not mean both tests failed. It means the first test passed and the second test failed.
Try changing the two values and running the program again. Before each run, predict both the message and whether Java will reach the pass check.
| roomOpen | hasPass | Does Java check hasPass? | Message |
|---|---|---|---|
| true | true | Yes | Come in. |
| true | false | Yes | You need a pass. |
| false | true | No | The room is closed. |
| false | false | No | The room is closed. |
The last two rows have the same result. Java does not check hasPass on either run. This is why the pass check belongs inside the room check, not in a separate if after it.
See How Missing Braces Can Change the Meaning
Indentation is the space at the start of a line. It helps people see which code belongs together, but Java does not use it to decide which if an else belongs to. Without braces to mark the block, an else belongs to the nearest if that can still take an else. When the spacing suggests a different match, you have an example of the dangling else problem.
Save this second program as BraceCheck.java. Look at where the else lines up, and predict the output before you run it.
public class BraceCheck {
public static void main(String[] args) {
boolean roomOpen = true;
boolean hasPass = false;
if (roomOpen)
if (hasPass)
System.out.println("Come in.");
else
System.out.println("The room is closed.");
}
}The program prints “The room is closed.” even though roomOpen is true. Here, Java pairs the else with the inner if, the one that checks hasPass. Since the room is open, Java reaches that check. Since hasPass is false, it runs the else. Try setting roomOpen to false. Now nothing prints: Java skips the entire inner decision, and the outer if has no else.
Use Braces to Show the Match
Look back at RoomEntry. The inner else comes right after the closing brace of the hasPass block. Both parts of that inner decision sit inside the roomOpen block. The closing brace just before the outer else ends the entire roomOpen block. The outer else therefore belongs to the check for roomOpen.
When you read a nested decision, start with the outer condition. If it is false, skip its whole block, including any decisions inside it. Then look for an else after the brace that closes that block. This is often faster than trying to follow every line in order.
Practice
Exercise
In RoomEntry, roomOpen is false and hasPass is true. What prints, and does Java check hasPass?
Show answer
It prints “The room is closed.” Java skips the whole outer if block, so it does not check hasPass.
Exercise
In BraceCheck, would shifting the else to line up with the inner if change the result? What would make the else belong to the outer if instead?
Show answer
No. Shifting it only changes how the code looks. Put the inner if and its print statement inside braces for the outer if, then put the else after the brace that closes the outer block.