Skip to main content
United StatesComputer Science PrinciplesSyllabus dot point

How do conditionals let a program make decisions, and how do nested conditionals handle several cases?

Topic 3.6/3.7 Conditionals and Nested Conditionals: conditional (IF/ELSE) statements select which code runs based on a Boolean condition, and nested conditionals handle multiple decision paths.

A focused answer to AP CSP Topics 3.6 and 3.7, covering IF and IF/ELSE selection, the role of the Boolean condition, nested conditionals for multiple paths, tracing which branch runs, and writing decision logic in AP CSP pseudocode.

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. The IF statement
  3. The IF/ELSE statement
  4. Nested conditionals for multiple paths
  5. Why the condition must be Boolean
  6. Try this

What this topic is asking

The College Board (Topics 3.6 and 3.7) wants you to use conditional statements to make a program make decisions. An IF statement runs a block of code only when its Boolean condition is true; an IF/ELSE chooses between two blocks. Nested conditionals (a conditional inside another) handle multiple decision paths, such as grading into bands. You need to write decision logic and trace which branch runs for a given input.

The IF statement

IF (temperature > 30)
{
  DISPLAY("It is hot")
}

The IF/ELSE statement

IF (score ≥ 50)
{
  DISPLAY("Pass")
}
ELSE
{
  DISPLAY("Fail")
}

A conditional without an ELSE simply does nothing when the condition is false, and the program continues with the statements after the IF block. With an ELSE, one of the two paths is always taken. This is the difference between "do this extra step if a condition holds" and "choose one of two paths".

Nested conditionals for multiple paths

When there are more than two outcomes, you nest a conditional inside a branch. Each level narrows the possibilities. Order the tests carefully: once you are inside an ELSE, you already know the outer condition was false, so the inner test only needs to distinguish the remaining cases.

A nested chain reads as a series of questions. "Is it the highest band? If not, is it the next band? If not, it must be the lowest." Each ELSE block has already ruled out everything tested above it, which is why the inner conditions can be simpler than they first appear. The depth of nesting matches the number of distinct outcomes minus one.

Why the condition must be Boolean

A conditional always tests a Boolean expression, one that evaluates to true or false. That expression usually comes from a relational comparison (score >= 90) or a logical combination (age >= 13 AND age < 65). Getting the condition right is most of the work: a correct branch structure with a wrong condition still gives wrong results. When you write a conditional, first decide exactly what Boolean question separates the cases, then build the branches around it.

Try this

Q1. For the code in the worked example, what is displayed when age ← 10? [1 point]

  • Cue. 10 < 13 is true, so the outer IF runs and "child" is displayed.

Q2. In an IF/ELSE statement, how many of the two blocks run each time? [1 point]

  • Cue. Exactly one: the IF block when the condition is true, the ELSE block when it is false.

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 2022 (style)1 marksMultiple choice. Consider the following AP CSP pseudocode. ``` n ← 4 IF (n > 10) { DISPLAY("big") } ELSE { IF (n > 3) { DISPLAY("medium") } ELSE { DISPLAY("small") } } ``` What is displayed? (A) big (B) medium (C) small (D) nothing
Show worked answer →

The answer is (B).

n is 4. The outer condition n > 10 is false, so the ELSE branch runs. Inside it, n > 3 is true (4 > 3), so "medium" is displayed. (A) would need n > 10. (C) would need n <= 3. (D) is wrong because one branch always runs in an IF/ELSE.

Markers reward tracing nested conditionals by evaluating the outer condition first, then the inner one only in the branch that runs.

AP 2021 (style)3 marksFree response (code writing). Write AP CSP pseudocode that takes a numeric `score` and displays "A" for 90 or above, "B" for 80 to 89, and "C" for below 80.
Show worked answer →

A 3-point question on multi-way selection with nested conditionals.

IF (score ≥ 90)
{
  DISPLAY("A")
}
ELSE
{
  IF (score ≥ 80)
  {
    DISPLAY("B")
  }
  ELSE
  {
    DISPLAY("C")
  }
}

Point 1: test the highest band first (score ≥ 90). Point 2: in the ELSE, test the next band (score ≥ 80); because we already know score < 90, this captures exactly 80 to 89. Point 3: the final ELSE handles everything below 80. A common error is testing score ≥ 80 first, which would label a 95 as "B".

Related dot points

Sources & how we know this