Reference Guide

Key Ideas

  • An if statement runs its body only when the condition is true. This is one way selection.
  • if … else chooses between two blocks. Exactly one of them runs. This is two way selection.
  • The condition must be a Boolean expression: if (x > 5), never if (x).
  • Braces { } group the statements that belong to the if. Without braces, only the very next statement belongs to it.
  • Code after the if or if-else runs 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 else part is optional.
  • No semicolon after if (condition). Writing if (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 if with a false condition runs nothing and moves on. That is normal, not an error.
  • if-else always 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.

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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.

Driver's License Checker
Green lines run. Faded lines are skipped.

Activity 2: Trace an if Without an else

Step through the program. Change temp to see the body get skipped entirely.

Jacket Check
One way selection: the body runs zero times or one time.

Activity 3: Predict the Output

Predict the Output
Braces and semicolons change everything. Read carefully.
Question 1

What does this code print?

int x = 3;
if (x > 5)
System.out.println("A");
System.out.println("B");
Question 2

What does this code print?

int score = 40;
if (score >= 60) {
System.out.println("Pass");
} else {
System.out.println("Retake");
}
System.out.println("Done");
Question 3

What is wrong with this code?

int coins = 3;
if (coins > 5);
{
System.out.println("Rich!");
}

Activity 4: Build the Program

Arrange the lines so the program prints Even when n is even and Odd otherwise, then prints Checked!.

Assemble an if-else
Every brace matters. Use the arrows or drag the lines.
int n = 12;
if (n % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
System.out.println("Checked!");

Popcorn Hack

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack

Two short tasks.

  1. Finish the runner below. A store gives free shipping on orders of $50 or more. Print Free shipping or Shipping: $5.99, then always print the order total.
  2. 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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
Homework Quick Check
One question.

How many lines does this code print? Type a number.

int level = 7;
if (level > 10) {
System.out.println("Expert");
System.out.println("Unlocked");
}
System.out.println("Level " + level);

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

All Java lessons