Skip to main content
United StatesComputer ScienceSyllabus dot point

How does an if-else statement choose between two alternative blocks of code?

Topic 3.3 if-else Statements: use a two-way if-else statement so that exactly one of two code blocks runs depending on whether the boolean condition is true or false.

A focused answer to AP CSA Topic 3.3, covering two-way selection with if-else, the guarantee that exactly one branch runs, how the else attaches to the nearest if, nested if-else, and how to trace each branch, with a fully worked example.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Syntax of if-else
  3. Exactly one branch runs
  4. The else attaches to the nearest if
  5. Nested if-else
  6. Try this

What this topic is asking

The College Board (Topic 3.3) wants you to write and trace a two-way if-else statement: a structure that chooses between two blocks of code. When the boolean condition is true, the if block runs; when it is false, the else block runs. Exactly one of the two always runs - never both, never neither. This is the standard tool for either/or decisions.

Syntax of if-else

if (condition) {
  // block A: runs when condition is true
} else {
  // block B: runs when condition is false
}
// execution continues here afterwards

The else keyword has no condition of its own. It simply provides the alternative path taken whenever the if condition evaluates to false.

Exactly one branch runs

You can visualize the choice:

flowchart TD A[evaluate condition] -->|true| B[run if block A] A -->|false| C[run else block B] B --> D[continue after if-else] C --> D

The else attaches to the nearest if

Because of this rule, careful bracing matters whenever you nest an if inside another if. Writing each block with braces guarantees the else attaches where you intend.

Nested if-else

You can place an if-else inside the if or else block of another, to make a decision in stages. For long chains of mutually exclusive options, the else if form (Topic 3.4) is cleaner, but a single nested if-else is common for two-level decisions. When you nest, brace every block: this both removes the dangling-else ambiguity and makes it obvious which condition each branch belongs to. The outer decision is made first, and only the chosen branch's inner if-else is then evaluated, so the two decisions happen in sequence rather than at once.

if (temp > 30) {
  System.out.println("hot");
} else {
  if (temp < 10) {
    System.out.println("cold");
  } else {
    System.out.println("mild");
  }
}

Try this

Q1. In an if-else, how many of the two blocks run on a single pass? [1 point]

  • Cue. Exactly one - the if block if the condition is true, otherwise the else block.

Q2. Explain the difference between one if-else and two separate if statements with opposite conditions. [2 points]

  • Cue. if-else guarantees exactly one branch runs and evaluates the condition once; two separate if statements evaluate two conditions and, if written carelessly, could both run or both be skipped.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2021 (style)1 marksMultiple choice. Consider the following code segment. ```java int x = 5; int y = 0; if (x > 10) { y = 1; } else { y = 2; } y = y + 3; System.out.println(y); ``` What is printed? (A) `5` (B) `4` (C) `1` (D) `2` (E) `3`
Show worked answer →

The answer is (A).

The condition x > 10 is 5 > 10, which is false, so the else block runs and y becomes 2. The if block (y = 1) is not executed; exactly one of the two branches runs. After the if-else, y = y + 3 runs unconditionally: 2 + 3 = 5. (C) and (D) forget the trailing + 3; (B) wrongly takes the if branch.

Markers reward knowing that exactly one branch of an if-else executes, and that code after the if-else runs in every case.

AP 2019 (style)3 marksFree response (code writing). An `int` variable `num` is given. Write a code segment that prints `"even"` if `num` is divisible by 2 and `"odd"` otherwise, using a single if-else statement. Print exactly one of the two words.
Show worked answer →

A 3-point question testing two-way selection.

if (num % 2 == 0) {
  System.out.println("even");
} else {
  System.out.println("odd");
}

Point 1: the condition num % 2 == 0 correctly tests divisibility by 2 using modulo. Point 2: the if block prints "even" when the remainder is 0. Point 3: the else block prints "odd" in every other case. Because it is if-else, exactly one word is printed - never both, never neither. Using two separate if statements without else would risk printing nothing or duplicating logic.

Related dot points

Sources & how we know this