Code below uses the AP CSP pseudocode – the exam's language-neutral reference. Assignment is written a ← expression, and list indices start at 1.
算法与编程
AP 计算机科学原理 · 知识点 3
9:10
算法与编程
想象一本有一百万个名字的电话簿,你必须找到其中一个。逐个检查,你可能一整天都找不完。有一种方法可以在大约…内找到它
英语旁白 · 英中内嵌字幕
3.1
变量与赋值
教学大纲
持久理解 (AAP-1): 为了找到可推广问题的特定解,程序员以多种方式表示和组织数据。
学习目标 AAP-1.A: 用变量表示一个值。[技能 3.A]
- AAP-1.A.1 变量是程序内部的一种抽象,用于存储一个值。每个变量都关联着数据存储,一次只能表示一个值,但该值可以是一个列表或其他集合,进而包含多个值。
- AAP-1.A.2 使用具有意义的变量名有助于提高程序代码的可读性,并帮助理解变量所代表的值。
- AAP-1.A.3 某些编程语言提供类型来表示数据,并通过变量引用这些类型。这些类型包括数字、布尔值、列表和字符串。
- AAP-1.A.4 某些值更适合使用某一种数据类型进行表示,而不适合使用另一种。
学习目标 AAP-1.B: 根据赋值确定变量的值。[技能 4.B]
-
AAP-1.B.1 赋值运算符允许程序修改变量所表示的值。
-
AAP-1.B.2 考试参考页提供了“$\leftarrow$”运算符用于赋值。例如,
文本:
a ← expression块:
a ← expression会求值
expression,然后将结果的一个副本赋给变量a。 -
AAP-1.B.3 存储在变量中的值将是最近赋值的值。例如:
a ← 1b ← aa ← 2display(b)仍然显示
1。
来源:College Board AP 课程与考试说明
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 ← 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.
Worked example (a failed swap). What is displayed?
a ← 3
b ← 7
a ← b
b ← a
DISPLAY(a)
DISPLAY(b)
| Line | a |
b |
|---|---|---|
a ← 3 |
3 | – |
b ← 7 |
3 | 7 |
a ← b |
7 | 7 |
b ← a |
7 | 7 |
It displays 7 7: the old value of a was replaced before it was copied. A swap needs a third variable: temp ← a, a ← b, b ← temp.
观察变量保持并改变其值
变量是一个命名的容器,一次只存储一个值。赋值操作将值复制到该容器中;再次赋值会覆盖其中原有的内容。
| English | 中文 | 拼音 |
|---|---|---|
| variable/ˈveərɪəbl/ | 变量 | biàn liàng |
| assignment/əˈsaɪnmənt/ | 赋值 | fù zhí |
3.2
数据抽象
教学大纲
持久理解 (AAP-1): 为了找到可推广问题的特定解决方案,程序员以多种方式表示和组织数据。
学习目标 AAP-1.C: 使用变量表示列表或字符串。[技能 3.A]
-
AAP-1.C.1 列表是元素的有序序列。例如,
[value1, value2, value3, ...]描述了一个列表,其中
value1是第一个元素,value2是第二个元素,value3是第三个元素,以此类推。 -
AAP-1.C.2 元素是列表中分配了唯一索引的单个值。
-
AAP-1.C.3 索引是使用自然数引用列表或字符串中元素的常用方法。
-
AAP-1.C.4 字符串是字符的有序序列。
学习目标 AAP-1.D: 关于数据抽象: a. 使用列表存储多个元素来开发数据抽象。[技能 3.B] b. 解释如何使用数据抽象来管理程序代码中的复杂性。[技能 3.C]
-
AAP-1.D.1 数据抽象提供了数据类型的抽象属性与其表示的具体细节之间的分离。
-
AAP-1.D.2 数据抽象通过给一组数据命名而不引用表示的具体细节来管理程序中的复杂性。
-
AAP-1.D.3 可以使用列表创建数据抽象。
-
AAP-1.D.4 开发用于在程序中实现的数据抽象可以使程序更易于开发和维护。
-
AAP-1.D.5 数据抽象通常包含不同类型的元素。
-
AAP-1.D.6 使用列表允许多个相关项被视为单个值。根据编程语言的不同,列表被称为不同的名称,例如 数组。
- 排除说明 (EK AAP-1.D.6): 链表的使用不在本课程和 AP 考试的范围内。
-
AAP-1.D.7 考试参考表提供以下符号
[value1, value2, value3, ...]用于创建具有这些值的列表,使其分别作为第一、第二、第三等项目。例如,
-
文本格式:
aList ← [value1, value2, value3, ...]块格式:
aList ← value1, value2, value3创建一个新列表,该列表在索引
1、2、3和...处分别包含值value1、value2、value3和...,并将其赋值给aList。 -
文本格式:
aList ← []块格式:
aList ←(空)创建一个新空列表并将其赋值给
aList。 -
文本格式:
aList ← bList块格式:
aList ← bList将列表
bList的副本赋值给列表aList。例如,如果bList包含[20, 40, 60],则在赋值后aList也将包含[20, 40, 60]。
-
-
AAP-1.D.8 考试参考表描述了一种列表结构,其索引值为 1 到列表中的元素数量(含)。对于所有列表操作,如果列表索引小于 1 或大于列表长度,将产生错误消息且程序将终止。
来源:College Board AP 课程与考试说明
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 | 中文 | 拼音 |
|---|---|---|
| Data abstraction/ˈdeɪtə əbˈstrækʃn/ | 数据抽象 | shù jù chōu xiàng |
3.3
数学表达式
教学大纲
持久理解 (AAP-2): 程序中语句的排列和组合方式决定了计算结果。程序利用迭代和选择结构来表示重复并做出决策,以处理不同的输入值。
学习目标 AAP-2.A: 用非编程语言表达一个使用顺序结构的算法。[技能 2.A]
- AAP-2.A.1 算法 是一组有限指令,用于完成特定任务。
- AAP-2.A.2 除了可视化编程和文本编程外,算法还可以通过多种方式表达,例如自然语言、图表和伪代码。
- AAP-2.A.3 由程序执行的算法是通过编程语言实现的。
- AAP-2.A.4 每个算法都可以由顺序、选择和迭代的组合构建而成。
学习目标 AAP-2.B: 使用顺序代码语句表示分步的算法过程。[技能 2.B]
- AAP-2.B.1 顺序执行 是指按照代码语句给出的顺序依次应用算法的每一步。
- AAP-2.B.2 代码语句 是程序代码的一部分,用于表达要执行的操作。
- AAP-2.B.3 表达式 可以由值、变量、运算符或返回值的函数调用组成。
- AAP-2.B.4 表达式经过求值后产生单个值。
- AAP-2.B.5 表达式的求值遵循由编程语言定义的一套运算顺序。
- AAP-2.B.6 顺序语句按照其在代码段中出现的顺序执行。
- AAP-2.B.7 当使用编程语言表达算法时,清晰性和可读性是需要考虑的重要因素。
学习目标 AAP-2.C: 评估使用算术运算符的表达式。[技能 4.B]
-
AAP-2.C.1 算术运算符是大多数编程语言的一部分,包括加法、减法、乘法、除法和取模运算符。
-
AAP-2.C.2 考试参考表提供了
a MOD b,其结果为a除以b的余数。假设a是大于或等于0的整数,且b是大于0的整数。例如,17 MOD 5的结果为2。 -
AAP-2.C.3 考试参考表提供了算术运算符
+、-、*、/和MOD。文本与代码块:
a + ba - ba * ba / ba MOD b
这些运算符用于对
a和b进行算术运算。例如,17 / 5的结果为3.4。 -
AAP-2.C.4 数学中使用的运算顺序同样适用于表达式求值。
MOD运算符具有与*和/运算符相同的优先级。
来源:College Board AP 课程与考试说明
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.

Worked example. MOD has the same priority as * and /, so it is done before + and -:
| Expression | Steps | Value |
|---|---|---|
17 MOD 5 + 3 * 2 |
2 + 6 |
8 |
(17 + 3) MOD 5 |
20 MOD 5 |
0 |
472 MOD 10 |
remainder after dividing by 10 | 2, the last digit |
(5 + 4) MOD 7 |
day 5 plus 4 days, in a 7-day week | 2 |
3 MOD 8 |
8 goes 0 times, remainder 3 | 3 |
The last two rows show the wrap-around: day numbers 0 to 6 never go past 6. Note that / in AP pseudocode is ordinary division, so 7 / 2 is 3.5.
逐步求值表达式
表达式的求值遵循运算顺序:乘法和除法优先于加法和减法,从左到右进行。
| English | 中文 | 拼音 |
|---|---|---|
| remainder/rɪˈmeɪndə/ | 余数 | yú shù |
3.4
字符串
教学大纲
持久理解 (AAP-2): 程序中语句的排列和组合方式决定了计算结果。程序通过包含迭代和选择结构来表示重复行为,并做出决策以处理不同的输入值。
学习目标 AAP-2.D: 评估操作字符串的表达式。[技能 4.B]
- AAP-2.D.1 字符串连接 将两个或更多字符串首尾相连,从而生成一个新字符串。
- AAP-2.D.2 子串 是现有字符串的一部分。
来源:College Board AP 课程与考试说明
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.

The exam reference sheet has no string procedures, so a question defines the ones it uses, for example concat(str1, str2), which joins two strings, and substring(str, start, length), which returns length characters of str starting at position start.
Worked example. With those definitions and word ← "computer":
substring(word, 4, 3)starts at character 4,p, and takes 3 characters:"put".substring(word, 1, 1)is"c".concat(substring(word, 1, 3), "et")isconcat("com", "et"), which is"comet".
Positions start at 1, so the last character of a string s is at position LENGTH(s); but always read the question's own definition first.
| English | 中文 | 拼音 |
|---|---|---|
| string/strɪŋ/ | 字符串 | zì fú chuàn |
| concatenation/kənˌkætəˈneɪʃn/ | 拼接 | pīn jiē |
3.5
布尔表达式
教学大纲
持久性理解 (AAP-2): 程序中语句的顺序和组合方式决定了计算结果。程序利用迭代和选择结构来表示重复操作,并做出决策以处理不同的输入值。
学习目标 AAP-2.E: 针对两个变量、表达式或值之间的关系: a. 使用关系运算符编写表达式。[技能 2.B] b. 求值使用关系运算符的表达式。[技能 4.B]
-
AAP-2.E.1 布尔值 要么是 true(真),要么是 false(假)。
-
AAP-2.E.2 考试参考表提供了以下关系运算符:
=,≠,>,<,≥, 和≤。文本和代码块:
a = ba ≠ ba > ba < ba ≥ ba ≤ b
这些运算符用于测试两个变量、表达式或值之间的关系。使用关系运算符进行的比较会求值为一个布尔值。例如,如果
a和b相等,则a = b求值为true;否则,它求值为false。
学习目标 AAP-2.F: 针对布尔值之间的关系: a. 使用逻辑运算符编写表达式。[技能 2.B] b. 求值使用逻辑运算符的表达式。[技能 4.B]
-
AAP-2.F.1 考试参考表提供了逻辑运算符
NOT、AND和OR,它们求值为一个布尔值。 -
AAP-2.F.2 考试参考表提供了
文本:
NOT condition代码块:
NOT condition如果
condition为false,则该表达式求值为true;否则求值为false。 -
AAP-2.F.3 考试参考表提供了
文本:
condition1 AND condition2代码块:
condition1 AND condition2如果
condition1和condition2均为true,则该表达式求值为true;否则求值为false。 -
AAP-2.F.4 考试参考表提供了
文本:
condition1 OR condition2代码块:
condition1 OR condition2如果
condition1为true,或者condition2为true,或者condition1和condition2均为true,则该表达式求值为true;否则求值为false。 -
AAP-2.F.5 逻辑运算符的操作数要么是一个布尔表达式,要么是一个单独的布尔值。
来源:College Board AP 课程与考试说明
A Boolean expression 布尔表达式 evaluates to true or false. It uses relational operators (=, ≠, <, >, ≤, ≥) and logical operators NOT, AND, OR:

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.
Worked example. With age ← 15 and member ← false, evaluate (age ≥ 13 AND age ≤ 19) AND NOT member:
age ≥ 13is true;age ≤ 19is true; so the bracket is true.NOT memberisNOT false, true.true AND trueis true.
p |
q |
p AND q |
p OR q |
NOT p |
|---|---|---|---|---|
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |
The opposite of x > 5 is x ≤ 5, not x < 5: the value 5 itself belongs to the opposite.
尝试 OR 真值表
布尔表达式要么为真(1),要么为假(0)。当至少有一个输入为真时,或运算的结果为真;翻转输入以查看所有情况。
| English | 中文 | 拼音 |
|---|---|---|
| Boolean expression/ˈbuːlɪən ekˈspreʃn/ | 布尔表达式 | bù ěr biǎo dá shì |
3.6
条件语句
教学大纲
持久性理解 (AAP-2): 程序中语句的序列化和组合方式决定了计算结果。程序通过包含迭代和选择结构来表示重复,并做出决策以处理不同的输入值。
学习目标 AAP-2.G: 在不使用编程语言的情况下,表达一个使用选择的算法。[技能 2.A]
- AAP-2.G.1 选择根据条件是否为
true或false来决定执行算法的哪些部分。
学习目标 AAP-2.H: 对于选择: a. 编写条件语句。[技能 2.B] b. 确定条件语句的结果。[技能 4.B]
-
AAP-2.H.1 条件语句(即“if语句”)通过根据布尔表达式的值执行不同的语句来影响顺序控制流。
-
AAP-2.H.2 考试参考表提供了
文本格式:
IF(condition){<block of statements>}块格式:
IF conditionblock of statements其中,如果布尔表达式
condition的计算结果为true,则执行block of statements中的代码;如果condition的计算结果为false,则不采取任何操作。 -
AAP-2.H.3 考试参考表提供了
文本格式:
IF(condition){<first block of statements>}ELSE{<second block of statements>}块格式:
IF conditionfirst block of statementsELSEsecond block of statements其中,如果布尔表达式
condition的计算结果为true,则执行first block of statements中的代码;否则,执行second block of statements中的代码。
来源:College Board AP 课程与考试说明
A conditional (selection) 条件语句 chooses which code to run. IF runs a block only when its condition is true; ELSE gives an alternative:

IF (score ≥ 60)
{
DISPLAY("Pass")
}
ELSE
{
DISPLAY("Fail")
}
Worked example. Two separate IFs are not the same as IF … ELSE. For n ← 12:
IF (n > 10)
{
n ← n - 5
}
IF (n < 10)
{
n ← n * 2
}
DISPLAY(n)
The first test is true, so n becomes 7; the second IF then tests the new value, 7 < 10, which is true, so n becomes 14. Written with ELSE, only the first branch would run and the program would display 7.
遵循 if / else 决策
条件语句会根据其条件是否为真来执行其中一个分支。将值滑过阈值,观察会执行哪个分支。
| English | 中文 | 拼音 |
|---|---|---|
| conditional (selection)/kənˈdɪʃənl/ | 条件语句 | tiáo jiàn yǔ jù |
3.7
嵌套条件语句
教学大纲
持久理解 (AAP-2): 程序中语句的排列顺序和组合方式决定了计算结果。程序通过包含迭代和选择结构来表示重复操作并做出决策,从而处理不同的输入值。
学习目标 AAP-2.I: 针对嵌套选择: a. 编写嵌套条件语句。[技能 2.B] b. 确定嵌套条件语句的计算结果。[技能 4.B]
- AAP-2.I.1 嵌套条件语句是指在条件语句内部包含其他条件语句的结构。
来源:College Board AP 课程与考试说明
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 | 中文 | 拼音 |
|---|---|---|
| nested conditional/ˈnestɪd kənˈdɪʃənl/ | 嵌套条件 | qiàn tào tiáo jiàn |
3.8
迭代
教学大纲
持久性理解 (AAP-2): 程序中语句的序列化和组合方式决定了计算结果。程序通过引入迭代和选择结构来表示重复操作,并做出决策以处理不同的输入值。
学习目标 AAP-2.J: 不使用编程语言来表达使用迭代的算法。[技能 2.A]
- AAP-2.J.1 迭代是算法中重复执行的部分。迭代会重复执行指定次数,或者直到满足给定条件为止。
学习目标 AAP-2.K: 关于迭代: a. 编写迭代语句。[技能 2.B] b. 确定迭代语句的结果或副作用。[技能 4.B]
-
AAP-2.K.1 迭代语句通过零次或多次重复一组语句来改变顺序控制流,直到满足停止条件。
-
AAP-2.K.2 考试参考表提供了
文本:
REPEAT n TIMES{<block of statements>}块:
REPEAT n TIMESblock of statements其中
block of statements被执行n次。 -
AAP-2.K.3 考试参考表提供了
文本:
REPEAT UNTIL(condition){<block of statements>}块:
REPEAT UNTIL conditionblock of statements其中
block of statements中的代码会重复执行,直到布尔表达式condition求值为true。 -
AAP-2.K.4 在
REPEAT UNTIL(condition)迭代中,如果结束条件永远无法求值为true,则会发生无限循环。 -
AAP-2.K.5 在
REPEAT UNTIL(condition)迭代中,如果条件初始时求值为true,由于条件是在循环体之前检查的,因此循环体根本不会执行。
来源:College Board AP 课程与考试说明
Iteration (a loop) 迭代 repeats instructions. AP pseudocode has two forms:

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 无限循环.
REPEAT UNTIL (condition) checks the condition before each pass and stops as soon as it is true; if it is already true at the start, the body runs zero times.
Worked example. What is displayed, and how many times does the body run?
n ← 1
REPEAT UNTIL (n > 20)
{
n ← n * 2
}
DISPLAY(n)
| Pass | n before |
n after |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 3 | 4 | 8 |
| 4 | 8 | 16 |
| 5 | 16 | 32 |
After pass 5, n > 20 is true, so the loop stops: the body ran 5 times and the program displays 32. If the body were n ← n * 1, n would stay 1 and the loop would never end.
逐次追踪一个循环
循环在其计数器遍历某个范围时重复执行一个代码块。逐步执行以观察计数器和运行总和在每次迭代中的更新情况。
| English | 中文 | 拼音 |
|---|---|---|
| Iteration (a loop)/ˌɪtəˈreɪʃn/ | 迭代 | dié dài |
| infinite loop/ˈɪnfɪnət luːp/ | 无限循环 | wú xiàn xún huán |
3.9
开发算法
教学大纲
持久理解 (AAP-2): 程序中语句的顺序和组合方式决定了计算结果。程序通过包含迭代和选择结构来表示重复操作,并做出决策以处理不同的输入值。
学习目标 AAP-2.L: 比较多种算法,判断它们是否产生相同的副作用或结果。[技能 1.D]
- AAP-2.L.1 算法可以用不同的方式编写,但仍能完成相同的任务。
- AAP-2.L.2 看起来相似的算法可能产生不同的副作用或结果。
- AAP-2.L.3 某些条件语句可以写成等价的布尔表达式。
- AAP-2.L.4 某些布尔表达式可以写成等价的条件语句。
- AAP-2.L.5 可以通过开发或使用不同的算法来解决同一个问题。
学习目标 AAP-2.M: 对于算法: a. 创建算法。[技能 2.A] b. 组合和修改现有算法。[技能 2.B]
- AAP-2.M.1 可以从一个想法出发创建算法,也可以通过组合现有算法或修改现有算法来创建。
- AAP-2.M.2 了解现有算法有助于构建新的算法。一些常见的现有算法包括:
- 确定两个或多个数字中的最大值或最小值
- 计算两个或多个数字的总和或平均值
- 识别一个整数是否能被另一个整数整除
- 确定机器人在迷宫中的路径
- AAP-2.M.3 将正确的现有算法作为构建新算法的模块具有以下优点,例如缩短开发时间、减少测试工作量以及简化错误识别。
来源:College Board AP 课程与考试说明
An algorithm is not the same thing as code. Beyond visual and textual programming languages, an algorithm can be expressed in a variety of ways: in natural language (ordinary sentences), as a diagram such as a flowchart, or in pseudocode. Those forms are for people — they let you check the logic and agree on it before any language is chosen, and the same algorithm can then be written in any language.

When you do write it in a programming language, clarity and readability are important considerations, not decoration: meaningful variable names, consistent indentation and comments explaining why rather than what. The program has to be read and modified later by someone — often you — and an algorithm nobody can follow cannot be maintained or debugged.
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.

| English | 中文 | 拼音 |
|---|---|---|
| algorithm/ˈælɡərɪθəm/ | 算法 | suàn fǎ |
3.10
列表
教学大纲
持久理解 (AAP-2): 程序中语句的序列和组合方式决定了计算结果。程序包含迭代和选择结构,以表示重复操作并做出决策来处理不同的输入值。
学习目标 AAP-2.N: 关于列表操作: a. 编写使用列表索引和列表过程(procedures)的表达式。[技能 2.B] b. 评估使用列表索引和列表过程的表达式。[技能 4.B]
- AAP-2.N.1 考试参考表提供了列表的基本操作,包括:
-
通过索引访问元素
文本:
aList[i]块状:
aList i访问
aList中索引为i的元素。aList的第一个元素位于索引1,使用记法aList[1]进行访问。 -
将列表元素的值赋给变量
文本:
x ← aList[i]块状:
x ← aList i将
aList[i]的值赋给变量x。 -
将值赋给列表的元素
文本:
aList[i] ← x块状:
aList i ← x将
x的值赋给aList[i]。文本:
aList[i] ← aList[j]块状:
aList i ← aList j将
aList[j]的值赋给aList[i]。 -
在给定索引处插入元素
文本:
INSERT(aList, i, value)块状:
INSERT aList, i, value将
aList中索引大于或等于i的所有值向右移动。列表长度增加 1,并将value放置在aList的索引i处。 -
向列表末尾添加元素
文本:
APPEND(aList, value)块状:
APPEND aList, value将
aList的长度增加 1,并将value放置在aList的末尾。 -
移除元素
文本:
REMOVE(aList, i)块状:
REMOVE aList, i移除
aList中索引i处的项目,并将索引大于i的所有值向左移动。aList的长度减少 1。 -
确定列表的长度
文本:
LENGTH(aList)块状:
LENGTH aList求值结果为当前
aList中的元素数量。
-
- AAP-2.N.2 列表过程按照编程语言的语法规则实现。
学习目标 AAP-2.O: 关于涉及列表元素的算法: a. 编写用于遍历列表的迭代语句。[技能 2.B] b. 确定包含列表遍历的算法的结果。[技能 4.B]
-
AAP-2.O.1 遍历列表可以是完全遍历(complete traversal),即访问列表中的所有元素;也可以是部分遍历(partial traversal),即仅访问一部分元素。
- 排除说明 (EK AAP-2.O.1): 使用相同索引同时遍历多个列表(并行遍历)超出了本课程及 AP 考试的范畴。
-
AAP-2.O.2 可以使用迭代语句来遍历列表。
-
AAP-2.O.3 考试参考表提供了
文本:
FOR EACH item IN aList{<block of statements>}块状:
FOR EACH item IN aListblock of statements变量
item按顺序从第一个元素到最后一个元素依次被赋予aList中每个元素的值。block of statements中的代码针对每次对item的赋值执行一次。 -
AAP-2.O.4 了解现有的使用迭代的算法有助于构建新算法。经常与列表结合使用的现有算法的一些示例包括:
- 确定列表中的最小值或最大值
- 计算数字列表的总和或平均值
-
AAP-2.O.5 线性搜索(linear search)或顺序搜索(sequential search)算法按顺序检查列表中的每个元素,直到找到所需的值或已检查完列表中的所有元素为止。
来源:College Board AP 课程与考试说明
A list 列表 is an ordered collection of values under one name, the course's key data abstraction. AP pseudocode indexes from 1:

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
}
Worked example (tracing a list traversal). This procedure should count the scores of at least 80:
count ← 0
FOR EACH x IN [88, 74, 95, 80]
{
IF (x ≥ 80)
{
count ← count + 1
}
}
DISPLAY(count)
x |
x ≥ 80? |
count after |
|---|---|---|
| 88 | true | 1 |
| 74 | false | 1 |
| 95 | true | 2 |
| 80 | true | 3 |
It displays 3. Two traps the exam sets: delete count ← 0 and count has no starting value, so the result is undefined; write x > 80 and the last score is missed, so it displays 2. Trace the boundary value every time.
| English | 中文 | 拼音 |
|---|---|---|
| list/lɪst/ | 列表 | liè biǎo |
3.11
二分查找
教学大纲
持久理解 (AAP-2): 程序中语句的排序和组合方式决定了计算结果。程序利用迭代和选择结构来表示重复操作,并做出决策以处理不同的输入值。
学习目标 AAP-2.P: 针对二分搜索算法: a. 确定在数据集中查找一个值所需的迭代次数。[技能 1.D] b. 解释完成二分搜索所必需的条件。[技能 1.A]
- AAP-2.P.1 二分搜索算法从已排序数字数据集的中间开始,并排除一半的数据;该过程会重复进行,直到找到目标值或所有元素都被排除。
- 排除说明 (EK AAP-2.P.1): 二分搜索的具体实现不属于课程范围和 AP 考试范围。
- AAP-2.P.2 必须按已排序的顺序排列数据才能使用二分搜索算法。
- AAP-2.P.3 当应用于已排序数据时,二分搜索通常比顺序/线性搜索更高效。
来源:College Board AP 课程与考试说明
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.


Worked example. Searching a sorted list of $8$ items, binary search halves the range each step: $8\rightarrow4\rightarrow2\rightarrow1$ in three halvings, and the one item left must still be checked, so at most $4$ comparisons ($\log_2 8+1$), 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 | 中文 | 拼音 |
|---|---|---|
| Binary search/ˈbaɪnəri sɜːtʃ/ | 二分搜索 | èr fēn sōu suǒ |
3.12
调用过程
教学大纲
持久性理解 (AAP-3): 程序员将问题分解为更小、更易管理的部分。通过创建过程并利用参数,程序员可以概括可重用的处理流程。过程允许程序员利用已经过测试的现有代码,从而更快速、更自信地编写程序。
学习目标 AAP-3.A: 针对过程调用: a. 编写语句来调用过程。[技能 3.B] b. 确定过程调用的结果或影响。[技能 4.B]
-
AAP-3.A.1 过程是一组具有名称的编程指令,可能包含参数和返回值。
-
AAP-3.A.2 根据编程语言的不同,过程被称为不同的名称,如方法或函数。
-
AAP-3.A.3 参数是过程的输入变量。实参指定在调用过程时参数的值。
-
AAP-3.A.4 过程调用会中断语句的顺序执行,导致程序在执行完过程内的语句后继续运行。一旦执行了过程中的最后一条语句(或返回语句),控制流将返回到调用过程的位置之后的点。
-
AAP-3.A.5 考试参考表提供了
procName(arg1, arg2, ...)作为调用
Text:
PROCEDURE procName(parameter1, parameter2, ...){<block of statements>}Block:
PROCEDURE procName parameter1, parameter2,...block of statements的方式;它接受零个或多个实参;
arg1被赋值给parameter1,arg2被赋值给parameter2,依此类推。 -
AAP-3.A.6 考试参考表提供了过程
Text:
DISPLAY(expression)Block:
DISPLAY expression用于显示
expression的值,后跟一个空格。 -
AAP-3.A.7 考试参考表提供了
Text:
RETURN(expression)Block:
RETURN expression语句,该语句用于将控制流返回到调用过程的位置,并返回
expression的值。 -
AAP-3.A.8 考试参考表提供了
result ← procName(arg1, arg2, ...)以将调用
Text:
PROCEDURE procName(parameter1, parameter2, ...){<block of statements>RETURN(expression)}Block:
PROCEDURE procName parameter1, parameter2,...block of statementsRETURN expression所返回的“过程的值”赋值给
result。 -
AAP-3.A.9 考试参考表提供了过程
Text:
INPUT()Block:
INPUT该过程接受来自用户的值并返回输入值。
来源:College Board AP 课程与考试说明
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 过程抽象.
Worked example. What is displayed?
PROCEDURE Mystery(x, y)
{
IF (x > y)
{
RETURN(x - y)
}
RETURN(y - x)
}
DISPLAY(Mystery(3, 10))
DISPLAY(Mystery(Mystery(9, 4), 2))
Mystery(3, 10):3 > 10is false, so it returns10 - 3, which is7.- The inner call
Mystery(9, 4)returns5; thenMystery(5, 2)returns3.
It displays 7 3. The procedure returns the distance between two numbers. RETURN ends the procedure at once, so the second RETURN runs only when the IF was false.

| English | 中文 | 拼音 |
|---|---|---|
| procedure (function)/prəˈsiːdʒə/ | 过程 | guò chéng |
| procedural abstraction/prəˈsiːdʒərəl əbˈstrækʃn/ | 过程抽象 | guò chéng chōu xiàng |
3.13
开发过程
教学大纲
持久性理解 (AAP-3): 程序员将问题分解为更小、更易管理的部分。通过创建过程并利用参数,程序员可以概括可重用的处理流程。过程使程序员能够利用已经测试过的现有代码,从而让他们更快速、更自信地编写程序。
学习目标 AAP-3.B: 解释如何使用过程抽象来管理程序中的复杂性。[技能 3.C]
- AAP-3.B.1 一种常见的抽象类型是过程抽象,它为某个处理过程提供一个名称,并允许使用者仅知道该过程做什么,而不必知道它是如何做的,即可使用该过程。
- AAP-3.B.2 过程抽象使得大问题的解决方案可以基于较小子问题的解决方案。这是通过创建过程来解决每个子问题来实现的。
- AAP-3.B.3 将计算机程序划分为单独子程序的过程称为模块化。
- AAP-3.B.4 过程抽象可以提取共享特性以概括功能,而不是复制代码。这实现了程序代码的重用,有助于管理复杂性。
- AAP-3.B.5 使用参数可以使过程通用化,从而使过程能够在一系列输入值或参数下被重用。
- AAP-3.B.6 使用过程抽象有助于提高代码的可读性。
- AAP-3.B.7 在程序中使用过程抽象允许程序员更改过程的内部实现(例如使其更快、更高效、占用更少存储空间等),只要过程的功能保持不变,就无需通知用户这些更改。
学习目标 AAP-3.C: 通过编写过程来开发过程抽象,以管理程序中的复杂性。[技能 3.B]
-
AAP-3.C.1 考试参考表提供了
文本:
PROCEDURE procName(parameter1, parameter2, ...){<block of statements>}块:
PROCEDURE procName parameter1, parameter2,...block of statements用于定义一个接受零个或多个参数的过程。该过程包含
block of statements。 -
AAP-3.C.2 考试参考表提供了
文本:
PROCEDURE procName(parameter1, parameter2, ...){<block of statements>RETURN(expression)}块:
PROCEDURE procName parameter1, parameter2,...block of statementsRETURN expression用于定义一个接受零个或多个参数的过程。该过程包含
block of statements并返回expression的值。RETURN语句可以出现在过程中的任何位置,并导致立即从过程返回到调用语句。
来源:College Board AP 课程与考试说明
You define a procedure with a name, parameters (inputs), and a body, and optionally RETURN a result:

PROCEDURE Add(a, b)
{
RETURN(a + b)
}
Worked example. Write a procedure CountAbove(list, limit) that returns how many values in list are greater than limit.
PROCEDURE CountAbove(list, limit)
{
count ← 0
FOR EACH item IN list
{
IF (item > limit)
{
count ← count + 1
}
}
RETURN(count)
}
CountAbove([4, 9, 12, 7], 6) returns 3. The parameters make it general: the same procedure works for any list and any limit, which is the point of a procedure. Displaying count inside the loop, or forgetting RETURN, are the usual mistakes.
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 | 中文 | 拼音 |
|---|---|---|
| abstraction/əbˈstrækʃn/ | 抽象 | chōu xiàng |
3.14
库
教学大纲
持久理解 (AAP-3): 程序员将问题分解为更小、更易管理的部分。通过创建过程和利用参数,程序员可以概括那些可重用的过程。过程使程序员能够调用已经过测试的现有代码,从而让他们更快速、更有信心地编写程序。
学习目标 AAP-3.D: 选择适当的库或现有代码段以用于创建新程序。[技能 2.B]
- AAP-3.D.1 软件库包含可用于创建新程序的过程。
- AAP-3.D.2 现有代码段可能来自内部或外部来源,例如库或先前编写的代码。
- AAP-3.D.3 使用库简化了创建复杂程序的任务。
- AAP-3.D.4 应用程序编程接口(API)是规定库中过程的行为和使用方式规范。
- AAP-3.D.5 API/库的文档对于理解其提供的行为以及如何使用该 API/库是必要的。
来源:College Board AP 课程与考试说明
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.
The documentation is part of the library. Documentation for an API or library is necessary in order to understand the behaviours it provides and how to use them — what each procedure expects as parameters, what it returns, and what it does at the edges. Without it you would have to read the source, which defeats the point of abstraction; with it you can use a procedure correctly without knowing how it works inside.

| English | 中文 | 拼音 |
|---|---|---|
| library/ˈlaɪbrəri/ | 库 | kù |
| Interface/ˈɪntəfeɪs/ | 应用程序接口 | yìng yòng chéng xù jiē kǒu |
3.15
随机值
教学大纲
持久理解 (AAP-3): 程序员将问题分解为更小、更易管理的部分。通过创建过程并利用参数,程序员可以概括那些可重复使用的流程。过程使程序员能够利用已经过测试的现有代码,从而更快速、更有信心地编写程序。
学习目标 AAP-3.E: 关于生成随机值: a. 编写表达式以生成可能的值。[技能 2.B] b. 评估表达式以确定可能的结果。[技能 4.B]
-
AAP-3.E.1 考试参考表提供
文本格式:
RANDOM(a, b)块格式:
RANDOM a, b该函数生成并返回一个从
a到b(含首尾)的随机整数。每个结果出现的可能性均等。例如,RANDOM(1, 3)可能返回1、2或3。 -
AAP-3.E.2 在程序中使用随机数生成意味着每次执行都可能产生不同的结果。
来源:College Board AP 课程与考试说明
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.
Worked example. A game gives a prize 30% of the time:
IF (RANDOM(1, 10) ≤ 3)
{
DISPLAY("Prize!")
}
RANDOM(1, 10) has 10 equally likely values; 1, 2 and 3 are 3 of them, so the chance is $\frac{3}{10} = 30\%$. RANDOM(0, 10) ≤ 3 would be wrong: it has 11 values, 4 of them at most 3. To list every possible output of a program with RANDOM, try the smallest and largest values the call can return.

3.16
模拟
教学大纲
持久理解 (AAP-3): 程序员将问题分解为更小、更易管理的部分。通过创建过程和利用参数,程序员可以概括出可重用的过程。过程使程序员能够调用已经过测试的现有代码,从而更快速、更自信地编写程序。
学习目标 AAP-3.F: 针对模拟: a. 解释计算机如何用于表示现实世界的现象或结果。[技能 1.A] b. 比较模拟与现实世界的背景。[技能 1.D]
- AAP-3.F.1 模拟是为了特定目的而对更复杂的对象或现象进行的抽象。
- AAP-3.F.2 模拟 是一种表示形式,它使用不同的值集来反映现象状态的变化。
- AAP-3.F.3 模拟通常模仿现实世界的事件,目的是进行推断,允许在不受现实世界限制的情况下研究某种现象。
- AAP-3.F.4 开发抽象模拟的过程涉及去除具体细节或简化功能。
- AAP-3.F.5 模拟可能包含源于对纳入或排除的现实世界元素之选择的偏差。
- AAP-3.F.6 当现实世界的事件不适合实验时(例如:太大、太小、太快、太慢、太贵或太危险),模拟最为有用。
- AAP-3.F.7 模拟有助于提出和修正与所考虑对象或现象相关的假设。
- AAP-3.F.8 随机数生成器可用于模拟现实中存在的变异性。
来源:College Board AP 课程与考试说明
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.
A simulation is a way of doing science, not just a picture. Because it can be run many times, cheaply and with one variable changed at a time, a simulation facilitates the formulation and refinement of hypotheses about the object or phenomenon under consideration: you propose an explanation, run the model, compare the result with reality, and adjust either the hypothesis or the model. That is why a simulation's simplifications matter — a result only supports a hypothesis about the real world to the extent that what was left out does not matter.

| English | 中文 | 拼音 |
|---|---|---|
| simulation/ˌsɪmjʊˈleɪʃn/ | 模拟 | mó nǐ |
3.17
算法效率
教学大纲
持久理解 (AAP-4): 存在计算机无法解决的问题,即使计算机能够解决某些问题,也可能无法在合理的时间内完成。
学习目标 AAP-4.A: 关于确定算法的效率: a. 解释运行时间合理的算法与不合理的算法之间的区别。[技能 1.D] b. 识别启发式解法可能更为适用的情况。[技能 1.D]
- AAP-4.A.1 问题是对一项可以通过(或无法通过)算法解决的任务的通用描述。问题的一个实例还包括特定的输入。例如,排序是一个问题;对列表 (2,3,1,7) 进行排序是该问题的一个实例。
- AAP-4.A.2 判定问题是指答案为“是”或“否”的问题(例如,是否存在从 A 到 B 的路径?)。优化问题是指在众多方案中寻找“最佳”解决方案的问题(例如,从 A 到 B 的最短路径是什么?)。
- AAP-4.A.3 效率是对算法所使用计算资源量的估算。效率通常表示为输入规模的函数。
- 排除声明 (EK AAP-4.A.3): 算法的形式分析(大O表示法)以及使用数学公式进行的严格推理不属于本课程及AP考试的范围。
- AAP-4.A.4 算法的效率通过形式化或数学推理来确定。
- AAP-4.A.5 可以通过统计某条语句或一组语句的执行次数来非正式地衡量算法的效率。
- AAP-4.A.6 针对同一问题的不同正确算法可以具有不同的效率。
- AAP-4.A.7 具有多项式效率或更慢效率(常数、线性、平方、立方等)的算法被认为是在合理的时间内运行的。具有指数级或阶乘级效率的算法则属于在不合理的时间内运行的算法示例。
- AAP-4.A.8 有些问题无法在合理时间内解决,因为不存在解决这些问题的有效算法。在这种情况下,人们会寻求近似解。
- AAP-4.A.9 启发式方法是一种解决问题的方法,它产生的解不能保证是最优的,但当那些能保证始终找到最优解的技术不切实际时,可以使用该方法。
- 排除声明 (AAP-4.A.9): 具体的启发式解法不属于本课程及AP考试的范围。
来源:College Board AP 课程与考试说明
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.

| English | 中文 | 拼音 |
|---|---|---|
| Efficiency/ɪˈfɪʃənsi/ | 效率 | xiào lǜ |
| heuristic/hjuːˈrɪstɪk/ | 启发式 | qǐ fā shì |
3.18
不可判定问题
教学大纲
持久理解 (AAP-4): 存在计算机无法解决的问题,即使计算机能够解决某些问题,也可能无法在合理的时间范围内完成。
学习目标 AAP-4.B: 解释计算机科学中不可判定问题的存在性。[技能 1.A]
- AAP-4.B.1 可判定问题是指对于该决策问题,可以编写一个算法,使其对所有输入都能产生正确的输出(例如,“该数字是否为偶数?”)。
- AAP-4.B.2 不可判定问题是指无法构造出任何算法,使其始终能够提供正确的“是”或“否”答案的问题。
- 排除说明 (EK AAP-4.B.2): 判定给定问题是否属于不可判定问题,超出了本课程及 AP 考试的考查范围。
- AAP-4.B.3 不可判定问题可能包含某些具有算法解的特例实例,但不存在能够解决该问题所有实例的通用算法。
来源:College Board AP 课程与考试说明
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.
The best-known example is the halting problem 停机问题: deciding, for any program and its input, whether the program will eventually stop or run for ever. A tool can answer this for many programs, but it has been proved that no algorithm answers it correctly for all of them. An undecidable problem may still be solved for particular cases; what is impossible is one algorithm for every case.
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 | 中文 | 拼音 |
|---|---|---|
| undecidable/ˌʌndɪˈsaɪdəbl/ | 不可判定 | bù kě pàn dìng |
| halting problem | 停机问题 | tíng jī wèn tí |
3.18
考试技巧
- 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.
该知识点的互动课程
逐步完成,配合即时检查练习。