Edexcel A-Level Programming: From Variables to Object-Oriented Design | Edexcel A-Level 编程:从变量到面向对象设计

📚 Edexcel A-Level Programming: From Variables to Object-Oriented Design | Edexcel A-Level 编程:从变量到面向对象设计

Programming is the practical heart of the Edexcel A-Level Computer Science specification. It requires you to move beyond reading pseudocode and to design, trace, test and evaluate solutions for real computational problems. This revision guide brings together the core programming ideas you need for Paper 1 and the on-screen programming assessment, with an emphasis on clarity, precision and exam-style reasoning.

编程是 Edexcel A-Level 计算机科学课程中最具实操性的核心部分。它要求你不只是阅读伪代码,还要为真实计算问题设计、跟踪、测试和评估解决方案。本复习指南汇总了 Paper 1 和机考编程评估所需的编程核心思想,重点关注清晰度、准确性以及考试风格的推理能力。

Throughout the article, pseudocode is written in a Pearson-style notation. All variables, control structures and data structures are covered from first principles, so you can use this guide both for initial learning and for final revision.

全文中的伪代码采用 Pearson 风格记法。所有变量、控制结构和数据结构都从基本原理讲起,因此本指南既可用于初次学习,也可用于最终复习。


1. Variables, Constants and Data Types | 变量、常量与数据类型

A variable is a named storage location whose value can change while a program runs. In Edexcel pseudocode, assignment is shown with the left arrow, for example score ← 0. Always declare variables before use and choose a name that shows its purpose, such as totalMarks rather than t.

变量是一个命名的存储位置,其值在程序运行期间可以改变。在 Edexcel 伪代码中,赋值使用左箭头表示,例如 score ← 0。在使用变量前必须先声明,并选择一个能体现其用途的名称,例如 totalMarks 而不是 t

A constant is similar to a variable but its value cannot be changed after it is set. Constants are useful for fixed values such as VAT rate, maximum entries or mathematical constants. They make code easier to maintain because changing one declaration updates the whole program.

常量与变量类似,但常量的值在设定后不能改变。常量适用于固定值,例如增值税率、最大条目数或数学常数。它们使代码更容易维护,因为只需修改一处声明即可更新整个程序。

Edexcel expects you to know common simple data types:

Edexcel 要求你掌握常见的简单数据类型:

  • Integer: whole numbers, e.g. -3, 0, 42
  • Real / Float: numbers with fractional parts, e.g. 3.14, -0.5
  • Boolean: values TRUE or FALSE
  • Char: a single character such as 'A' or '7'
  • String: a sequence of characters such as "Hello"
  • 整数:整数值,例如 -3、0、42
  • 实数 / 浮点数:带小数部分的数值,例如 3.14、-0.5
  • 布尔型:值为 TRUEFALSE
  • 字符型:单个字符,例如 'A''7'
  • 字符串:字符序列,例如 "Hello"

When converting between types, use functions such as INT(), REAL(), STR(), CHAR() and BOOL(). Be careful: converting from real to integer truncates the fractional part, it does not round.

在类型之间转换时,使用 INT()REAL()STR()CHAR()BOOL() 等函数。注意:从实数转换为整数会截断小数部分,而不是四舍五入。


2. Operators and Boolean Logic | 运算符与布尔逻辑

Arithmetic operators include +, -, *, /, MOD and DIV. MOD returns the remainder of a division, while DIV returns the integer quotient. For example, 17 MOD 5 gives 2, and 17 DIV 5 gives 3.

算术运算符包括 +-*/MODDIVMOD 返回除法中的余数,而 DIV 返回整数商。例如,17 MOD 5 得到 217 DIV 5 得到 3

Comparison operators test the relationship between two values. They are =, , >, <, and . Their result is always a Boolean value. You must use = rather than == in Edexcel pseudocode.

比较运算符用于测试两个值之间的关系。它们是 =><。它们的结果始终是布尔值。在 Edexcel 伪代码中必须使用 =,而不是 ==

Boolean operators combine logical expressions:

布尔运算符用于组合逻辑表达式:

  • AND returns TRUE only if both operands are TRUE.
  • OR returns TRUE if at least one operand is TRUE.
  • NOT reverses the Boolean value.
  • AND 只有在两个操作数都为 TRUE 时才返回 TRUE
  • OR 只要至少一个操作数为 TRUE 就返回 TRUE
  • NOT 反转布尔值。

An exam-style expression can be written as:

考试风格的表达式可以写为:

IF age ≥ 18 AND hasLicence = TRUE THEN
OUTPUT “Allowed to drive”

Always evaluate Boolean expressions using a truth table if you are asked to simplify or prove equivalence. Short-circuit evaluation is not required by Edexcel, so treat both sides as if they are fully evaluated.

如果要求你简化或证明等价,请始终使用真值表来计算布尔表达式。Edexcel 不要求短路求值,因此把两侧都视为会被完整求值。


3. Selection and Iteration | 选择与迭代

Selection allows a program to take different paths based on a condition. The basic form is a single IF...THEN...ELSE...ENDIF block. You can nest selections, but keep indentation consistent so the structure is clear to the examiner.

选择允许程序根据条件采取不同路径。基本形式是单个 IF...THEN...ELSE...ENDIF 块。你可以嵌套选择结构,但要保持缩进一致,使结构对阅卷人清晰可见。

A multi-way selection is often clearer with CASE:

多分支选择通常使用 CASE 更清晰:

CASE grade OF
‘A’: OUTPUT “Excellent”
‘B’: OUTPUT “Good”
‘C’: OUTPUT “Pass”
OTHERWISE: OUTPUT “Fail”
ENDCASE

Iteration means repeating a block of code. Edexcel pseudocode uses three forms: FOR, WHILE and REPEAT...UNTIL. A FOR loop is count-controlled; a WHILE loop is pre-condition; a REPEAT...UNTIL loop is post-condition and always runs at least once.

迭代意味着重复执行一段代码。Edexcel 伪代码使用三种形式:FORWHILEREPEAT...UNTILFOR 循环是计数控制;WHILE 循环是前置条件;REPEAT...UNTIL 循环是后置条件,因此至少执行一次。

Choose the right loop for the situation. Use FOR when you know the exact number of iterations, such as processing an array of known length. Use WHILE when the loop may run zero times. Use REPEAT...UNTIL when at least one execution is required, such as reading and validating a menu choice.

根据情况选择合适的循环。当你知道确切的迭代次数时使用 FOR,例如处理已知长度的数组。当循环可能执行零次时使用 WHILE。当至少需要执行一次时使用 REPEAT...UNTIL,例如读取并验证菜单选项。


4. Arrays and Records | 数组与记录

An array is an ordered collection of elements of the same data type, accessed by an index. In Edexcel pseudocode, a one-dimensional array is declared as ARRAY scores[1:10] OF INTEGER. Indexing usually starts at 1, but always read the question carefully because some questions may use zero-based indexing.

数组是相同数据类型元素的有序集合,通过索引访问。在 Edexcel 伪代码中,一维数组声明为 ARRAY scores[1:10] OF INTEGER。索引通常从 1 开始,但一定要仔细阅读题目,因为有些题目可能使用从 0 开始的索引。

Two-dimensional arrays are declared with two bounds, such as ARRAY grid[1:8, 1:8] OF CHAR. They are useful for tables, boards or maps. Nested loops are the standard way to traverse a 2D array: the outer loop controls rows and the inner loop controls columns.

二维数组用两个边界声明,例如 ARRAY grid[1:8, 1:8] OF CHAR。它们适用于表格、棋盘或地图。嵌套循环是遍历二维数组的标准方式:外循环控制行,内循环控制列。

A record is a data structure that groups related values of possibly different types. Each value is a field. In pseudocode you can define a record type and then create variables of that type. Example:

记录是一种将可能不同类型但相关的值组合在一起的数据结构。每个值是一个字段。在伪代码中,你可以定义记录类型,然后创建该类型的变量。示例:

TYPE StudentRecord
  name : STRING
  age : INTEGER
  grade : CHAR
ENDTYPE

DECLARE pupil : StudentRecord
pupil.name ← “Alice”

Records are especially useful when dealing with databases, file handling and object-like data before full OOP is introduced.

记录在处理数据库、文件操作以及在引入完整面向对象编程之前的类对象数据时特别有用。


5. Functions and Parameters | 函数与参数

A function is a named block of code that returns a single value. A procedure is similar but does not return a value. In Edexcel pseudocode, a function is declared with FUNCTION name(parameters) RETURNS type and finishes with RETURN value and ENDFUNCTION.

函数是一个返回单个值的命名代码块。过程与之类似,但不返回值。在 Edexcel 伪代码中,函数用 FUNCTION name(parameters) RETURNS type 声明,并以 RETURN valueENDFUNCTION 结束。

Parameters are values passed into a function or procedure. They can be passed by value or by reference. By value means a copy is used, so changes inside the routine do not affect the original variable. By reference means the routine can modify the caller’s variable. You must state which method you are using when the question asks for it.

参数是传递给函数或过程的值。它们可以按值传递或按引用传递。按值传递意味着使用副本,因此例程内部的修改不会影响原始变量。按引用传递意味着例程可以修改调用者的变量。当题目要求时,你必须说明使用的是哪种方法。

A clear function example:

一个清晰的函数示例:

FUNCTION maxOfTwo(a: INTEGER, b: INTEGER) RETURNS INTEGER
  IF a > b THEN
    RETURN a
  ELSE
    RETURN b
  ENDIF
ENDFUNCTION

Functions support modular design. Each function should perform one clear task, have a meaningful name and avoid depending on global variables unless a question specifically requires it.

函数支持模块化设计。每个函数应执行一个明确的任务,有意义的名称,并且除非题目明确要求,否则应避免依赖全局变量。


6. Recursion and Stack Frames | 递归与栈帧

Recursion is a technique in which a function calls itself to solve a smaller instance of the same problem. A correct recursive routine always has a base case that stops the recursion, plus a recursive case that reduces the problem toward that base case.

递归是一种让函数调用自身来解决同一问题更小实例的技术。正确的递归例程总是有一个停止递归的基准情形,以及一个将问题缩小到该基准情形的递归情形。

The factorial function is a classic example:

阶乘函数是一个经典示例:

FUNCTION factorial(n: INTEGER) RETURNS INTEGER
  IF n = 0 THEN
    RETURN 1
  ELSE
    RETURN n * factorial(n – 1)
  ENDIF
ENDFUNCTION

Each recursive call creates a new stack frame containing local variables, parameters and the return address. If the base case is missing or unreachable, the stack overflows and the program crashes. This is called infinite recursion.

每次递归调用都会创建一个新的栈帧,其中包含局部变量、参数和返回地址。如果缺少基准情形或基准情形不可达,栈就会溢出并导致程序崩溃。这称为无限递归。

Recursion is elegant but can be less efficient than iteration because of the overhead of creating stack frames. In Edexcel questions, you may be asked to trace recursive calls, convert recursion to iteration, or compare the two approaches.

递归虽然优雅,但由于创建栈帧的开销,其效率可能低于迭代。在 Edexcel 题目中,你可能会被要求跟踪递归调用、将递归转换为迭代,或比较这两种方法。


7. Searching and Sorting Algorithms | 查找与排序算法

Searching is the process of finding a target value in a data structure. Linear search checks each element one by one. It works on unsorted data and has time complexity O(n) in the worst case.

查找是在数据结构中寻找目标值的过程。线性查找逐个检查每个元素。它适用于未排序的数据,最坏情况下的时间复杂度为 O(n)。

Binary search is much faster but only works on sorted arrays. It repeatedly compares the target with the middle element and eliminates half of the remaining data. Its worst-case time complexity is O(log n).

二分查找速度更快,但只适用于已排序的数组。它反复将目标值与中间元素进行比较,并排除剩余数据的一半。其最坏情况下的时间复杂度为 O(log n)。

Sorting puts data into a defined order. Bubble sort repeatedly swaps adjacent out-of-order elements until no swaps are needed. It is simple but has O(n²) complexity. Insertion sort builds a sorted sublist by inserting each new element into its correct position; it is efficient for small or nearly sorted data.

排序将数据按指定顺序排列。冒泡排序反复交换相邻的顺序错误元素,直到不再需要交换。它简单,但复杂度为 O(n²)。插入排序通过将每个新元素插入到正确位置来构建有序子列表;它对于小规模或接近有序的数据效率较高。

Merge sort uses a divide-and-conquer strategy: it splits the list into halves recursively, sorts each half, then merges the sorted halves. Merge sort has O(n log n) complexity and is stable, but requires additional memory for merging.

归并排序采用分治策略:它递归地将列表分成两半,对每一半排序,然后合并有序的两半。归并排序的复杂度为 O(n log n),并且是稳定的,但合并时需要额外的内存。

You must be able to trace sorting and searching algorithms by hand, showing the state of the array after each pass. Exam questions often ask you to complete a trace table or identify the algorithm from a description.

你必须能够手工跟踪排序和查找算法,展示每一趟之后数组的状态。考试题目经常要求你完成跟踪表,或根据描述识别算法。


8. Object-Oriented Programming | 面向对象编程

Object-oriented programming (OOP) organises code around objects that combine data and behaviour. A class is a template or blueprint; an object is an instance of a class. Edexcel expects you to understand the main OOP concepts and use them in simple designs.

面向对象编程(OOP)围绕同时包含数据和行为的对象来组织代码。类是模板或蓝图;对象是类的实例。Edexcel 要求你理解主要 OOP 概念,并在简单设计中应用它们。

Encapsulation means hiding the internal state of an object and exposing only necessary operations through methods. Attributes should usually be private and accessed via public methods like getBalance() and setBalance(). This protects data from invalid changes.

封装意味着隐藏对象的内部状态,只通过方法公开必要的操作。属性通常应该是私有的,并通过 getBalance()setBalance() 等公共方法访问。这可以保护数据免受无效修改。

Inheritance allows a class to derive from another class. The subclass inherits all attributes and methods of the superclass and can add or override behaviour. For example, Car and Motorbike can inherit from Vehicle.

继承允许一个类派生自另一个类。子类继承超类的所有属性和方法,并可以添加或重写行为。例如,CarMotorbike 可以从 Vehicle 继承。

Polymorphism means ‘many forms’. It allows objects of different classes to respond to the same method call in their own way. For instance, a draw() method can be defined in Shape and overridden in Circle and Square.

多态意味着“多种形态”。它允许不同类的对象以自己的方式响应同一个方法调用。例如,draw() 方法可以在 Shape 中定义,并在 CircleSquare 中重写。

In Edexcel pseudocode, OOP may be assessed through class diagrams, method definitions or simple inheritance questions. You do not need to write full code in a specific OOP language, but you must understand the terminology and be able to apply it to a scenario.

在 Edexcel 伪代码中,OOP 可能通过类图、方法定义或简单的继承问题来考查。你不需要用特定面向对象语言编写完整代码,但必须理解术语并能将其应用到场景中。


9. Exception Handling and Robust Code | 异常处理与健壮代码

Robust code continues to behave correctly even when it receives unexpected input or encounters runtime errors. Validation and exception handling are the main tools. Validation checks data before it is processed; exception handling responds to errors that occur during execution.

健壮的代码即使在接收到意外输入或遇到运行时错误时也能继续正确运行。验证和异常处理是主要工具。验证在数据处理之前检查数据;异常处理则响应执行期间发生的错误。

Common validation techniques include presence check, range check, type check, length check and format check. For example, an age field should use a range check such as age ≥ 0 AND age ≤ 120.

常见的验证技术包括存在性检查、范围检查、类型检查、长度检查和格式检查。例如,年龄字段应使用范围检查,如 age ≥ 0 AND age ≤ 120

Exception handling can be represented in pseudocode with TRY...EXCEPT...ENDTRY. This structure attempts a block of code; if an error occurs, control passes to the exception block. It prevents a crash and allows the program to display a useful message or recover.

异常处理可以用 TRY...EXCEPT...ENDTRY 表示。该结构尝试执行一段代码;如果发生错误,控制权会转移到异常块。它防止程序崩溃,并允许程序显示有用信息或进行恢复。

For example, attempting to open a file that does not exist should be wrapped in a try-except block. The same applies to invalid numeric conversion or division by zero.

例如,尝试打开不存在的文件时,应将其放入 try-except 块中。无效的数字转换或除以零也同样适用。

Exam solutions should also consider maintainability: use meaningful identifiers, consistent indentation, comments where helpful, and avoid hard-coded magic numbers by using constants.

考试答案还应考虑可维护性:使用有意义的标识符、一致的缩进、适当注释,并使用常量避免硬编码的魔数。


10. Testing and Trace Tables | 测试与跟踪表

Testing is not just a final step; it is part of every programming task. Test data should include normal values, boundary values and erroneous values. A boundary test uses values at the edge of the valid range, such as testing an age of 0, 17, 18, 120 and 121 for a driving check.

测试不只是最后一步;它是每个编程任务的一部分。测试数据应包括正常值、边界值和错误值。边界测试使用有效范围边缘的值,例如对驾驶检查测试年龄 0、17、18、120 和 121。

A trace table records the values of variables as a program runs. It is a powerful tool for understanding algorithms and answering Paper 1 questions. Draw columns for each variable and any conditions being tested, then fill in a new row whenever a value changes.

跟踪表记录程序运行时变量的值。它是理解算法和解答 Paper 1 题目的强大工具。为每个变量和正在测试的条件绘制列,然后在值改变时填写新行。

To trace an algorithm correctly, work line by line and update the table after each statement. For loops, show each iteration separately. For functions, record local variables and return values. This method helps you detect logic errors and explain what a program does.

要正确跟踪算法,请逐行执行,并在每条语句之后更新表格。对于循环,分别显示每次迭代。对于函数,记录局部变量和返回值。这种方法可帮助你发现逻辑错误并解释程序的功能。

A typical trace table for a loop might look like this:

循环的典型跟踪表可能如下:

Iteration i total Condition
Start 1 0 i ≤ 5
1 1 2 TRUE
2 2 4 TRUE

In the on-screen programming assessment, you may also need to use debugging tools and correct runtime errors. Always keep a working version of your code and test incrementally after each small change.

在机考编程评估中,你可能还需要使用调试工具并修正运行时错误。始终保留代码的可运行版本,并在每次小改动后进行增量测试。


11. Exam Tips and Common Pitfalls | 考试技巧与常见失分点

When writing pseudocode in an exam, clarity matters more than syntactic perfection. Use consistent indentation, declare variables, and make your logic readable. If a question asks for a specific construct such as a WHILE loop, do not replace it with a FOR loop unless the mark scheme allows it.

在考试中编写伪代码时,清晰度比语法完美更重要。使用一致的缩进、声明变量,并保持逻辑可读。如果题目要求使用特定结构(如 WHILE 循环),除非评分标准允许,否则不要用 FOR 循环代替。

Common pitfalls include off-by-one errors in array indexing, forgetting to initialise a total before adding to it, and using = for assignment instead of the left arrow. Also, do not confuse MOD and DIV, and remember that string comparison is case-sensitive in most pseudocode.

常见失分点包括数组索引的差一错误、在累加之前忘记初始化总和,以及将赋值写成 = 而不是左箭头。此外,不要混淆 MODDIV,并记住大多数伪代码中字符串比较是区分大小写的。

If a question asks you to describe an algorithm, use precise terms such as ‘divides the array in half’, ‘swaps adjacent elements’ or ‘calls itself with n-1’. Avoid vague phrases like ‘does it quickly’ or ‘keeps going until done’.

如果题目要求描述算法,请使用精确术语,如“将数组分成两半”“交换相邻元素”或“用 n-1 调用自身”。不要使用“很快完成”或“一直做到结束”等模糊表述。

Finally, manage your time. For Paper 1 algorithm questions, spend a few minutes planning the structure before writing. For the on-screen assessment, save and run your code frequently, and keep your test evidence organised for the final report.

最后,要合理分配时间。对于 Paper 1 算法题,在动手写之前花几分钟规划结构。对于机考评估,请经常保存并运行代码,并为最终报告整理好测试证据。

Published by TutorHao | Computer Science Revision Series | aleveler.com

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

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading