01
Overview

Two paths, exactly one taken: the if-else.

When a program must always do something — option A or option B — if-else guarantees that exactly one branch runs.

An if-else statement extends the single if by adding a fallback. If the condition is true, the if block runs. Otherwise the else block runs.

This is your tool for complete coverage: every possible input lands in exactly one branch, never both, never neither.

When more than two outcomes are needed, Java chains them using else if ladders — a structure tested frequently on the AP exam.

02
Core Concept

Mutually exclusive branches.

Only one block in the chain ever executes per pass.

Basic syntax

if (condition) {
    // runs when condition is true
} else {
    // runs when condition is false
}

else if ladder

if (score >= 90) {
    grade = "A";
} else if (score >= 80) {
    grade = "B";
} else if (score >= 70) {
    grade = "C";
} else {
    grade = "F";
}

Java evaluates the conditions from top to bottom. The first true condition wins, and all remaining branches are skipped. This is why order matters in an else if ladder.

Why order matters

If you reverse the conditions above and start with score >= 70, a score of 95 would still be assigned "C" because the first true condition triggers first.

03
Key Vocabulary

The language of branching.

Terms used in every AP CSA conditional question.

Vocabulary
  • if-else statement — a conditional with exactly one mandatory branch out of two.
  • else if — a chained condition checked only when all previous conditions were false.
  • Mutually exclusive — only one branch can run in a given execution.
  • Branch coverage — ensuring every possible input reaches some branch.
  • Default branch — the final else that handles any case not caught earlier.
04
AP Exam Focus

How if-else is tested.

Expect ordering traps and branch-coverage questions.

Exam Focus

MCQ traps to watch for:

  • Wrong ladder order. A ladder that checks score >= 70 before score >= 90 assigns the wrong grade to high scorers.
  • Multiple independent if statements instead of an if-else chain. These can each fire — sometimes producing duplicate output.
  • Missing else. Without a final else, some inputs may produce no output at all.

FRQ tip: when your method must always return a value, an if-else chain ending in else guarantees compilation. Returning only from inner if blocks can trigger a "missing return" error.

05
Worked Example

Convert independent ifs into an else if ladder.

A subtle rewrite that fixes a duplicate-output bug.

Worked Example AP-style reasoning

Problem. What is wrong with this code when score = 95?

if (score >= 60) System.out.println("Pass");
if (score >= 80) System.out.println("Honors");
if (score >= 90) System.out.println("Distinction");

Trace. All three conditions are independent. With score = 95, all three are true, so the program prints Pass, then Honors, then Distinction.

Fix. Use an else if ladder, ordered from highest to lowest, so only the most specific label prints.

if (score >= 90) {
    System.out.println("Distinction");
} else if (score >= 80) {
    System.out.println("Honors");
} else if (score >= 60) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}

Why this works. The first true condition fires and the rest are skipped — exactly one line prints per input. The final else covers the lowest scores.

06
Common Mistakes

Errors that break decision logic.

Most if-else bugs come from structure, not syntax.

Watch Out
  • Using separate if statements when you meant else if. This causes multiple branches to fire when ranges overlap.
  • Bad ladder order. Always order from the most specific (tightest) condition to the most general.
  • Forgetting the final else. Some inputs may quietly produce no output or no return value.
  • Returning from only some branches in a non-void method. Java requires that every possible path returns a value.
  • Dangling-else confusion. Without braces, else binds to the nearest if, not the one you expect.
07
Reference Table

Branching patterns at a glance.

Pick the right structure for the number of outcomes.

if-else Reference

AP Quick Reference
Pattern When to use AP Exam Tip
Single if Optional action when no fallback is needed The block may run zero or one time.
if / else Exactly two mutually exclusive outcomes Guarantees one branch always runs.
else if ladder Three or more ranges or categories Order matters — most specific first.
Independent ifs Truly unrelated checks Each can fire — beware duplicate output.
Final else Default catch-all Ensures full branch coverage.
08
Practice Tip

How to master if-else chains.

Move from tracing to structuring your own decision logic.

Practice Strategy

Use the three practice tools below this lesson in order:

  • MCQ Practice — focus on tracing ladders and spotting wrong order. Predict the output for each branch before choosing.
  • Java Lab — convert messy independent if statements into clean else if chains. Confirm exactly one branch fires per input.
  • FRQ Practice — when a method must always return a value, finish with a default else so the compiler is happy and your logic is exhaustive.

If you can sketch the branches as a number line before writing code, your ladder order will almost always be correct.

09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

Topic Quiz 25 min 15 marks Pending

AP CSA Topic Practice: if-else

AP-style topic practice assessment

Start
FRQ Practice 30 min 18 marks Pending

AP CSA Unit 02 Topic 02 if-else FRQ Practice

AP-style topic practice assessment

Start