2.03 if Statements
Make one way and two way decisions with if and if-else, and see exactly which lines run.
- 1. LxD Cycle Process
- 2. Lesson Plan
- 3. Reference Guide
- 4. Code Examples
- A. Simple: if-else 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 assume something must happen when a condition is false, so a one-way if that simply skips its block reads as broken. They also drop braces and are then surprised which lines ran.
Define:
- POV: CSA students need to see exactly which lines execute for a given condition, because the AP exam tests tracing far more than it tests writing new
ifstatements. - Learning Goal: Students will trace one-way and two-way selection, predict output before running, and assemble a correct
if-elsefrom shuffled lines.
Ideate:
- HMW Question: How might we make the path a program takes visible, so students can see the skipped lines rather than assume them?
- Activity: A path tracer that highlights the executing line, a trace of an
ifwith noelse, an output prediction, and a build-the-program ordering task.
Prototype & Test: Activity 2 deliberately uses an if with no else so the false path is visible as nothing happening, which is the case students most often get wrong.
2. Lesson Plan
Learning Objective: Represent branching logical processes by using selection statements.
Success Criteria: You can predict which branch of an if or if-else runs, trace a one-way if where the condition is false, and place braces so the intended statements are inside the branch.
Tech Talk & Introduction (5 minutes)
An if statement runs a block only when its condition is true. Adding else gives a second block that runs when the condition is false.
- A one-way
ifwith a false condition runs nothing and moves on. That is normal, not an error. if-elsealways runs exactly one of the two blocks.- Without braces, only the next single statement belongs to the branch. This is a frequent source of silent bugs.
3. Reference Guide
Key Ideas
- An
ifstatement runs its body only when the condition istrue. This is one way selection. if…elsechooses between two blocks. Exactly one of them runs. This is two way selection.- The condition must be a Boolean expression:
if (x > 5), neverif (x). - Braces
{ }group the statements that belong to theif. Without braces, only the very next statement belongs to it. - Code after the
iforif-elseruns no matter which path was taken.
Shape of the Statement
if (condition) {
// runs only when condition is true
} else {
// runs only when condition is false
}
// always runs
- The
elsepart is optional. - No semicolon after
if (condition). Writingif (x > 5);makes an empty body, a silent bug.
4. Code Examples
A. Simple: if-else in Code
Run the program, then change number to an odd value and run again.
Code Runner Challenge
if-else demo: even or odd. Change number and run again.
View IPYNB Source
// CODE_RUNNER: if-else demo: even or odd. Change number and run again.
public class EvenOdd {
public static void main(String[] args) {
int number = 2; // change this to an odd number and run again
if (number % 2 == 0) {
System.out.println(number + " is even");
} else {
System.out.println(number + " is odd");
}
System.out.println("This line always prints.");
}
}
EvenOdd.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.03 if Statements HW
categories: [Java, If-Statements]
lesson_language: Java
lesson_topic: If-Statements HW
lesson_source: APCSA
lesson_type: lesson
permalink: /csa/unit_02/2_3_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: Which Path Runs?
Slide the age. The highlighted lines are the ones Java executes. Notice the last line always runs.
Activity 2: Trace an if Without an else
Step through the program. Change temp to see the body get skipped entirely.
Activity 3: Predict the Output
What does this code print?
What does this code print?
What is wrong with this code?
Activity 4: Build the Program
Arrange the lines so the program prints Even when n is even and Odd otherwise, then prints Checked!.
Popcorn Hack (In-Class)
Find and fix the two errors so the program compiles and prints Number is even.
Code Runner Challenge
Popcorn hack: two errors. Hint: Strings use double quotes, and every statement ends with a semicolon.
View IPYNB Source
// CODE_RUNNER: Popcorn hack: two errors. Hint: Strings use double quotes, and every statement ends with a semicolon.
public class FixMe {
public static void main(String[] args) {
int number = 2;
if (number % 2 == 0) {
System.out.println('Number is even');
} else {
System.out.println("Number is odd")
}
}
}
FixMe.main(null);
Homework Hack
Two short tasks.
- Finish the runner below. A store gives free shipping on orders of $50 or more. Print
Free shippingorShipping: $5.99, then always print the order total. - Answer the quick check.
Code Runner Challenge
Homework: write an if-else for free shipping, then a line that always runs.
View IPYNB Source
// CODE_RUNNER: Homework: write an if-else for free shipping, then a line that always runs.
public class Shipping {
public static void main(String[] args) {
double orderTotal = 42.50; // test with 42.50 and with 75.00
// TODO: if orderTotal is at least 50, print "Free shipping"
// TODO: otherwise print "Shipping: $5.99"
// TODO: always print "Order total: $" followed by orderTotal
}
}
Shipping.main(null);
How many lines does this code print? Type a number.
6. Grading Plan (1 Point Total)
Classroom Rubric
-
0.2 points: Popcorn completion Student found and fixed both errors so the program compiles and prints
Number is even. -
0.8 points: Homework completion
- 0.5 Free shipping branch:
Free shippingorShipping: $5.99prints correctly for orders above and below the $50 threshold. - 0.2 Always-run line: The order total prints in both cases, showing the statement was placed outside the branch.
- 0.1 Notebook quality: Every code cell executed with output visible, and a markdown cell before each code section explaining the concept.
- 0.5 Free shipping branch:
Quick Validation Checklist
- Present: both branches tested with a value above and below 50
- Present: braces around every branch body
- Present: quick check answered
- Test: an order of exactly 50 takes the free shipping branch
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 deliberately uses an if with no else so the false path is visible as nothing happening, which is the case students most often get wrong.
Open Item: Record peer feedback from the team teach delivery here once the lesson has been taught.
8. Summary
if (condition) { ... }runs the body when the condition is true, otherwise skips it.if ... elseruns exactly one of two blocks.- Use braces every time; without them only the next statement belongs to the
if. - Never put a semicolon right after
if (condition). - Statements after the
ifrun no matter what. - Next lesson: putting an
ifinside anotherif, andelse ifchains.
9. References
College Board Course and Exam Description
Topic 2.3, 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
The language specification defines the two forms of the statement separately, which is why a one-way if with a false condition completes normally without running anything.
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.