Big Idea 3 — Algorithms and Programming
The heart of AP CSP: variables, lists, conditionals, loops, procedures, libraries, simulations, algorithm efficiency, and the limits of computing. This Big Idea is worth 30–35% of the AP exam and supports the Create Performance Task. Code uses AP exam pseudocode (lists start at index 1). Each section ends with a short practice set — click "Show answer" to check yourself.
Big Idea 2 — Data
How computers represent data with bits, how data is shrunk for storage and transmission, and how people and programs turn raw data into useful information. This Big Idea is worth 17–22% of the AP exam. Each section ends with a short practice set — click "Show answer" to check yourself.
Big Idea 1 — Creative Development
How computing innovations are made: working with others, understanding what a program does and why, following a design process, and finding and fixing mistakes. This Big Idea is worth 10–13% of the AP exam and also underpins the Create Performance Task. Each section ends with a short practice set — click "Show answer" to check yourself.
Variables and Assignments
A variable is an abstraction inside a program that holds a single value and has a name. The value can change as the program runs. An assignment evaluates the expression on the right, then stores the result in the variable on the left (written ← in AP pseudocode).
score ← 10 score ← score + 5 // evaluate 10 + 5, store 15 in score DISPLAY(score) // 15
- A variable holds only its most recent value; assigning a new value replaces the old one.
- Common data types: integer, real number, string, Boolean.
- Use meaningful names (
totalCostnotx) so code is easier to read. - Swapping two variables needs a temporary variable:
temp ← a, a ← b, b ← temp.
b ← a copies the current value of a into b. Later changes to a do not change b.1. After a ← 3, b ← a, a ← 5, what are a and b?
Show answer
a = 5 and b = 3. b kept the value copied earlier.2. What does x ← x + 1 do?
Show answer
Data Abstraction
Data abstraction hides the details of how data is stored so programmers can work with a simpler idea. A list is the main example: it stores many values under a single name.
// Without a list: score1, score2, score3 ... (hard to scale) scores ← [82, 91, 77, 68] // one name, many values
- Lists manage complexity — you don't need a separate variable for every item.
- Code using a list can handle any number of items without being rewritten, so it is easier to develop and maintain.
- Naming a collection (like
scores) gives it meaning, which makes the program easier to read.
1. Name one way a list manages program complexity.
Show answer
2. A program tracks 500 student names. Why is a list better than 500 variables?
Show answer
Mathematical Expressions
Expressions combine values, variables, and operators. AP pseudocode uses + − * / and MOD.
| Operator | Meaning | Example |
|---|---|---|
+ − * | Add, subtract, multiply | 2 + 3 * 4 → 14 |
/ | Division | 10 / 4 → 2.5 |
MOD | Remainder after division | 17 MOD 5 → 2 |
- Order of operations follows normal math: parentheses, then
* / MOD, then+ −, left to right. n MOD 2 = 0tests whether n is even.MODis also useful for wrap-around, like a clock or cycling through list positions.
a MOD b is always between 0 and b − 1 (for positive numbers).1. Evaluate (7 + 2) MOD 4.
Show answer
2. Evaluate 2 + 3 * 4 − 6 / 2.
Show answer
Strings
A string is an ordered sequence of characters (letters, digits, symbols, spaces). Strings are written in quotes.
- Length — the number of characters, including spaces.
- Concatenation — joining two strings end to end:
"Sun"and"shine"become"Sunshine". - Substring — a part of an existing string:
"com"is a substring of"computer". - String operations are available as procedures in most languages, though the exact names differ.
first ← "Ada" last ← "Lovelace" full ← first + " " + last // "Ada Lovelace" (concatenation)
"5" + "3" as strings gives "53", not 8.1. What is the length of "Hi there"?
Show answer
2. Concatenate "Data" and "base".
Show answer
"Database"Boolean Expressions
A Boolean value is true or false. Boolean expressions use relational operators and logical operators.
| Type | Operators |
|---|---|
| Relational | = ≠ > < ≥ ≤ |
| Logical | NOT, AND, OR |
| a | b | a AND b | a OR b | NOT a |
|---|---|---|---|---|
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |
1. If a = 5 and b = 10, evaluate (a < b) AND (b = 10).
Show answer
2. Evaluate NOT (a > b) OR (b < 5) when a = 5, b = 10.
Show answer
Conditionals
Selection lets a program choose which statements to run based on a Boolean condition. AP pseudocode uses IF and an optional ELSE.
IF (temp > 30)
{
DISPLAY("Hot")
}
ELSE
{
DISPLAY("Comfortable")
}
- If the condition is true, the
IFblock runs; otherwise theELSEblock runs (if present). - Without an
ELSE, nothing happens when the condition is false.
1. What is displayed if temp ← 25 in the code above?
Show answer
2. Write a conditional that displays "Even" if n is even.
Show answer
IF (n MOD 2 = 0) { DISPLAY("Even") }Nested Conditionals
A nested conditional places one conditional inside another, allowing more than two outcomes. AP pseudocode has no "else if", so extra choices go inside ELSE.
IF (grade ≥ 90)
{
DISPLAY("A")
}
ELSE
{
IF (grade ≥ 80)
{
DISPLAY("B")
}
ELSE
{
DISPLAY("C or below")
}
}
- The inner conditional only runs if the outer branch containing it is taken.
- Order matters: check the strictest condition first.
1. What is displayed for grade ← 95?
Show answer
2. What is displayed for grade ← 70?
Show answer
Iteration
Iteration repeats a block of statements. AP pseudocode has three forms:
| Form | Repeats |
|---|---|
REPEAT n TIMES | A fixed number of times |
REPEAT UNTIL (condition) | Until the condition becomes true (checked before each pass) |
FOR EACH item IN list | Once for every element of a list |
i ← 1
sum ← 0
REPEAT UNTIL (i > 4)
{
sum ← sum + i
i ← i + 1
}
DISPLAY(sum) // 10
- If the
REPEAT UNTILcondition is true at the start, the loop body never runs. - If the condition never becomes true, you get an infinite loop.
1. x ← 5, then REPEAT UNTIL (x ≥ 20) { x ← x + 5 }. How many times does the body run?
Show answer
2. What happens if a REPEAT UNTIL condition never becomes true?
Show answer
Developing Algorithms
An algorithm is a finite set of instructions that accomplishes a task. Every algorithm is built from sequencing, selection, and iteration.
- Algorithms can be described in natural language, flowcharts, or pseudocode, and then coded in any language.
- Different algorithms can solve the same problem, and they can differ in efficiency.
- New algorithms can be built by combining or modifying existing ones.
Common patterns to know: finding the maximum, summing/averaging, counting items that meet a condition, and linear search.
max ← nums[1]
FOR EACH n IN nums
{
IF (n > max)
{
max ← n
}
}
DISPLAY(max)
1. Name the three building blocks of every algorithm.
Show answer
2. Write pseudocode to count how many numbers in nums are above 50.
Show answer
count ← 0; FOR EACH n IN nums { IF (n > 50) { count ← count + 1 } }Lists
A list is an ordered sequence of elements. Each element has an index. In AP pseudocode, indexes start at 1.
| Operation | Effect |
|---|---|
list[i] | The element at index i |
list[i] ← value | Replace the element at i |
INSERT(list, i, value) | Add at i; later elements shift right |
APPEND(list, value) | Add at the end |
REMOVE(list, i) | Delete element at i; later elements shift left |
LENGTH(list) | Number of elements |
- Traversal visits each element — with
FOR EACHor a loop over indexes. - Linear (sequential) search checks elements one by one from the start; it works on any list, sorted or not.
- Using an index outside
1toLENGTH(list)causes an error.
1. list ← [10, 20, 30], then INSERT(list, 2, 15). What is list now?
Show answer
[10, 15, 20, 30]2. Then REMOVE(list, 1). What is list now?
Show answer
[15, 20, 30]Binary Search
Binary search finds a target in a sorted list by repeatedly checking the middle element and discarding the half that can't contain the target.
- Look at the middle element.
- If it equals the target, done.
- If the target is smaller, keep the lower half; if larger, keep the upper half.
- Repeat on the remaining half until found or empty.
- The list must be sorted first.
- Each step halves the search space, so it needs roughly log2 n steps — a list of 1024 items takes about 10 steps.
- For large sorted lists it is much faster than linear search, which may check all n elements.
1. Can binary search be used on [8, 3, 5, 1]?
Show answer
2. About how many comparisons does binary search need (worst case) for 1,024 sorted items?
Show answer
Calling Procedures
A procedure is a named group of programming instructions (also called a method or function). You call it to run its code.
- Parameters are the input variables in the procedure's definition; arguments are the actual values passed when calling.
- A procedure may return a value (
RETURN), ending the procedure and handing a result back. - When called, the program jumps to the procedure, runs it, then continues after the call.
PROCEDURE doubleIt(x)
{
RETURN (x * 2)
}
result ← doubleIt(7) // 7 is the argument → result is 14
1. What does doubleIt(10) return?
Show answer
2. In PROCEDURE greet(name), is name a parameter or an argument?
Show answer
Developing Procedures
Procedural abstraction lets you use a procedure without knowing how it works inside. It supports modularity: breaking a big program into smaller reusable parts.
- Procedures reduce repeated code — write once, call many times.
- Parameters generalize a procedure so it works with different inputs.
- Programmers can change the internal code of a procedure without changing the code that calls it, as long as it still does the same job.
- Good documentation states what a procedure does, its parameters, and what it returns.
- Procedures make programs easier to debug, test, and maintain.
PROCEDURE average(a, b, c)
{
RETURN ((a + b + c) / 3)
}
1. How does a procedure reduce repetition?
Show answer
2. What is procedural abstraction?
Show answer
Libraries
A library is a collection of procedures that can be used in programs. An API (application programming interface) describes how to use those procedures — their names, parameters, and what they return.
- Libraries save time because the code already exists and has been tested.
- You need to read the documentation to use a library correctly.
- Libraries and APIs let you build complex programs (graphics, maps, data analysis) quickly.
- Give credit and follow licensing when using other people's code.
1. What is the main benefit of using a library?
Show answer
2. What does API documentation tell a programmer?
Show answer
Random Values
Random values make a program behave differently each time. In AP pseudocode, RANDOM(a, b) returns an integer from a to b, inclusive, with each value equally likely.
roll ← RANDOM(1, 6) // 1, 2, 3, 4, 5 or 6
IF (RANDOM(1, 2) = 1)
{
DISPLAY("Heads")
}
ELSE
{
DISPLAY("Tails")
}
- Because the result is unpredictable, the same program can produce different output on different runs.
- Used in games, simulations, and testing.
1. How many possible outcomes does RANDOM(3, 7) have?
Show answer
2. How could you simulate rolling a 10-sided die?
Show answer
RANDOM(1, 10)Simulations
A simulation is a program model of a real-world object or event. It is an abstraction that includes some details and leaves out others.
- Used when real experiments are too dangerous, slow, expensive, or impossible (e.g. crash tests, weather, disease spread).
- Often use random values to mimic real unpredictability.
- Allow testing many scenarios by changing variables.
- Limitations: results depend on the simplifications and assumptions chosen, so a simulation may contain bias or miss real-world factors.
1. Why might engineers use a simulation instead of a real crash test?
Show answer
2. Give one limitation of simulations.
Show answer
Algorithmic Efficiency
Efficiency describes how many steps (or how much time or memory) an algorithm needs as the input size n grows.
| Growth | Examples | Time |
|---|---|---|
| Constant, linear (n), square (n²), cubic (n³) | Fixed step, linear search, nested loops | Reasonable (polynomial) |
| Exponential (2n), factorial (n!) | Trying every combination | Unreasonable — too slow for large n |
- A heuristic is an approach that finds a good-enough answer when finding the best one takes too long.
- A decision problem has a yes/no answer; an optimization problem asks for the best solution.
- Efficiency is often judged by counting steps, not by timing on one computer.
1. Is an algorithm that takes 2n steps reasonable for large n?
Show answer
2. When is a heuristic useful?
Show answer
Undecidable Problems
Some problems cannot be solved by any algorithm for every input.
- A decidable problem has an algorithm that always gives a correct yes/no answer for every input.
- An undecidable problem has no such algorithm — no program can solve it for all possible inputs.
- Famous example: the halting problem — deciding whether any given program will eventually stop or run forever.
- An undecidable problem may still be solvable for some specific inputs.
1. What does it mean for a problem to be undecidable?
Show answer
2. Give an example of an undecidable problem.