2.03 if Statements
Make one way and two way decisions with if and if-else, and see exactly which lines run.
- Reference Guide
- College Board Requirements
- Lesson Plan
- Code Examples
- A. Simple: if-else in Code
- Code Runner Challenge
- Hacks & Practice Tasks
- Activity 1: Which Path Runs?
- Activity 2: Trace an if Without an else
- Activity 3: Predict the Output
- Activity 4: Build the Program
- Popcorn Hack
- Code Runner Challenge
- Homework Hack
- Code Runner Challenge
- References
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.
College Board Requirements
AP CSA Unit 2, Topic 2.3: if Statements.
This lesson is built directly against the College Board’s stated learning objective for this topic:
Represent branching logical processes by using selection statements.
Topic 2.3, if Statements, is required content on the AP Computer Science A Exam. The learning objective quoted above is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
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
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.
Code Examples
A. Simple: if-else in Code
Run the program, then change number to an odd value and run again.
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.");
}
}
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);
Hacks & Practice Tasks
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
Find and fix the two errors so the program compiles and prints Number is even.
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")
}
}
}
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.
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
}
}
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.
References
College Board Course and Exam Description
Topic 2.3, if Statements, is required content on the AP Computer Science A Exam. The learning objective quoted in section 3 (College Board Requirements) 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