Skip to content
Marlbridge

AP® Computer Science Principles · Unit 3 · Topic 3.6

Conditionals: Revision Notes (Computer Science Principles 3.6)

One-page recap of selection: IF versus IF/ELSE, flow of control, testing each branch and boundary values, with the classic two-IF trap and quick checks.

Recap

  • Selection decides which statements run based on a Boolean condition.
  • IF(condition) { ... } runs the block only if the condition is true.
  • IF(condition) { ... } ELSE { ... } runs exactly one block.
  • Flow of control normally goes top to bottom; a conditional skips blocks.
  • Every algorithm can be built with sequencing, selection and iteration.

Key relationships

Pattern What runs Example
IF only Block if true; nothing if false Apply a discount only on big orders
IF/ELSE Exactly one of two blocks Child price or adult price
Two separate IFs Zero, one or both blocks Two independent checks
Boolean variable as condition IF(isOpen) No need to write isOpen = true
Default then override Set value, IF changes it discount ← 0 then IF raises it

Assumptions

  • The condition is a Boolean expression evaluated when the IF is reached.
  • Statements after the conditional run whichever block was chosen.
  • Blocks are marked by braces in the Text notation.

Method for tracing

  1. Write the values of all variables.
  2. Evaluate the condition and write true or false.
  3. Run only the matching block; cross out the other.
  4. Continue from the first statement after the conditional.

Mistakes to avoid

  • Running both the IF block and the ELSE block.
  • Treating two IFs as IF/ELSE.
  • Using > instead of ≥ at the boundary.
  • Testing only one branch.
  • Forgetting to give a variable a default value before an IF without ELSE.

Worked mini-example

n ← 14
IF(n MOD 2 = 0)
{
  DISPLAY("even")
}
ELSE
{
  DISPLAY("odd")
}

Output: even

14 MOD 2 is 0, so the condition is true and the first block runs. With n equal to 15 the remainder is 1, the condition is false and the ELSE block runs.

Quick self-check

  1. In an IF/ELSE, how many blocks run?
  2. After x ← 7, IF(x > 5) {DISPLAY("a")}, IF(x > 6) {DISPLAY("b")}, what is displayed?
  3. Which test value checks the boundary of score ≥ 500?
Answers
  1. Exactly one.
  2. a b: both conditions are true and the IFs are independent.
  3. 500 itself (and 499 to confirm it fails).

Where marks are won and lost

Tracing questions give values and ask what a segment displays or what variable holds at the end. The classic trap is independent IF statements versus IF/ELSE. Writing questions ask which condition matches a description, or which segment is correct, so test boundaries. On the exam you may also be asked to say which test values would reach every block. In the Create task you must include selection in your own procedure and explain what it does. Before you commit to an answer, ask two questions: what happens exactly at the boundary value, and should exactly one block run or possibly several?

Back to the full study guide for worked examples and explanations.

AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.