Algorithms and Programming / LESSON 07

Evaluate the condition, then choose the branch · AP Computer Science Principles

10 min + practice · Original foundation lesson

Evaluate relational and Boolean expressions exactly as written, then trace which IF or ELSE branch runs.

Core ideas and conditions

Relational operators =, !=, >, <, >= and <= compare two values and evaluate to a Boolean, true or false. In this pseudocode = tests equality, while the left arrow ← assigns a copy to a variable. The Boolean operators are the word forms NOT, AND and OR: NOT is true exactly when its condition is false, AND is true only when both parts are true, and OR is true when at least one part is true. Evaluate comparisons first, then NOT, then AND, then OR. Boundary operators differ exactly at the boundary: >= and <= include the boundary value, > and < exclude it. Selection then consumes those Booleans: an IF runs its block only when the condition is true and otherwise does nothing, while IF/ELSE runs exactly one of its two blocks.

Worked example: a two-condition gate

A grade portal sets score ← 65 and flagged ← false, then runs IF(score >= 65 AND NOT flagged) { DISPLAY("pass") } ELSE { DISPLAY("retry") }. Evaluate inside out: 65 >= 65 is true because >= includes the boundary; NOT false is true; true AND true is true, so the IF block runs and pass displays. Now change only flagged to true: NOT true is false, and true AND false is false, so exactly the ELSE block runs and retry displays. The displayed word changed without any change to score because one operand of the AND flipped. The trace order is the discipline: a reader who applies OR before AND, or lets NOT swallow the whole expression, selects a different branch than the code does.

Branch selection, nesting and common errors

Each IF/ELSE decision selects exactly one branch; nesting simply places another selection inside one branch. When a nested IF owns the inner test, a failed outer test does not cascade into running everything else: control enters the ELSE subtree and re-evaluates there. Three wrong turns account for most trace errors: assuming both branches of one decision can run in a single pass, treating a false first condition as if the whole construct were skipped even when an ELSE exists, and judging the outer test with the inner threshold. For two-way outcomes such as pass or retry, write the >= form in the IF and its complement in the ELSE so the two cases partition the possible values.

Transfer check: move the boundary

A venue rule admits ages 13 and up using IF(age >= 13). For age ← 12, which branch runs, and why? Then rewrite the condition as age > 13 and name the exact age whose outcome changes. Reading on: at 12 both versions refuse; the versions differ only at exactly 13, which >= admits and > refuses, and every other age keeps its original outcome because the operators differ only at the boundary. Complete this check by picking one age of your choice, writing each comparison’s Boolean result, and naming the branch each version selects.

Try it, then explain it.

Complete a short set without looking at the example. After each answer, explain which evidence or rule supports your decision and why the alternatives do not fit.

Practice 3 questions

Sources & authorship

Original OpenExam explanations and examples. References support the concepts; they are not the source of copied questions. Independent educator review is pending.

AP Computer Science Principles Exam Reference Sheet (PDF)