Unit 2.4: Nested if Statements

AP Computer Science A

Learning Objectives:

• Develop code to represent nested branching logical processes

• Determine the result of these processes

• Understand multiway selection (if-else-if)

What are Nested if Statements?

Definition:

Nested if statements consist of if, if-else, or if-else-if statements within other if, if-else, or if-else-if statements.

Basic Structure:

if (outerCondition) { if (innerCondition) { // Inner code block } else { // Inner else block } }

Key Point:

The Boolean expression of the inner nested if statement is evaluated only if the Boolean expression of the outer if statement evaluates to true.

Simple Nested if Example

Weather Decision Program

public class WeatherDecision { public static void main(String[] args) { boolean isSunny = true; int temperature = 75; if (isSunny) { if (temperature > 70) { System.out.println("Perfect day for the beach!"); } else { System.out.println("Sunny but too cold for beach."); } } else { System.out.println("Not sunny today."); } } }

Execution Flow:

1. Check if isSunny is true

2. If true, then check if temperature > 70

3. If false, skip inner conditions entirely

Interactive Nested if Example

Grade Calculator

Try it yourself!

Enter values and click Calculate Grade
if (score >= 60) { if (attendance >= 90) { grade = "A"; } else if (attendance >= 80) { grade = "B"; } else { grade = "C"; } } else { grade = "F"; }

Multiway Selection (if-else-if)

Key Concept:

Multiway selection is used when there are a series of expressions with different segments of code for each condition. No more than one segment of code is executed based on the first expression that evaluates to true.

Structure:

if (condition1) { // Code block 1 } else if (condition2) { // Code block 2 } else if (condition3) { // Code block 3 } else { // Default code block }

Important:

• Conditions are checked in order

• First true condition executes

• Remaining conditions are skipped

• else block runs if no condition is true

Complex Nested Example

Student Classification System

public static String classifyStudent(int age, double gpa, boolean isHonors) { String classification; if (age >= 18) { if (gpa >= 3.5) { if (isHonors) { classification = "Senior Honors Student"; } else { classification = "Senior High Achiever"; } } else { classification = "Senior Student"; } } else { if (gpa >= 3.0) { classification = "Junior High Achiever"; } else { classification = "Junior Student"; } } return classification; }

Trace this example: age = 19, gpa = 3.7, isHonors = true

Result: "Senior Honors Student"

Common Patterns & Best Practices

✅ Good Practices:

  • Use proper indentation
  • Keep nesting levels reasonable (max 3-4)
  • Use meaningful variable names
  • Consider if-else-if for mutually exclusive conditions
  • Test all possible paths

❌ Common Mistakes:

  • Too much nesting (hard to read)
  • Missing else cases
  • Incorrect operator precedence
  • Not considering all conditions
  • Redundant conditions

When to Use Nested vs. Multiway:

Nested: When conditions are dependent on each other

Multiway: When conditions are mutually exclusive

// Dependent conditions (use nested) if (hasPermission) { if (isValidUser) { // Grant access } } // Mutually exclusive (use multiway) if (grade >= 90) { // A grade } else if (grade >= 80) { // B grade }

Summary & Key Takeaways

Essential Knowledge Review:

2.4.A.1: Nested if statements consist of conditional statements within other conditional statements

2.4.A.2: Inner conditions are only evaluated if outer conditions are true

2.4.A.3: Multiway selection executes at most one code segment based on the first true condition

Quick Reference:

  • Outer condition must be true first
  • Only one path executes in multiway
  • else provides default case
  • Indentation shows structure

Practice Tips:

  • Trace through code with different inputs
  • Draw flowcharts to visualize logic
  • Test edge cases
  • Simplify complex nested structures
// Practice: What does this output when x = 5, y = 10? if (x > 0) { if (y > x) { System.out.println("Both conditions true"); } else { System.out.println("Only first condition true"); } } else { System.out.println("First condition false"); }
1 / ?