| Study Guides
College Board · cb-cs-principles · AP Computer Science Principles · Algorithms and Programming · 18 min read · Updated 2026-05-07

Algorithms and Programming — AP CS Principles CSP Study Guide

For: AP CS Principles candidates sitting AP Computer Science Principles.

Covers: Core programming fundamentals including variables and data types, mathematical and Boolean expressions, algorithm design and program flow, iteration, conditionals, list operations, and procedural abstraction.

You should already know: No prior CS required.

A note on the practice questions: All worked questions in the "Practice Questions" section below are original problems written by us in the AP CS Principles style for educational use. They are not reproductions of past College Board papers and may differ in wording, numerical values, or context. Use them to practise the technique; cross-check with official College Board mark schemes for grading conventions.


1. What Is Algorithms and Programming?

Algorithms and Programming is the practice of designing finite, step-by-step problem-solving instructions (algorithms) and translating them into structured code that computers can execute to automate tasks, analyze data, and build functional tools. This topic makes up 30-35% of your AP CSP exam score, and all content is taught using language-agnostic pseudocode that maps to common real-world languages like Python and JavaScript. Common synonyms include computational thinking, coding fundamentals, and software development basics. Unlike informal instruction sets (like a recipe for baking cookies), valid algorithms have clear, unambiguous steps and guarantee termination after a fixed number of operations.

2. Variables and Types

A variable is a named storage location in a computer’s memory that holds a modifiable value you can access or update during program execution. When you declare a variable, you use the assignment operator to bind a value to a unique identifier (variable name). AP CSP uses dynamic typing, so you do not need to explicitly declare the data type of a variable when creating it, but you must understand the four core data types tested on the exam to avoid invalid operations:

  1. Number: Any integer or floating-point (decimal) value, e.g., , ,
  2. Boolean: Only two possible values: true or false
  3. String: A sequence of characters (letters, numbers, symbols, spaces) enclosed in quotes, e.g., "AP CSP", "2026"
  4. List: An ordered collection of values (can be mixed types) stored under a single name, e.g., [90, 85, 95, "pass"]

Worked Example

To store and update a student’s test score and name in AP pseudocode:

  1. Declare initial values: testScore ← 87, studentName ← "Mia"
  2. Update the score after a retake: testScore ← 92
  3. Invalid operation: testScore + studentName (you cannot add a number and a string, so this will throw an error)

Exam tip: Examiners frequently test valid variable naming rules: you cannot use spaces, special characters (!, @, # except underscores), or reserved words (like if, else) as identifiers.

3. Mathematical and Boolean expressions

Expressions combine variables, values, and operators to produce a single new value. The two core expression types tested on AP CSP are mathematical and Boolean.

Mathematical Expressions

These combine numbers and arithmetic operators to produce a new number value, following standard PEMDAS order of operations: Parentheses first, then Exponents, then Multiplication/Division/Modulus, then Addition/Subtraction. The modulus operator () returns the remainder of a division operation, e.g., .

Worked Math Example

Calculate the value of :

  1. Evaluate parentheses:
  2. Evaluate multiplication:
  3. Evaluate modulus: (3 fits 5 times into 16, leaving a remainder of 1)

Boolean Expressions

These evaluate to either true or false, and form the foundation of conditional program logic. They use two sets of operators:

  1. Relational operators: Compare two values of the same type: (equal to), (not equal to), (greater than), (less than), (greater than or equal to), (less than or equal to)
  2. Logical operators: Combine multiple Boolean expressions: AND (returns true only if both inputs are true), OR (returns true if at least one input is true), NOT (reverses the input value, e.g., NOT true = false)

Worked Boolean Example

Evaluate when and :

  1. Evaluate first expression:
  2. Evaluate second expression:
  3. Evaluate OR operator:

4. Algorithms and program flow

All valid algorithms are built from three basic control structures that define the order in which code runs:

  1. Sequence: The default flow, where instructions run top-to-bottom in the exact order they are written
  2. Selection: Runs different blocks of code depending on whether a Boolean condition is true or false
  3. Iteration: Repeats a block of code multiple times without rewriting it

Worked Algorithm Example

Sequential algorithm to calculate the average of three test scores:

  1. Input: , ,
  2. Calculate sum:
  3. Calculate average:
  4. Output:

Exam note: Examiners often ask you to identify invalid algorithms: any set of instructions that never terminates (infinite loop) or has ambiguous steps is not a valid algorithm.

5. Iteration, conditionals, lists

This section covers the three most heavily tested practical programming skills on the AP CSP exam.

Conditionals (Selection)

AP CSP uses IF, IF-ELSE, and nested IF statements to control selection logic:

  • IF statement: Runs the code block only if the condition is true
  • IF-ELSE statement: Runs one block if the condition is true, a separate block if false

Worked Conditional Example

Code to assign a letter grade based on test score:

IF testScore ≥ 90 {
 grade ← "A"
} ELSE IF testScore ≥ 80 {
 grade ← "B"
} ELSE {
 grade ← "C"
}

For , this returns grade = "B".

Iteration (Loops)

Two core loop types are tested:

  1. WHILE loop: Runs as long as a Boolean condition is true, stops when the condition becomes false
  2. FOR EACH loop: Runs once for every item in a list, no manual counter required

Worked Iteration Example

Print all numbers from 1 to 10 with a WHILE loop:

i ← 1
WHILE i ≤ 10 {
 DISPLAY(i)
 i ← i + 1
}

This outputs 1, 2, ..., 10, then stops when .

Lists

A list is an ordered collection of values indexed starting at 1 in AP CSP pseudocode (this is a critical difference from most real languages that use 0-based indexing, and a common exam trap). Core list operations:

  • Access element by index: listName[index]
  • Add element to end: APPEND(listName, value)
  • Insert element at specific position: INSERT(listName, index, value)
  • Remove element: REMOVE(listName, index)
  • Get list length: LENGTH(listName)

Worked List Example

Given the list testScores ← [87, 92, 78, 95]:

  1. Add a new score 89 to the end: APPEND(testScores, 89) → list becomes [87,92,78,95,89]
  2. Access the 3rd element: testScores[3] → returns

6. Procedural abstraction

Abstraction is the process of hiding complex implementation details and only exposing the functionality a user needs. Procedural abstraction (also called function or method abstraction) means writing a reusable block of code (called a procedure) that performs a specific task, and can be called anywhere in your program by name, without rewriting the code. A procedure can take input values called parameters, and can return a single output value.

Key benefits of procedural abstraction:

  • Reduces code length by eliminating duplicate code
  • Makes code easier to debug (you only need to fix a bug once in the procedure, not everywhere it is used)
  • Improves readability by breaking large programs into small, named blocks
  • Makes code reusable across multiple projects

Worked Procedure Example

Reusable procedure to calculate the average of any list of test scores:

PROCEDURE calculateAverage(scoreList) {
 sum ← 0
 FOR EACH score IN scoreList {
 sum ← sum + score
 }
 average ← sum / LENGTH(scoreList)
 RETURN average
}

To use the procedure:

classScores ← [87,92,78,95]
classAverage ← calculateAverage(classScores)

This returns , and you can call calculateAverage on any list of numbers anywhere in your program.

Exam tip: 20% of multiple-choice questions test abstraction concepts, so make sure you can identify when procedural abstraction is used correctly, and how to modify a procedure to add functionality.

7. Common Pitfalls (and how to avoid them)

  1. Wrong move: Using 0-based indexing for lists in AP CSP pseudocode. Why students do it: Most real programming languages like Python use 0-based indexing, so students mix up conventions. Correct move: Always remember AP CSP lists start at index 1, so the first element is list[1], not list[0].
  2. Wrong move: Forgetting to increment the counter variable in a WHILE loop, leading to an infinite loop. Why students do it: They focus on writing the loop body and forget to update the variable that controls the loop condition. Correct move: Always check that your WHILE loop has a line that modifies the condition variable so it will eventually become false.
  3. Wrong move: Writing invalid Boolean expressions like instead of . Why students do it: They transfer natural language syntax to code. Correct move: Every operand of a logical operator must be a complete, standalone Boolean expression.
  4. Wrong move: Modifying a list (adding/removing elements) while iterating over it with an index-based loop, leading to skipped elements or index errors. Why students do it: They don’t realize that modifying the list changes its length during iteration. Correct move: Use a FOR EACH loop when modifying list contents, or create a copy of the list to iterate over if you need index positions.
  5. Wrong move: Writing procedures that modify global variables (side effects) instead of returning a value. Why students do it: It feels easier to modify a global variable than to set up parameters and return values. Correct move: Use parameters and return values for procedures wherever possible, so they are reusable and don’t accidentally change other parts of your program.

8. Practice Questions (AP Computer Science Principles Style)

Question 1

Consider the following pseudocode:

x ← 5
y ← 3
z ← (x + y) * 2 MOD 4
DISPLAY(z)

What is the output of this code? A) 0 B) 1 C) 2 D) 3

Worked Solution

Step 1: Evaluate parentheses: Step 2: Evaluate multiplication: Step 3: Evaluate modulus: (4 fits exactly 4 times into 16, leaving no remainder) Correct answer: A


Question 2

Consider the list grades ← [82, 95, 76, 89, 91] and the following pseudocode:

count ← 0
FOR EACH g IN grades {
 IF g ≥ 90 {
 count ← count + 1
 }
}
DISPLAY(count)

What is the output? A) 1 B) 2 C) 3 D) 4

Worked Solution

Iterate over each grade in the list:

  1. : no change to count
  2. : count becomes 1
  3. : no change
  4. : no change
  5. : count becomes 2 Correct answer: B

Question 3

Which of the following best describes the primary benefit of procedural abstraction? A) It makes code run faster by reducing the number of operations executed B) It allows code to be reused across multiple parts of a program without rewriting C) It eliminates the need for comments in code D) It reduces the total number of variables needed in a program

Worked Solution

  • Option A is incorrect: Procedural abstraction does not change the number of operations executed, it only organizes existing code.
  • Option B is correct: The core benefit of procedural abstraction is reusability, readability, and simplified debugging.
  • Option C is incorrect: Abstraction does not replace comments; you still need to document what procedures do for other programmers.
  • Option D is incorrect: Procedures use parameters, which are additional variables, so they do not reduce total variable count. Correct answer: B

9. Quick Reference Cheatsheet

Concept AP CSP Pseudocode Syntax/Rules
Variable assignment variableName ← value
Core data types Number, Boolean (true/false), String, List
Arithmetic operators , , , , (remainder)
Relational operators , , , , ,
Logical operators AND (both true), OR (at least one true), NOT (reverse value)
List rules Index starts at 1, access: list[index]
List operations APPEND(list, value), INSERT(list, index, value), REMOVE(list, index), LENGTH(list)
Conditional IF (condition) { code block } ELSE { code block }
Loops WHILE (condition) { code block }, FOR EACH item IN list { code block }
Procedure PROCEDURE name(parameters) { code block RETURN value }

Key Exam Rules

  1. Order of operations: PEMDAS for math, inner parentheses first for Boolean expressions
  2. Valid algorithms must be finite, clear, and guaranteed to terminate
  3. Procedural abstraction hides implementation details to improve reusability and readability

10. What's Next

This topic is the foundation for all remaining AP CSP content: you will use these programming skills to analyze large datasets, build cybersecurity tools, and complete your Create Performance Task, which makes up 30% of your final exam score. In later units, you will apply iteration and list operations to sort and search datasets, use procedural abstraction to build modular programs for the Create Task, and analyze algorithm efficiency to compare different solutions to the same problem. Mastery of the concepts in this guide is non-negotiable for earning a 5 on the exam, as every question on the test relies on at least one of these core skills.

If you have any questions about variable types, Boolean logic, or procedure design, you can ask Ollie, our AI tutor, for personalized explanations and extra practice problems at any time. You can also find more AP CSP study guides, full-length practice exams, and Create Task resources on the homepage to help you prepare for your exam.

← Back to topic

Stuck on a specific question?
Snap a photo or paste your problem — Ollie (our AI tutor) walks through it step-by-step with diagrams.
Try Ollie free →