Code below uses the AP CSP pseudocode – the exam's language-neutral reference. Assignment is written a ← expression, and list indices start at 1.
Algorithms and Programming
AP Computer Science Principles · Topic 3
3.1
Variables and Assignments
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-1 | AAP-1.A |
|
AAP-1.B |
|
Source: College Board AP Course and Exam Description
A variable 变量 is a named place that holds a value. The assignment 赋值 operator stores the value on the right into the variable on the left:
A variable is a named store whose value can change
a ← 5
b ← a + 3 // b is now 8
A variable holds one value at a time; assigning again replaces it. Variables let a program store input, remember results, and reuse them.
Watch a variable hold and change its value
A variable is a named box that stores one value at a time. An assignment copies a value into the box; assigning again overwrites whatever was there.
| English | Chinese | Pinyin |
|---|---|---|
| variable | 变量 | biàn liàng |
| assignment | 赋值 | fù zhí |
3.2
Data Abstraction
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-1 | AAP-1.C |
|
AAP-1.D |
|
Source: College Board AP Course and Exam Description
Data abstraction 数据抽象 lets you manage complexity by giving a single name to a collection of data – for example, a list rather than dozens of separate variables. It hides detail: you use the named collection without worrying about how it is stored. Lists (below) are the course's main data abstraction.
| English | Chinese | Pinyin |
|---|---|---|
| Data abstraction | 数据抽象 | shù jù chōu xiàng |
3.3
Mathematical Expressions
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-2 | AAP-2.A |
|
AAP-2.B |
| |
AAP-2.C |
|
Source: College Board AP Course and Exam Description
Programs compute with the operators +, -, *, /, and MOD (the remainder 余数 of a division, e.g. 17 MOD 5 is 2). Expressions follow the usual order of operations. MOD is especially useful for testing divisibility (n MOD 2 = 0 means n is even) and for wrapping values around a range.
Evaluate an expression step by step
An expression is evaluated with order of operations: multiplication and division happen before addition and subtraction, left to right.
| English | Chinese | Pinyin |
|---|---|---|
| remainder | 余数 | yú shù |
3.4
Strings
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-2 | AAP-2.D |
|
Source: College Board AP Course and Exam Description
A string 字符串 is an ordered sequence of characters, like "hello". Programs join strings (concatenation 拼接) and find their length. Strings represent text – names, messages, sequences – and are a common program input and output.
| English | Chinese | Pinyin |
|---|---|---|
| string | 字符串 | zì fú chuàn |
| concatenation | 拼接 | pīn jiē |
3.5
Boolean Expressions
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-2 | AAP-2.E |
|
AAP-2.F |
|
Source: College Board AP Course and Exam Description
A Boolean expression 布尔表达式 evaluates to true or false. It uses relational operators (=, ≠, <, >, ≤, ≥) and logical operators NOT, AND, OR:
The three families of operators: arithmetic, relational, and logical
NOTreverses a value,ANDis true only when both sides are true,ORis true when at least one side is true.
These conditions drive every decision and loop.
Try the OR truth table
A Boolean expression is either true (1) or false (0). OR is true when at least one input is true; flip the inputs to see every case.
| English | Chinese | Pinyin |
|---|---|---|
| Boolean expression | 布尔表达式 | bù ěr biǎo dá shì |
3.6
Conditionals
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-2 | AAP-2.G |
|
AAP-2.H |
|
Source: College Board AP Course and Exam Description
A conditional (selection) 条件语句 chooses which code to run. IF runs a block only when its condition is true; ELSE gives an alternative:
Selection chooses between paths based on a condition
IF (score ≥ 60)
{
DISPLAY("Pass")
}
ELSE
{
DISPLAY("Fail")
}
Follow an if / else decision
A conditional runs one branch or another depending on whether its condition is true. Slide the value across the threshold and watch which branch is taken.
| English | Chinese | Pinyin |
|---|---|---|
| conditional (selection) | 条件语句 | tiáo jiàn yǔ jù |
3.7
Nested Conditionals
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-2 | AAP-2.I |
|
Source: College Board AP Course and Exam Description
A nested conditional 嵌套条件 places one IF inside another (or chains ELSE IF) to choose among more than two paths. Only the first matching branch runs:
IF (g ≥ 90) { grade ← "A" }
ELSE IF (g ≥ 80) { grade ← "B" }
ELSE { grade ← "C" }
| English | Chinese | Pinyin |
|---|---|---|
| nested conditional | 嵌套条件 | qiàn tào tiáo jiàn |
3.8
Iteration
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-2 | AAP-2.J |
|
AAP-2.K |
|
Source: College Board AP Course and Exam Description
Iteration (a loop) 迭代 repeats instructions. AP pseudocode has two forms:
A pre-condition (WHILE) loop tests before the body, so it may run zero times
REPEAT 5 TIMES // a fixed count
{
DISPLAY("hi")
}
REPEAT UNTIL (found) // until a condition becomes true
{
...
}
A loop that never meets its stopping condition is an infinite loop 无限循环.
Trace a loop one pass at a time
A loop repeats a block while its counter runs through a range. Step through to watch the counter and the running total update each pass.
| English | Chinese | Pinyin |
|---|---|---|
| Iteration (a loop) | 迭代 | dié dài |
| infinite loop | 无限循环 | wú xiàn xún huán |
3.9
Developing Algorithms
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-2 | AAP-2.L |
|
AAP-2.M |
|
Source: College Board AP Course and Exam Description
An algorithm 算法 is a finite sequence of steps that solves a problem, built from sequencing, selection, and iteration. Different algorithms can solve the same problem, and you should be able to combine and modify existing algorithms (for example, count the values in a list that meet a condition, or find the largest). Trace an algorithm by hand to check it is correct.
A flowchart lays out an algorithm using the standard symbols
| English | Chinese | Pinyin |
|---|---|---|
| algorithm | 算法 | suàn fǎ |
3.10
Lists
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-2 | AAP-2.N |
|
AAP-2.O |
|
Source: College Board AP Course and Exam Description
A list 列表 is an ordered collection of values under one name, the course's key data abstraction. AP pseudocode indexes from 1:
A list holds many values in one variable, each found by its index
scores ← [88, 74, 95]
DISPLAY(scores[1]) // 88
scores[2] ← 80 // replace the 2nd value
APPEND(scores, 60) // add to the end
INSERT(scores, 1, 100) // insert at index 1
REMOVE(scores, 3) // delete the 3rd element
LENGTH(scores) // how many elements
Traverse a list with a loop to sum, count, search, or find a maximum:
FOR EACH x IN scores
{
total ← total + x
}
| English | Chinese | Pinyin |
|---|---|---|
| list | 列表 | liè biǎo |
3.11
Binary Search
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-2 | AAP-2.P |
|
Source: College Board AP Course and Exam Description
Binary search 二分搜索 finds a value in a sorted list far faster than checking each element. It looks at the middle element, then discards the half that cannot contain the target, repeating until found. Each step halves the search space, so a list of $n$ items takes about $\log_2 n$ steps. It requires the data to be sorted first.
Binary search halves the range at each step (the list must be sorted)
Worked example. Searching a sorted list of $8$ items, binary search halves the range each step: $8\rightarrow4\rightarrow2\rightarrow1$, at most $3$ comparisons ($\log_2 8=3$), whereas a linear search could take up to $8$. The advantage grows explosively: about $1{,}000$ items need only $\approx10$ binary-search steps (but up to $1{,}000$ linear ones), and $1{,}000{,}000$ items need just $\approx20$. Halving is what makes it a reasonable-time algorithm.
| English | Chinese | Pinyin |
|---|---|---|
| Binary search | 二分搜索 | èr fēn sōu suǒ |
3.12
Calling Procedures
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-3 | AAP-3.A |
|
Source: College Board AP Course and Exam Description
A procedure (function) 过程 is a named, reusable block of code. Calling it runs its code with the arguments you supply, and it may return a value:
sum ← Add(3, 4) // call, passing 3 and 4
Procedures let you use code without knowing its inner workings – procedural abstraction 过程抽象.
| English | Chinese | Pinyin |
|---|---|---|
| procedure (function) | 过程 | guò chéng |
| procedural abstraction | 过程抽象 | guò chéng chōu xiàng |
3.13
Developing Procedures
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-3 | AAP-3.B |
|
AAP-3.C |
|
Source: College Board AP Course and Exam Description
You define a procedure with a name, parameters (inputs), and a body, and optionally RETURN a result:
Decomposing a program into procedures and sub-procedures
PROCEDURE Add(a, b)
{
RETURN(a + b)
}
Writing your own procedures reduces repetition, breaks a big problem into named pieces, and makes programs readable and easier to test – the essence of abstraction 抽象.
| English | Chinese | Pinyin |
|---|---|---|
| abstraction | 抽象 | chōu xiàng |
3.14
Libraries
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-3 | AAP-3.D |
|
Source: College Board AP Course and Exam Description
A library 库 is a collection of ready-made procedures that others can reuse. An API (Application Program Interface) 应用程序接口 documents what each procedure does, its parameters, and its result – so you can use it without seeing its code. Libraries save time and let you build on existing, tested work.
| English | Chinese | Pinyin |
|---|---|---|
| library | 库 | kù |
| Interface | 应用程序接口 | yìng yòng chéng xù jiē kǒu |
3.15
Random Values
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-3 | AAP-3.E |
|
Source: College Board AP Course and Exam Description
RANDOM(a, b) returns a random integer from a to b (inclusive), letting a program produce unpredictable results – for games, sampling, or simulations. Each call may give a different value, so a program using randomness behaves differently each run.
3.16
Simulations
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-3 | AAP-3.F |
|
Source: College Board AP Course and Exam Description
A simulation 模拟 is a program that models a real-world process to study it safely and cheaply. Simulations simplify reality (they leave out detail) and often use randomness to imitate chance events. They let you test scenarios that would be too costly, slow, or dangerous in real life – but their results are only as good as their assumptions.
| English | Chinese | Pinyin |
|---|---|---|
| simulation | 模拟 | mó nǐ |
3.17
Algorithmic Efficiency
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-4 | AAP-4.A |
|
Source: College Board AP Course and Exam Description
Efficiency 效率 is how much time (or memory) an algorithm needs as its input grows. A reasonable-time algorithm's work grows like a polynomial of the input size (e.g. linear or quadratic); an unreasonable-time algorithm grows far faster (e.g. doubling with each added item), becoming impractical for large inputs. A faster algorithm can make a previously impossible problem solvable. Sometimes an exact answer takes too long, so a heuristic 启发式 – an approach that finds a good-enough answer quickly – is used instead.
How the running time of an algorithm grows with the input size n
| English | Chinese | Pinyin |
|---|---|---|
| Efficiency | 效率 | xiào lǜ |
| heuristic | 启发式 | qǐ fā shì |
3.18
Undecidable Problems
Syllabus
| Enduring Understanding | Learning Objective | Essential Knowledge |
|---|---|---|
AAP-4 | AAP-4.B |
|
Source: College Board AP Course and Exam Description
Some problems are undecidable 不可判定: no algorithm can solve every case of them with a correct yes/no answer. This is a fundamental limit of computing – not a matter of needing a faster computer, but a proof that no such algorithm can exist.
Exam skill: be able to determine a code segment's result by tracing it, compare two algorithms' efficiency (reasonable vs unreasonable time), and recognize procedural and data abstraction in a program.
| English | Chinese | Pinyin |
|---|---|---|
| undecidable | 不可判定 | bù kě pàn dìng |
3.18
Exam tips
- Know a variable is a named store for a value and trace how assignment updates it step by step.
- Read the AP pseudocode carefully —
a <- expressionassigns, and lists are 1-indexed on the exam reference sheet. - Distinguish a variable from a list (a collection accessed by index) and use list operations correctly.
- Evaluate expressions with the right precedence and boolean logic (
AND,OR,NOT). - Pick clear, meaningful variable names — the written tasks reward readable code.