📚 Programming Fundamentals and Algorithms for Edexcel A-Level | Edexcel A-Level 编程基础与算法精讲
Programming is a central component of the Edexcel A-Level Computer Science specification. It tests your ability to design, write, trace and evaluate code under exam conditions. This revision guide brings together the essential constructs, data structures, standard algorithms and object-oriented techniques you need to succeed in Paper 2 and the practical programming project.
编程是 Edexcel A-Level 计算机科学考试的核心组成部分。它考查你在考试条件下设计、编写、追踪和评估代码的能力。本复习指南汇总了你在 Paper 2 和实践编程项目中取得成功所需的基本结构、数据结构、标准算法与面向对象技术。
1. Variables, Data Types and Constants | 变量、数据类型与常量
In Edexcel pseudocode and Python, a variable is a named memory location whose value can change during execution. Constants are fixed values declared with the keyword CONSTANT, and their identifiers are usually written in upper case.
在 Edexcel 伪代码和 Python 中,变量是命名的内存位置,其值在执行过程中可以改变。常量是用关键字 CONSTANT 声明的固定值,其标识符通常用大写字母书写。
Common data types include Integer, Real/Float, Boolean, Char, String, and Date. Choosing the correct type affects memory use and the operations available; for example, string concatenation uses '+' while integer division uses DIV or //.
常见数据类型包括整型、实型/浮点型、布尔型、字符型、字符串型和日期型。选择正确的类型会影响内存使用和可用的操作;例如,字符串连接使用 '+',而整数除法使用 DIV 或 //。
ion functions such as INT_TO_STRING and STRING_TO_INT are useful when handling user input.
算术运算符包括 +、-、*、/、MOD 和 DIV。比较运算符为 =、≠、<、>、≤ 和 ≥。类型转换函数(如 INT_TO_STRING 和 STRING_TO_INT)在处理用户输入时非常有用。
CONSTANT VAT_RATE ← 0.20
price ← 50.00
vat ← price × VAT_RATE
2. Sequence, Selection and Iteration | 顺序、选择与迭代
All algorithms can be built from three control structures: sequence, selection and iteration. Sequence executes statements one after another; selection uses IF...THEN...ELSE...ENDIF to make decisions; iteration repeats blocks using FOR, WHILE or REPEAT...UNTIL loops.
所有算法都可以由三种控制结构构建:顺序、选择和迭代。顺序是一条接一条执行语句;选择使用 IF...THEN...ELSE...ENDIF 进行判断;迭代使用 FOR、WHILE 或 REPEAT...UNTIL 循环重复执行语句块。
When choosing between loops, remember that a WHILE loop tests the condition before each iteration and may never execute, whereas a REPEAT...UNTIL loop tests after each iteration and executes at least once.
在循环之间选择时,请记住 WHILE 循环在每次迭代之前测试条件,可能一次都不执行;而 REPEAT...UNTIL 循环在每次迭代之后测试条件,至少执行一次。
A CASE statement can replace multiple IF...ELSEIF checks when the same variable is compared with several literal values. Nested structures are allowed, but deep nesting reduces readability and should be avoided where possible.
当同一个变量与多个字面值比较时,CASE 语句可以替代多个 IF...ELSEIF 检查。允许嵌套结构,但深层嵌套会降低可读性,应尽可能避免。
IF score ≥ 80 THEN
grade ← 'A'
ELSE IF score ≥ 60 THEN
grade ← 'B'
ELSE
grade ← 'C'
ENDIF
3. Arrays, Lists and 2D Structures | 数组、列表与二维结构
Arrays and lists store multiple values under one identifier. A 1D array is indexed from 0 in Python or from 1 in some pseudocode; Edexcel pseudocode often uses 0-based indexing, so always check the question context.
数组和列表在一个标识符下存储多个值。一维数组在 Python 中从 0 开始索引,而在某些伪代码中从 1 开始;Edexcel 伪代码通常使用 0 基索引,因此务必检查题目上下文。
Common operations on arrays include traversing, inserting, deleting and searching. Inserting or deleting in the middle of an array requires shifting elements, which has O(n) time complexity.
数组的常见操作包括遍历、插入、删除和搜索。在数组中间插入或删除需要移动元素,时间复杂度为 O(n)。
A 2D array is a table of rows and columns, written as myArray[row, column]. Use nested loops to traverse it, with the outer loop controlling rows and the inner loop controlling columns.
二维数组是由行和列组成的表格,写作 myArray[row, column]。使用嵌套循环遍历它,外层循环控制行,内层循环控制列。
FOR i ← 0 TO rows-1
FOR j ← 0 TO cols-1
OUTPUT myArray[i, j]
NEXT j
NEXT i
4. Functions and Procedures | 函数与过程
Functions return a value and are called as part of an expression; procedures do not return a value and are called as standalone statements. Parameters can be passed by value or by reference, which affects whether changes persist outside the subroutine.
函数返回一个值,并作为表达式的一部分调用;过程不返回值,作为独立语句调用。参数可以按值传递或按引用传递,这会影响更改是否在子程序之外保留。
Local variables are declared inside a subroutine and exist only during its execution. Global variables can be accessed anywhere, but overusing them makes code harder to debug and reduces modularity.
局部变量在子程序内部声明,仅在其执行期间存在。全局变量可以在任何地方访问,但过度使用会使代码更难调试并降低模块化程度。
By default, scalar parameters are passed by value in most languages, meaning a copy is made. Arrays and objects are often passed by reference, so changes inside the subroutine can affect the original data structure.
在大多数语言中,标量参数默认按值传递,即创建副本。数组和对象通常按引用传递,因此子程序内部的更改可能会影响原始数据结构。
FUNCTION Add(a, b)
RETURN a + b
END FUNCTION
5. Recursion and the Call Stack | 递归与调用栈
A recursive subroutine calls itself with a smaller instance of the problem. Every recursive algorithm must have a base case to stop the recursion and a recursive case that reduces the problem size.
递归子程序用问题的更小实例调用自身。每个递归算法必须有一个基本情况来停止递归,以及一个递归情况来缩小问题规模。
Recursion uses the call stack to store return addresses and local variables. If the base case is missing or unreachable, the stack overflows, causing a runtime error.
递归使用调用栈来存储返回地址和局部变量。如果基本情况缺失或无法到达,栈会溢出,导致运行时错误。
Recursion often produces elegant solutions for problems such as factorial, Fibonacci and binary tree traversal. However, recursion can be less efficient than iteration because each call adds stack overhead and may recompute the same values.
递归通常为阶乘、斐波那契和二叉树遍历等问题提供优雅的解决方案。然而,递归可能比迭代效率低,因为每次调用都会增加栈开销,并可能重复计算相同的值。
FUNCTION Factorial(n)
IF n = 0 THEN
RETURN 1
ELSE
RETURN n × Factorial(n-1)
ENDIF
END FUNCTION
6. File Handling and Exception Management | 文件处理与异常管理
External data is stored in text or binary files. Typical operations include opening a file in read, write or append mode, reading lines with READLINE, writing with PRINT or WRITELINE, and closing the file with CLOSE.
外部数据存储在文本或二进制文件中。典型操作包括以读、写或追加模式打开文件,用 READLINE 读取行,用 PRINT 或 WRITELINE 写入,以及用 CLOSE 关闭文件。
Always close files after use to flush buffers and release file handles. The EOF marker indicates the end of a file, and loops should stop reading when EOF is reached.
使用后始终关闭文件以刷新缓冲区并释放文件句柄。EOF 标记表示文件结束,循环应在到达 EOF 时停止读取。
Exception handling uses TRY...EXCEPT...FINALLY to manage runtime errors such as a missing file or invalid numeric input. This prevents the program from crashing and allows graceful recovery.
异常处理使用 TRY...EXCEPT...FINALLY 来管理运行时错误,例如文件缺失或无效的数字输入。这可以防止程序崩溃并允许优雅地恢复。
TRY
OPEN 'data.txt' FOR READ
WHILE NOT EOF
READLINE
ENDWHILE
EXCEPT IOError
OUTPUT 'File not found'
FINALLY
CLOSE FILE
ENDTRY
7. Searching Algorithms: Linear and Binary Search | 查找算法:线性搜索与二分搜索
Linear search checks each element in turn until the target is found or the list ends. It works on unsorted data and has a worst-case time complexity of O(n).
线性搜索依次检查每个元素,直到找到目标或列表结束。它适用于未排序的数据,最坏情况时间复杂度为 O(n)。
Binary search repeatedly halves a sorted list by comparing the middle element with the target. It has O(log₂ n) time complexity but requires the data to be sorted first.
二分搜索通过将中间元素与目标比较,反复将已排序列表减半。它的时间复杂度为 O(log₂ n),但要求数据先排序。
In a binary search, if the target is less than the middle value, discard the upper half; if greater, discard the lower half. This process continues until the target is found or the search interval is empty.
在二分搜索中,如果目标小于中间值,则丢弃上半部分;如果大于中间值,则丢弃下半部分。这个过程持续到找到目标或搜索区间为空。
WHILE low ≤ high
mid ← (low + high) DIV 2
IF arr[mid] = target THEN RETURN mid
IF arr[mid] < target THEN low ← mid + 1
ELSE high ← mid - 1
ENDWHILE
8. Sorting Algorithms: Bubble, Insertion, Merge | 排序算法:冒泡、插入与归并
Bubble sort compares adjacent pairs and swaps them if they are out of order, moving the largest value to the end on each pass. It is stable but inefficient with O(n²) average time.
冒泡排序比较相邻元素并在顺序错误时交换它们,每次遍历将最大值移动到末尾。它是稳定的,但平均时间复杂度为 O(n²),效率较低。
Insertion sort builds a sorted sublist by inserting each new element into its correct position. It performs well on nearly sorted data and is also stable.
插入排序通过将每个新元素插入到正确位置来构建已排序子列表。它对接近有序的数据表现良好,并且也是稳定的。
Merge sort uses a divide-and-conquer approach, recursively splitting the list in half, sorting each half, and merging the results. It guarantees O(n log₂ n) time but uses extra memory.
归并排序使用分治法,递归地将列表分成两半,对每半排序,然后合并结果。它保证 O(n log₂ n) 时间,但使用额外内存。
A stable sorting algorithm preserves the relative order of equal elements. This matters when sorting by one key and then another, such as sorting students by name and then by grade.
稳定的排序算法会保留相等元素的相对顺序。当按一个关键字排序后再按另一个关键字排序时,这一点很重要,例如先按姓名再按成绩对学生排序。
9. Big O Notation and Algorithm Efficiency | 大 O 表示法与算法效率
Big O notation describes how the time or space required by an algorithm grows as the input size n increases. Constant time is O(1), linear is O(n), quadratic is O(n²), and logarithmic is O(log₂ n).
大 O 表示法描述算法所需的时间或空间如何随着输入规模 n 的增长而增长。常数时间为 O(1),线性为 O(n),二次方为 O(n²),对数为 O(log₂ n)。
In an exam, you may be asked to compare algorithms for the same task. Always consider the best, average and worst cases, and whether extra memory is required.
在考试中,你可能会被要求比较解决同一任务
Published by TutorHao | A-Level 编程 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图