Unit 2.4: Nested Conditionals
Nested if Statements
You can place if or if-else statements inside other if or if-else statements. This is called nesting.
- Nested conditionals create "levels" of conditions. The computer only evaluates an inner condition if all its outer conditions were met. This is ideal for modeling step-by-step logic.
Nested if statements allow you to test for more complex, multi-level conditions. The inner if statement is only checked if the outer if statement's condition is true. This creates a hierarchical decision structure.
Task: Implementing hierarchical logic using nested if-else statements.
public class NestedIfExample {
public static void main(String args[]) {
int age = 20;
boolean hasLicense = true;
if (age >= 16) {
System.out.println("Old enough to be on the road.");
// This is the nested conditional
if (hasLicense) {
System.out.print("You can legally drive!");
} else {
System.out.println("You still need to get a license.");
}
} else {
System.out.println("You are too young to drive.");
}
}
}
In this example, the program first checks the age. Only if the person is 16 or older does it proceed to the nested check for hasLicense.
Multi-Way Selection: The if-else-if Ladder
A common structure that looks like nesting is the multi-way selection or if-else-if ladder. It is used to check a series of conditions in order. The first condition that evaluates to true has its code block executed, and the rest of the ladder is skipped. If no conditions are true, the final else block is executed.
This is an efficient way to handle multiple mutually exclusive conditions.
Exclusion Statement: The
chardata type used in the following example is outside the scope of the AP Computer Science A course and exam, which focuses onint,double, andboolean. However, it is used here to represent a single letter grade.
Task: Using an if-else-if ladder for grade conversion.
public class GradeConverter {
public static void main(String[] args) {
int score = 78;
char grade;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C'; // This block is executed
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("The grade is: " + grade);
}
}
Common Pitfalls: The Dangling else
When if statements are nested without braces {}, an else clause belongs to the closest preceding if statement that does not already have an else. Indentation can be misleading in these cases.
- Without braces, the compiler ignores indentation and pairs an
elsewith the nearestif. To avoid logic errors and maintain clarity, ALWAYS use curly braces even for single-line blocks.
Task: Identifying a dangling else logic error.
int x = 10;
int y = 2;
if (x > 5)
if (y > 5)
System.out.println("Both > 5");
else
System.out.println("Else executes here because y is not > 5");
// The 'else' actually pairs with 'if (y > 5)'.
// To avoid confusion, ALWAYS use braces.
Flattening Nested Code
Sometimes, nested if statements can be simplified using logical operators like &&.
Task: Implementing nested if statements.
if (score > 90) {
if (hasExtraCredit) {
grade = "A+";
}
}
Task: Simplifying nested logic using the AND operator.
if (score > 90 && hasExtraCredit) {
grade = "A+";
}