Conditionals (College Board AP® Computer Science Principles): Study Guide

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Conditional execution & IF statements

What is conditional execution?

  • Conditional execution is a form of selection: a section of code runs only if a specified condition is true

  • Selection is one of the three core building blocks of all algorithms, alongside sequencing and iteration

  • Without selection, a program follows the same path regardless of input; conditionals allow programs to respond differently to different situations

IF statements

  • An IF statement evaluates a Boolean condition and executes the statements inside its block only if that condition is true

  • If the condition is false, the block is skipped and flow control passes to the next statement after the IF block

score ← 72

IF (score ≥ 50)
{
   DISPLAY("Pass")
}
 
  • If score is 72, the condition 72 ≥ 50 is true, so "Pass" is displayed

  • If score were 40, the condition would be false and nothing would be displayed

Branching & nested conditionals

What is branching logic?

  • An IF-ELSE statement creates two branches: one that executes when the condition is true, and one that executes when the condition is false

  • Exactly one branch always executes; unlike a plain IF statement, an IF-ELSE guarantees an outcome in both cases

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

Structure

When to use

Guarantee of output

IF

When an action is only needed if the condition is true

No guarantee — if the condition is false, nothing happens

IF-ELSE

When a different action is needed for both true and false cases

Always produces output — exactly one branch executes regardless of the condition

Nested conditionals

  • A nested conditional is an IF or IF-ELSE statement placed inside another IF or IF-ELSE block

  • Nested evaluation works from the outside in: the outer condition is checked first; the inner condition is only evaluated if the outer condition is true

  • Nested conditionals allow programs to test multiple conditions in sequence

IF (score ≥ 50)
{
   IF (score ≥ 90)
   {
      DISPLAY("Honors pass")
   }
   ELSE
   {
      DISPLAY("Pass")
   }
}
ELSE
{
   DISPLAY("Fail")
}
 
  • The inner conditions are only reached if the outer condition (score ≥ 50) is already true

  • A score of 95 displays "Honors pass", a score of 72 displays "Pass", and a score of 40 displays "Fail"

Examiner Tips and Tricks

  • Use IF when only one outcome needs code — the false case requires no action. Use IF-ELSE when both outcomes require different code. When tracing nested conditionals, evaluate the outermost condition first and only move inward if that condition is true.

  • For the AP Create Performance Task, your program must include selection — an IF or IF-ELSE statement with a meaningful condition that affects your program's behavior. Be prepared to explain why your condition is necessary and what would happen without it.

Worked Example

A program contains the following code segment.

x ← 15

IF (x > 10)
{
   IF (x > 20)
   {
      DISPLAY("Large")
   }
   ELSE
   {
      DISPLAY("Medium")
   }
}
ELSE
{
   DISPLAY("Small")
}
 

What is displayed when this code segment is executed?

(A) Large

(B) Medium

(C) Small

(D) Nothing is displayed

[1]

Answer:

(B) Medium [1 mark]

  • x > 10 is true so the inner conditional is reached; x > 20 is false so the ELSE branch executes and displays "Medium"

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.