AP CSP Unit 3: Algorithms & Programming – Complete 2025 Study Guide

AP CSP Unit 3: Algorithms & Programming — Complete 2025 Study Guide

Unit 3 is the largest and most important part of the AP Computer Science Principles curriculum. It covers algorithms, variables, loops, conditionals, lists, functions, and general problem-solving — the foundation of programming.

What you’ll master in this guide:
  • Variables, expressions, and assignment
  • Conditionals (IF, ELSE IF, ELSE)
  • Loops (REPEAT, WHILE, FOR EACH)
  • Lists and data structures
  • Functions, parameters, and return values
  • Algorithmic patterns (traversal, accumulation, filtering)
  • Efficiency & problem-solving strategies
  • AP-style questions with explanations

🧮 Variables & Assignment

Variables store information that can change during a program. They allow algorithms to track state, update values, and compute results.

Key ideas:

  • Assignment uses ←, not equals
  • Variables update based on the sequence of instructions
  • The exam often tests variable tracing
score ← 0
score ← score + 5

The new value replaces the old value — variables do not “remember” past values.

AP Tip: Watch out for off-by-one errors when variables are updated inside loops.

🔁 Conditionals (IF Statements)

Conditionals let algorithms make decisions by evaluating Boolean expressions (true/false).

Boolean operators:

  • AND — both must be true
  • OR — at least one must be true
  • NOT — reverses true/false
IF (age ≥ 18)
{
    DISPLAY("Adult")
}
ELSE
{
    DISPLAY("Minor")
}

Only the first true condition runs — later branches are ignored.

Test Yourself: What is displayed?
age = 17 → “Minor”

🔄 Loops: REPEAT, WHILE, FOR EACH

Loops repeat instructions multiple times. Understanding loop flow is crucial for the AP CSP exam.

Loop types:

  • REPEAT n TIMES — fixed number of iterations
  • WHILE (condition) — repeats until condition becomes false
  • FOR EACH — iterates through all items in a list

Example:

counter ← 0
REPEAT 5 TIMES
{
    counter ← counter + 2
}

counter ends at 10.

AP Tip: The exam frequently tests nested loops or loops that update two variables at once.

📜 Lists: Storing Multiple Values

Lists are one of the most important data abstractions in AP CSP.

Common list operations:

  • APPEND(list, value)
  • INSERT(list, index, value)
  • REMOVE(list, index)
  • LENGTH(list)
  • list[i] — accessing an element

Traversal example:

sum ← 0
FOR EACH num IN numbers
{
    sum ← sum + num
}

Filtering example:

evens ← []
FOR EACH n IN nums
{
    IF (n MOD 2 = 0)
    {
        APPEND(evens, n)
    }
}
Key Skill: Understanding list + loop + condition combinations is essential for scoring high.

📦 Functions, Parameters, & Return Values

Functions allow code reuse and abstraction — hiding details so the user only focuses on what the function does.

Two kinds of functions:

  • Procedures — perform actions
  • Functions — return values
PROCEDURE Square(x)
{
    RETURN (x * x)
}

Many exam questions ask: “What value is returned?” — not displayed.

AP Tip: If a function uses RETURN, it immediately exits — statements after RETURN do not run.

🧠 Algorithm Patterns

Many AP CSP questions use the same algorithmic building blocks.

Key patterns:

  • Traversal — visit each element
  • Accumulation — track a running total
  • Filtering — build a new list based on a condition
  • Searching — look for a specific value
  • Mapping — transform each element
Exam Tip: If an algorithm processes every item in a list, the pattern is “traversal.”

⏱ Algorithm Efficiency

AP CSP does not require Big-O notation, but efficiency concepts still appear.

  • Fewer operations → more efficient
  • Loops inside loops (nested loops) are less efficient
  • Binary search is more efficient than linear search
  • Removing list items repeatedly can reduce efficiency
Key Idea: Efficiency compares algorithmic steps, not runtime on real hardware.

📝 AP Exam-Style Practice Questions

Question 1

What value is displayed by the following code?

x ← 2
y ← 1
REPEAT UNTIL (x > 10)
{
    x ← x * 2
    y ← y + 1
}
DISPLAY(y)

Question 2

What does this procedure return?

PROCEDURE Mystery(list)
{
    sum ← 0
    FOR EACH n IN list
    {
        IF (n > 5)
        {
            sum ← sum + n
        }
    }
    RETURN(sum)
}

Question 3

What is printed by this code?

count ← 0
nums ← [3, 6, 1, 8, 2]
FOR EACH n IN nums
{
    IF (n MOD 2 = 0)
    {
        count ← count + 1
    }
}
DISPLAY(count)

Question 4

What does this procedure return?

PROCEDURE DoubleEvens(list)
{
    result ← []
    FOR EACH x IN list
    {
        IF (x MOD 2 = 0)
        {
            APPEND(result, x * 2)
        }
    }
    RETURN(result)
}

Question 5

What value is returned?

PROCEDURE Mystery(x)
{
    IF (x < 10)
    {
        RETURN(x * 3)
    }
    RETURN(x - 4)
}

Need Help? Get AP CSP Tutoring

Work 1-on-1 with a certified AP CSP teacher to master the Internet, cybersecurity, routing, and protocols.

Learn About Tutoring

Contact form