📚 Programming Fundamentals: Data Types, Control Structures and Algorithms | 编程基础:数据类型、控制结构与算法
In Edexcel A-Level Computer Science, programming questions require more than writing code that happens to run. You need to understand how data is represented, how control flow is structured, and how algorithms can be traced and evaluated. This revision guide covers the core programming foundations tested across Paper 1 and Paper 2.
在 Edexcel A-Level 计算机科学中,编程题不仅要求代码能运行,更要求你理解数据如何表示、控制流如何组织,以及算法如何被追踪和评估。本复习指南涵盖 Paper 1 和 Paper 2 中考查的编程基础核心内容。
1. Primitive Data Types | 基本数据类型
In Edexcel A-Level Computer Science, understanding primitive data types is essential because selecting the correct type affects storage, precision and the operations available.
在 Edexcel A-Level 计算机科学中,理解基本数据类型至关重要,因为选择正确类型会影响存储空间、精度和可用操作。
- Integer: whole numbers such as 0, -7, 42. 中文:整数,例如 0、-7、42。
- Real/Float: numbers with fractional parts such as 3.14 or -0.5. 中文:实数/浮点数,例如 3.14 或 -0.5。
- Boolean: only TRUE or FALSE. 中文:布尔值,只有 TRUE 或 FALSE。
- Character: a single symbol like ‘A’, ‘7’, ‘$’. 中文:字符,单个符号,如 ‘A’、’7’、’$’。
- String: a sequence of characters like ‘hello’. 中文:字符串,字符序列,如 ‘hello’。
Edexcel pseudocode often assumes that variables are declared with a type before use, for example DECLARE age AS INTEGER.
Edexcel 伪代码通常假定变量在使用前声明类型,例如 DECLARE age AS INTEGER。
2. Constants and Variables | 常量与变量
A variable is a named storage location whose value can change while the program runs. A constant is given a value once and cannot change, which prevents accidental modification.
变量是命名存储位置,其值在程序运行期间可以改变。常量只赋值一次且不能更改,从而防止意外修改。
Using named constants improves clarity: instead of writing 0.1 repeatedly, write CONSTANT VAT_RATE ← 0.1. This makes code easier to update.
使用命名常量可提高清晰度:与其反复写 0.1,不如写 CONSTANT VAT_RATE ← 0.1。这让代码更易于更新。
In Edexcel questions, you may be asked to identify whether an identifier should be a variable or constant based on its role in the algorithm.
在 Edexcel 题目中,可能要求你根据标识符在算法中的作用判断它应作为变量还是常量。
3. Operators and Expressions | 运算符与表达式
Expressions combine values, variables and operators to produce a new value. Arithmetic operators are +, -, *, /, MOD (remainder) and DIV (integer division).
表达式将值、变量和运算符组合起来产生新值。算术运算符包括 +、-、*、/、MOD(取余)和 DIV(整除)。
Comparison operators produce Boolean results: =, ≠, <, >, ≤, ≥. Logical operators AND, OR and NOT combine Boolean values.
比较运算符产生布尔结果:=、≠、<、>、≤、≥。逻辑运算符 AND、OR 和 NOT 组合布尔值。
Precedence follows BIDMAS: brackets first, then multiplication/division, then addition/subtraction; logical operators are applied after comparisons.
优先级遵循 BIDMAS:括号优先,然后乘除,再加减;逻辑运算符在比较之后应用。
| Brackets ( ) | Highest priority 最高优先级 |
| * / MOD DIV | Multiplication and division 乘除 |
| + – | Addition and subtraction 加减 |
| = ≠ < > ≤ ≥ | Comparison 比较 |
| NOT | Logical NOT 逻辑非 |
| AND | Logical AND 逻辑与 |
| OR | Lowest priority 最低优先级 |
4. Selection: IF and CASE | 选择结构:IF 与 CASE
Selection structures allow a program to take different paths depending on a condition. The IF statement evaluates a Boolean expression and executes a block when it is TRUE.
选择结构允许程序根据条件走不同路径。IF 语句计算布尔表达式,当结果为 TRUE 时执行相应代码块。
Edexcel pseudocode uses: IF x < 10 THEN … ELSE … ENDIF. The ELSE branch handles the FALSE case; ELSE IF can chain multiple conditions.
Edexcel 伪代码使用:IF x < 10 THEN … ELSE … ENDIF。ELSE 分支处理 FALSE 情况;ELSE IF 可以串联多个条件。
CASE works well when comparing one variable against several discrete values. Each branch represents a constant value or range.
CASE 适用于将一个变量与多个离散值比较。每个分支表示一个常量值或范围。
Avoid unnecessary nested IFs; CASE often makes code clearer and easier to trace in exam papers.
避免不必要的嵌套 IF;CASE 通常使代码更清晰,在试卷中更容易追踪。
5. Iteration: FOR, WHILE, REPEAT | 迭代:FOR、WHILE、REPEAT 循环
Iteration repeats a block of code. A FOR loop is count-controlled: it runs a fixed number of times, often using a loop counter.
迭代重复执行代码块。FOR 循环是计数控制的:它运行固定次数,通常使用循环计数器。
A WHILE loop checks a condition before each iteration. If the condition is FALSE at the start, the loop body never executes.
WHILE 循环在每次迭代前检查条件。如果条件开始为 FALSE,循环体不会执行。
A REPEAT…UNTIL loop checks the condition after the loop body, so the body runs at least once.
REPEAT…UNTIL 循环在循环体之后检查条件,因此循环体至少执行一次。
Matching the correct loop to a scenario shows understanding: use FOR when the number of iterations is known, WHILE when there may be zero iterations, and REPEAT when at least one execution is required.
选择正确的循环体现理解:迭代次数已知用 FOR,可能为零次用 WHILE,至少需要执行一次用 REPEAT。
6. Arrays and Lists | 数组与列表
Arrays store multiple values of the same type under one identifier. Edexcel questions use one-dimensional and two-dimensional arrays, with indices either 0-based or 1-based depending on the context.
数组在一个标识符下存储多个相同类型的值。Edexcel 题目使用一维和二维数组,索引根据上下文可能从 0 或 1 开始。
Common operations include accessing by index, updating an element, traversing all elements, and searching for a value.
常见操作包括按索引访问、更新元素、遍历所有元素以及搜索值。
Lists are dynamic data structures that can grow and shrink; typical operations are append, insert, remove and length.
列表是动态数据结构,可以增长和收缩;典型操作包括追加、插入、删除和求长度。
When tracing arrays in a trace table, write the entire array state after each statement to avoid losing track of changes.
在追踪表中追踪数组时,在每条语句后写出整个数组状态,以免丢失变化。
7. Subroutines: Procedures and Functions | 子程序:过程与函数
Subroutines break programs into manageable, reusable blocks. A procedure does a job and does not return a value; a function returns a value to the caller.
子程序将程序分解为可管理、可复用的块。过程完成任务且不返回值;函数向调用者返回一个值。
Edexcel pseudocode defines a subroutine with SUBROUTINE name(params) … ENDSUBROUTINE. A function uses RETURN expr to send a result back.
Edexcel 伪代码用 SUBROUTINE name(params) … ENDSUBROUTINE 定义子程序。函数使用 RETURN expr 返回结果。
Parameters can be passed by value or by reference. By value copies the data; changes inside do not affect the original. By reference passes the address, so changes are visible outside.
参数可以按值传递或按引用传递。按值传递复制数据,内部修改不影响原始值。按引用传递传递地址,因此外部可以看到修改。
Local variables are declared inside a subroutine and exist only during its execution; global variables are accessible throughout the program. In exams, prefer local variables to reduce side effects.
局部变量在子程序内部声明,仅在执行期间存在;全局变量整个程序中可访问。考试中优先使用局部变量以减少副作用。
8. String Handling and Validation | 字符串处理与数据验证
Strings are sequences of characters. Common Edexcel pseudocode operations include LEN(str), SUBSTRING(str, start, length), and concatenation using + or &.
字符串是字符序列。常见 Edexcel 伪代码操作包括 LEN(str)、SUBSTRING(str, start, length) 以及使用 + 或 & 连接字符串。
Type conversion functions such as STR_TO_INT and INT_TO_STR are used when comparing or calculating with mixed data types.
在混合数据类型比较或计算时,使用 STR_TO_INT、INT_TO_STR 等类型转换函数。
Validation should check input before processing. Presence check ensures data is entered; range check accepts values within limits; length check verifies the number of characters; format check confirms a pattern such as a postcode.
验证应在处理前检查输入。存在性检查确保数据已输入;范围检查接受范围内的值;长度检查验证字符数;格式检查确认模式,如邮政编码。
Defensive design combines validation with sensible error messages so that invalid data is rejected rather than crashing the program.
防御性设计将验证与合理的错误信息结合,以便拒绝无效数据而不是使程序崩溃。
9. Searching and Sorting Algorithms | 查找与排序算法
Linear search checks each element in order until the target is found or the end is reached. It is simple but has O(n) time complexity on average.
线性查找按顺序检查每个元素,直到找到目标或到达末尾。它简单,但平均时间复杂度为 O(n)。
Binary search repeatedly halves a sorted array. It compares the target with the middle element, then searches the left or right half. Its time complexity is O(log n).
二分查找反复将有序数组对半分。它将目标与中间元素比较,然后搜索左半或右半部分。时间复杂度为 O(log n)。
Bubble sort repeatedly swaps adjacent elements that are out of order, sinking the largest remaining value to the end each pass. Insertion sort places each new element into its correct position within the sorted part.
冒泡排序反复交换相邻的乱序元素,每趟将剩余最大值沉到末尾。插入排序将每个新元素插入已排序部分的正确位置。
In Edexcel exams, be prepared to complete traces, state the number of comparisons, and compare algorithms for a specific scenario.
在 Edexcel 考试中,要准备好完成追踪、说明比较次数,并针对特定场景比较算法。
10. Trace Tables and Debugging | 追踪表与调试
A trace table records each variable’s value line by line as an algorithm runs. It is the primary method for dry running pseudocode and locating
Published by TutorHao | A-Level 编程 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导