2.04 Nested `if` Statements
Chain decisions with else if and put an if inside another if to handle three or more cases.
- 1. LxD Cycle Process
- 2. Lesson Plan
- 3. Reference Guide
- 4. Code Examples
- A. Simple: else if in Code
- Code Runner Challenge
- 5. Hacks & Practice Tasks
- 6. Grading Plan (1 Point Total)
- 7. Lesson Revisions & Feedback Evidence
- 8. Summary
- 9. References
1. LxD Cycle Process
Empathize: Students write grade chains that return the wrong letter because the conditions are ordered from lowest to highest, so the first broad test captures every case. The bug produces no error, only wrong answers.
Define:
- POV: CSA students need to reason about condition order, because an
else ifchain that compiles and runs can still be completely wrong. - Learning Goal: Students will build and trace
else ifchains, detect unreachable branches caused by ordering, and nest a decision inside a branch.
Ideate:
- HMW Question: How might we make an unreachable branch visible, so students test condition order instead of assuming it?
- Activity: A grade machine that shows which branch fires, an ordering exercise where a wrong order produces wrong letters, a nested trace, and an assemble-the-chain task.
Prototype & Test: Activity 2 is built around ordering rather than syntax, because ordering is the failure that survives compilation.
2. Lesson Plan
Learning Objective: Represent branching logical processes by using nested selection statements.
Success Criteria: You can build an else if chain that handles three or more cases, explain why only the first true branch runs, and order the conditions so no case is unreachable.
Tech Talk & Introduction (5 minutes)
An else if chain tests conditions in order and stops at the first one that is true. A nested if puts a second decision inside a branch that has already been taken.
- Only one branch of a chain ever runs, even if several conditions would be true.
- Order matters. A broad condition placed first makes the narrower ones below it unreachable.
- Nesting is for decisions that only make sense after an earlier decision was true.
3. Reference Guide
Key Ideas
- A nested if is an
ifinside the body of anotheriforelse. The inner condition is checked only when the outer path is taken. - An else if chain (multi way selection) tests conditions top to bottom. The first
truecondition wins and every remaining branch is skipped. - Order matters. Put the most specific or strictest condition first.
- A final
elseis optional. It catches everything the earlier conditions did not. - Use nesting when the second question only makes sense after the first one is answered.
Two Shapes
| else if chain | nested if |
|---|---|
if (score >= 90) {grade = "A";} else if (score >= 80) {grade = "B";} else {grade = "C or below";} |
if (isRaining) {if (isWindy) {msg = "Wear a raincoat";} else {msg = "Bring an umbrella";}} else {msg = "Enjoy the sun";} |
| One question with many possible answers | A second question asked only in one branch |
4. Code Examples
A. Simple: else if in Code
Run it, then change myAge to 19, 15, 13 and predict the output each time.
Code Runner Challenge
else if chain: only the first true branch runs. Change myAge and run again.
View IPYNB Source
// CODE_RUNNER: else if chain: only the first true branch runs. Change myAge and run again.
public class AgeChecker {
public static void main(String[] args) {
int myAge = 17; // try 19, 15, and 13
System.out.println("Current age: " + myAge);
if (myAge >= 18) {
System.out.println("You can register to vote.");
System.out.println("You are old enough for a license.");
} else if (myAge >= 16) {
System.out.println("You are old enough for a license.");
} else if (myAge >= 15) {
System.out.println("You can get a learner's permit.");
} else {
System.out.println("A few more years to go.");
}
}
}
AgeChecker.main(null);
5. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one markdown cell at the top with the frontmatter:
---
layout: post
title: CSA Unit 2.04 Nested `if` Statements HW
categories: [Java, Nested-If-Statements]
lesson_language: Java
lesson_topic: Nested-If-Statements HW
lesson_source: APCSA
lesson_type: lesson
permalink: /csa/unit_02/2_4_hw
author: yourGithubID
---
- Add code cells for the Popcorn Hack and the Homework Hack. Ensure all code runs and output is visible.
- Include a markdown cell before each code section explaining the concept.
Submission Safety Rules (Read First)
[IMPORTANT] To avoid grading errors, follow these rules exactly:
- Write your prediction before you run each code section
- Execute every code cell and leave the output visible
- Show your work for any hand-traced prediction
- Do not just copy code; explain what each line does
- Label each question clearly
Activity 1: Grade Machine
Slide the score. Only the first true condition runs, and everything after it is skipped, even if it would also be true.
Activity 2: Order Matters
What does this print when score is 95?
These are separate if statements, not a chain. What prints when score is 95?
Activity 3: Trace a Nested if
Step through the weather program. Change the two settings and watch which questions Java even bothers to ask.
Activity 4: Assemble the Chain
Build a ticket price program: children under 5 are free, students under 18 pay $8, everyone else pays $12.
Popcorn Hack (In-Class)
Add branches so the program prints the right letter for every average: A (90 and up), B (80 to 89), C (70 to 79), D (60 to 69), F (below 60). Test with 90, 75, 61, and 40.
Code Runner Challenge
Popcorn hack: extend the chain with C, D, and F.
View IPYNB Source
// CODE_RUNNER: Popcorn hack: extend the chain with C, D, and F.
public class GradeEvaluator {
public static void main(String[] args) {
double myAverage = 75; // test with 90, 75, 61, 40
System.out.println("Current average: " + myAverage);
if (myAverage >= 90) {
System.out.println("You have an A average.");
} else if (myAverage >= 80) {
System.out.println("You have a B average.");
}
// TODO: add C, D, and F branches
}
}
GradeEvaluator.main(null);
Homework Hack
Two short tasks.
- Finish the runner below with a nested if: members get a discount, and members who spend $100 or more get a bigger one.
- Answer the quick check.
Code Runner Challenge
Homework: nested if for member discounts. Expected for a member spending 120.0: Discount 20.0% and You pay $96.0.
View IPYNB Source
// CODE_RUNNER: Homework: nested if for member discounts. Expected for a member spending 120.0: Discount 20.0% and You pay $96.0.
public class Discount {
public static void main(String[] args) {
boolean isMember = true; // test all four combinations
double total = 120.0;
double discount = 0;
// TODO: if isMember is true:
// if total >= 100, discount = 0.20
// otherwise discount = 0.10
// if isMember is false, discount stays 0
System.out.println("Discount: " + (discount * 100) + "%");
System.out.println("You pay: $" + (total - total * discount));
}
}
Discount.main(null);
What does this print when temp is 75? Type the exact word.
6. Grading Plan (1 Point Total)
Classroom Rubric
-
0.2 points: Popcorn completion Student added branches covering A, B, C, D and F and tested with 90, 75, 61 and 40.
-
0.8 points: Homework completion
- 0.5 Nested discount: A nested
ifgives members a discount and gives members spending $100 or more the larger discount. - 0.2 Case coverage: Non-members, members under $100 and members at or above $100 are each tested.
- 0.1 Notebook quality: Every code cell executed with output visible, and a markdown cell before each code section explaining the concept.
- 0.5 Nested discount: A nested
Quick Validation Checklist
- Present: all five grade branches tested with the four listed values
- Present: nested structure, not a flat chain, for the member discount
- Absent: an unreachable branch caused by condition order
- Test: a non-member spending $200 receives no discount
7. Lesson Revisions & Feedback Evidence
Feedback Received: Team review of the Unit 2 set found that these lessons did not follow the section format used by the Unit 1 lessons, and that they ended without the assignment submission form every other CSA lesson provides.
Revision Made: The lesson was reorganised into the shared CSA lesson format, so it now opens with the LxD cycle and lesson plan, states its reference material before the code, and groups the activities, Popcorn Hack and Homework Hack under one practice section. The duplicate lesson heading was removed, and the page now renders the standard Submit Assignment form at the end.
Design Decision Kept: Activity 2 is built around ordering rather than syntax, because ordering is the failure that survives compilation.
Open Item: Record peer feedback from the team teach delivery here once the lesson has been taught.
8. Summary
else ifchains handle three or more cases; only the first true branch runs.- Put the strictest condition first, or later branches become unreachable.
- A nested
ifasks a second question inside one branch of the first. - A final
elsecatches every remaining case. - Next lesson: combining conditions with
&&,||, and!.
9. References
College Board Course and Exam Description
Topic 2.4, Nested if Statements, is required content on the AP Computer Science A Exam. The objective quoted in section 2 of this lesson is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
Outside Academic Reference
A nested selection is simply an if statement appearing as the statement inside another branch; the specification does not define a separate construct for it, which is why the ordinary rules about braces and scope apply unchanged.
Reference List
College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/courses/ap-computer-science-a
Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java language specification: Java SE 21 edition. Oracle America. https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.9
Submit Assignment
Need to update a submission later? Open the submissions dashboard.