AP CSA Unit 2 Day 5: If Else

Unit 2 Foundation (Cycle 1) Day 5 of 28 Foundation

If-Else Branching

Section 2.3 — If-Else Statements

Key Concept

An if-else statement guarantees exactly one of two code blocks will execute. When the condition is true, the if block runs; when false, the else block runs. This mutual exclusion is important for tracing: you never need to consider both paths for a single execution. The AP exam tests whether you correctly identify which branch executes based on the current variable values and whether subsequent code after the if-else always runs regardless of the branch taken.

Consider the following code segment.

int num = 0; if (num > 0) { System.out.print("positive"); } else if (num < 0) { System.out.print("negative"); } else { System.out.print("zero"); } System.out.print(" done");

What is printed as a result of executing the code segment?

Answer: (C) zero done

num = 0. First: 0 > 0 is false. Second: 0 < 0 is false. Else executes: prints "zero". Then the print after the if-else chain always executes: prints " done".

Why Not the Others?

(A) 0 is not greater than 0.

(B) 0 is not less than 0.

(D) The final print statement is outside the if-else chain, so it always executes.

Common Mistake

Code after an if-else chain is not part of the chain. The System.out.print(" done") is outside all branches, so it always executes regardless of which branch was taken.

AP Exam Tip

Carefully determine what is inside vs. outside an if-else chain. Indentation helps visually, but it is the curly braces that determine scope.

Review this topic: Section 2.3 — If-Else Statements • Unit 2 Study Guide

More Practice

Related FRQs

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.