← 返回资讯屏轩教育 · 行业资讯

IB Computer Science: Programming Fundamentals Revision | IB 计算机:编程基础考点精讲

2026-07-14 07:28 · 本文部分内容由AI辅助创作

📚 IB Computer Science: Programming Fundamentals Revision | IB 计算机:编程基础考点精讲

This article provides a comprehensive, exam-focused review of programming fundamentals as covered in the IB Computer Science syllabus. It is designed to help you master key concepts, common pseudocode conventions, and typical exam pitfalls, from variable declaration and control flow through subprograms, data structures and recursion.

本文全面梳理了 IB 计算机科学课程中的编程基础考点,旨在帮助你掌握核心概念、常见伪代码规范以及典型考试陷阱。内容从变量声明与控制流,一直延伸到子程序、数据结构与递归,覆盖 Paper 1 与 Paper 2 的典型命题方式。


1. Variables and Data Types | 变量与数据类型

A variable is a named storage location whose value can change while the program runs. Every variable has a data type, and the data type decides which values it can hold and which operations are legal on it. In IB programming questions you must correctly declare and use variables; the five core types you are expected to know are INTEGER, REAL (also written FLOAT), BOOLEAN, CHAR and STRING.

变量是一个有名称的存储位置,其值可在程序执行过程中改变。每个变量都有数据类型,数据类型决定了它可以存放哪些值、以及哪些运算对它合法。在 IB 编程题中,你需要正确声明和使用变量;必须掌握的五类核心类型是 INTEGER(整型)、REAL(也写作 FLOAT,实型)、BOOLEAN(布尔型)、CHAR(字符型)和 STRING(字符串型)。

Type | 类型 Holds | 存放内容 Example | 示例 Typical mistake | 典型错误
INTEGER Whole numbers | 整数 0, 42, −7 Storing 3.5 in an INTEGER | 把 3.5 存入整型
REAL / FLOAT Decimals | 小数 3.14, −0.5 Using = to compare two REALs | 用 = 比较两个实型
BOOLEAN TRUE or FALSE | 真或假 TRUE Writing "true" as a string | 把 true 写成字符串
CHAR One character | 单个字符 'A', '7' Confusing '7' with 7 | 把 '7' 与 7 混为一谈
STRING Text | 文本 "Hao" Comparing strings with < numerically | 用数值方式比较字符串

The IB pseudocode uses declaration statements such as DECLARE score : INTEGER, or simply an assignment such as score ← 0 when the type is obvious from context. Always initialise variables before use — reading an uninitialised variable is a logic error rather than a syntax error, so the program runs but produces nonsense, and the examiner will spot it immediately.

IB 伪代码使用声明语句,如 DECLARE score : INTEGER;当类型在语境中显而易见时,也可以直接写赋值语句 score ← 0。务必在使用前初始化变量——读取未初始化的变量属于逻辑错误而非语法错误,程序仍会运行,却会输出无意义的结果,而考官一眼就能看出来。


2. Constants, Naming and Assignment | 常量、命名与赋值

A constant is an identifier whose value is fixed once it has been defined and cannot be changed during execution. Constants are declared with the keyword CONSTANT, for example CONSTANT Pi = 3.14159. Using a constant instead of a literal number improves readability and makes maintenance safer: if the value ever changes, you edit one line rather than fifteen.

常量是一旦定义取值即固定、在程序执行过程中不可更改的标识符。常量使用关键字 CONSTANT 声明,例如 CONSTANT Pi = 3.14159。用常量代替直接写数字字面量,既能提升可读性,也让维护更安全:一旦取值需要修改,你只需改一行,而不必改十五处。

Naming conventions matter in every mark scheme. Use meaningful identifiers such as studentName or totalMarks rather than x or a1. Avoid single letters except for loop counters such as i and j. Choose a consistent style (camelCase is the usual IB convention) and never use reserved words as identifiers.

命名规范在每个评分标准中都很重要。请使用有意义的标识符,如 studentName 或 totalMarks,而不是 x 或 a1。除循环计数器(如 i、j)外,应避免使用单字母命名。请保持风格统一(IB 通常采用 camelCase 驼峰式),并且绝不把保留字用作标识符。

Assignment uses the left arrow ←, which is emphatically not the same as the equality sign =. The statement count ← count + 1 means "evaluate the right-hand side, then store the result in count". A common examination error is writing count = count + 1, which in most pseudocode conventions is a comparison, not an assignment.

赋值使用左箭头 ←,它绝不等于等号 =。count ← count + 1 的含义是"先计算右侧表达式的值,再把结果存入 count"。考试中一个常见错误是写成 count = count + 1,而在多数伪代码规范中这是比较、不是赋值。


3. Operators and Expressions | 运算符与表达式

Arithmetic operators are +, −, *, /, DIV, MOD and (in IB pseudocode) the power operator. The distinction between / and DIV is heavily examined: / always produces a real result, while DIV performs integer division and discards the remainder. MOD returns the remainder of integer division.

算术运算符包括 +、−、*、/、DIV、MOD 以及(IB 伪代码中的)幂运算符。其中 / 与 DIV 的区别是高频考点:/ 的结果总是实型,而 DIV 执行整除并舍去余数。MOD 返回整数除法的余数。

17 / 5 = 3.4    17 DIV 5 = 3    17 MOD 5 = 2

N value. Logical operators are AND, OR and NOT. Operator precedence runs: brackets first, then NOT, then arithmetic, then comparison, then AND, then OR. When in doubt, add brackets — examiners never deduct marks for clarity.

比较运算符包括 =、≠、<、>、≤、≥,它们返回 BOOLEAN 值。逻辑运算符包括 AND、OR、NOT。运算符优先级为:先括号,再 NOT,再算术运算,再比较运算,再 AND,最后 OR。不确定时请加括号——考官绝不会因为表达清晰而扣分。

A very practical exam skill is predicting the value of an expression before you trace the whole algorithm, for example (8 + 4) MOD 5 > 1 AND NOT (3 < 1) OD 5 = 2, 2 > 1 is TRUE, (3 < 1) is FALSE, NOT FALSE is TRUE, TRUE AND TRUE is TRUE.

一项非常实用的应试技巧是:在跟踪整个算法之前,先预测单个表达式的值,例如 (8 + 4) MOD 5 > 1 AND NOT (3 < 1)。分层拆解:(8 + 4) = 12,12 MOD 5 = 2,2 > 1 为 TRUE;(3 < 1) 为 FALSE,NOT FALSE 为 TRUE;TRUE AND TRUE 得 TRUE。


4. Input, Output and Pseudocode Conventions | 输入、输出与伪代码规范

IB pseudocode is language-independent. You are not writing Python, Java or VB, and mixing in real-language syntax such as print(), System.out.println or = for assignment costs marks even when the logic is correct. Learn the canonical keywords and use them consistently.

IB 伪代码是与具体语言无关的。你不是在写 Python、Java 或 VB;即使逻辑正确,若混入 print()、System.out.println 或把 = 当赋值用,也会被扣分。请掌握规范关键字并始终如一地使用。

Pseudocode is normally case-insensitive for identifiers, so totalMarks and totalmarks are treated as the same name, but using both in one answer looks careless. Every algorithm you write should declare its variables, initialise counters and accumulators, and end with OUTPUT statements that actually print the results the question asked for.

广告 / Sponsored

伪代码中的标识符通常不区分大小写,因此 totalMarks 与 totalmarks 会被视为同一个名称;但在同一份答案里两种写法混用,会显得很草率。你写的每个算法都应声明变量、初始化计数器与累加器,并以 OUTPUT 语句结束,真正输出题目要求的结果。


5. Selection: IF, CASE and Boolean Logic | 选择结构

Selection lets a program choose between alternative paths. The single-branch form is IF condition THEN ... ENDIF; the two-branch form adds ELSE; multiway decisions use ELSE IF chains or the CASE statement. Choosing the right structure is often worth a mark on its own.

选择结构让程序在不同路径之间做出取舍。单分支形式是 IF condition THEN ... ENDIF;双分支形式加上 ELSE;多路判断则使用 ELSE IF 链或 CASE 语句。选对结构本身往往就是一分。

A CASE statement is cleaner than a long ELSE IF chain when one variable is compared against a list of discrete values. It reads CASE grade OF "A" : ... OTHERWISE : ... ENDCASE. Remember that CASE compares for equality only; ranges must be tested with IF.

当同一个变量需要与一组离散取值逐一比较时,CASE 语句比冗长的 ELSE IF 链更清晰,写法形如 CASE grade OF "A" : ... OTHERWISE : ... ENDCASE。切记 CASE 只做相等比较;判断区间必须用 IF。

Two exam traps recur constantly. First, nesting: an inner IF must have its own ENDIF, and indentation should reveal which ELSE belongs to which IF. Second, boundary conditions: when a question says "at least 50", the correct test is mark &gt;= 50, not mark > 50. Silently flipping a ≥ into a > loses the boundary mark every single session.

考试中有两个反复出现的陷阱。第一是嵌套:内层 IF 必须有自己的 ENDIF,缩进要能看出哪个 ELSE 属于哪个 IF。第二是边界条件:当题目说"至少 50 分"时,正确判断是 mark &gt;= 50,而不是 mark > 50。把 ≥ 悄悄写成 >,每一次考试都会丢掉那个边界分。


6. Iteration: Loops and Trace Tables | 循环与跟踪表

Iteration repeats a block of statements. IB pseudocode offers three forms: LOOP WHILE condition ... ENDLOOP (pre-tested), LOOP UNTIL condition ... ENDLOOP (post-tested, so the body runs at least once), and the counted loop FOR i ← 1 TO 10 ... NEXT i.

迭代用于重复执行一段语句块。IB 伪代码提供三种形式:LOOP WHILE condition ... ENDLOOP(先判断后执行)、LOOP UNTIL condition ... ENDLOOP(先执行后判断,循环体至少执行一次),以及计数循环 FOR i ← 1 TO 10 ... NEXT i。

Do not confuse WHILE and UNTIL. LOOP WHILE count < 5 continues while the condition is true; LOOP UNTIL count = 5 continues until the condition becomes true, i.e. repeating while it is false. Swapping these two is the classic logic error in loop questions.

不要把 WHILE 和 UNTIL 混为一谈。LOOP WHILE count < 5 在条件为真时继续循环;LOOP UNTIL count = 5 则一直循环到条件变为真为止,也就是在条件为假时反复执行。把两者互换,是循环题中最典型的逻辑错误。

Trace tables are the examiner's favourite tool for testing whether you really understand a loop. Draw columns for each variable plus one for the condition and one for OUTPUT, then complete one row per iteration. A loop with an update such as i ← i + 2 is a favourite because candidates who expect a step of 1 get the wrong number of iterations, and an off-by-one error in a FOR loop bound is equally common.

跟踪表是考官检验你是否真正理解循环的最爱工具。为每个变量画一列,再加一列条件、一列 OUTPUT,每次迭代填一行。带有 i ← i + 2 这类更新的循环尤其受青睐,因为习惯步长为 1 的考生会算错迭代次数;而 FOR 循环终值上的"差一错误"同样常见。

Finally, watch for infinite loops. If the loop variable is never updated inside the body, or the terminating condition can never be reached, the algorithm never halts. Being able to state this in words — and explain the fix — earns full marks.

最后要警惕死循环。如果循环变量在循环体内从不更新,或者终止条件永远无法达到,算法就永远不会停止。能够用文字指出这一点并说明如何修正,即可拿到满分。


7. Arrays, Lists and Collections | 数组、列表与集合

An array stores a fixed number of elements of the same type under one identifier. Declare it with DECLARE marks : ARRAY[1..30] OF INTEGER and access a single element with marks[5]. IB pseudocode normally indexes arrays from 1, not from 0, which matters when you write a loop bound.

数组在同一个标识符下存放固定数量、类型相同的元素。声明写法为 DECLARE marks : ARRAY[1..30] OF INTEGER,访问单个元素写作 marks[5]。IB 伪代码中数组通常从 1 开始索引,而不是从 0 开始,这一点会直接影响你写循环终值。

Two-dimensional arrays are declared as ARRAY[1..3, 1..4] and accessed as grid[row, column]. Mixing up rows and columns is a frequent slip, so always name the loop variables row and column rather than i and j when the array is rectangular.

二维数组声明为 ARRAY[1..3, 1..4],访问写作 grid[row, column]。行列弄反是常见疏漏,因此处理矩形数组时,循环变量最好直接命名为 row 与 column,而不要用 i 和 j。

Two standard algorithms appear again and again. Linear search walks through the array comparing each element with the target until it is found or the array ends. Bubble sort repeatedly compares adjacent pairs and swaps them if they are out of order; one pass through n elements guarantees the largest value reaches the final position. In both cases, writing the loop bounds correctly is worth more marks than writing them quickly.

有两个经典算法反复出现。线性查找逐个比较数组元素与目标值,直到找到或遍历结束。冒泡排序反复比较相邻元素,若顺序不对就交换;对 n 个元素走一遍,就能保证最大值落到最后一位。在这两个算法中,把循环边界写对,比写得快更能拿分。

Dynamic structures such as LIST, STACK and QUEUE behave differently: lists grow and shrink, stacks are LIFO (last

广告 / Sponsored
IBDP国际文凭计算机科学学生用书 高级及标准水平 Computer Science for the IB Diplo…
计算机 备考教辅 · 正版现货 · 与本文内容相关
查看详情 / View on Taobao参考价 ¥524.0

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao