A-Level Edexcel Programming: Data Structures, Subroutines and Object-Oriented Programming | Edexcel A-Level 编程:数据结构、子程序与面向对象编程

📚 A-Level Edexcel Programming: Data Structures, Subroutines and Object-Oriented Programming | Edexcel A-Level 编程:数据结构、子程序与面向对象编程

In Edexcel A-Level Computer Science, the programming paper tests your ability to design, write, test and evaluate code using a high-level language. This revision guide covers the core programming techniques: variables and data types, selection and iteration, arrays and collections, subroutines, recursion, and object-oriented principles. Whether you use Python, Java or C#, understanding these foundations is essential for both Paper 2 and the non-exam assessment.

在 Edexcel A-Level 计算机科学中,编程部分考查你使用高级语言设计、编写、测试和评估代码的能力。本复习指南涵盖核心编程技术:变量与数据类型、选择与迭代、数组与集合、子程序、递归以及面向对象原则。无论你使用 Python、Java 还是 C#,理解这些基础对于 Paper 2 和非考试评估都至关重要。


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

A variable is a named storage location in memory whose value can change during program execution. A constant is similar, but its value is fixed once assigned, making the code safer and easier to maintain. Edexcel A-Level programming requires you to declare variables with appropriate data types such as integer, real, Boolean, character, and string.

变量是内存中有名称的存储位置,其值在程序执行期间可以改变。常量类似,但赋值后值固定不变,这使代码更安全、更易维护。Edexcel A-Level 编程要求你使用合适的数据类型声明变量,例如整数、实数、布尔、字符和字符串。

  • Integer: whole numbers, e.g. 0, 7, -42 | 整数:不带小数点的数,如 0、7、-42
  • Real/Float: numbers with decimal parts, e.g. 3.14, -0.001 | 实数/浮点数:带小数部分的数,如 3.14、-0.001
  • Boolean: true or false values only | 布尔:仅真或假两个值
  • Character: single symbol such as ‘A’ or ‘9’ | 字符:单个符号,如 ‘A’ 或 ‘9’
  • String: a sequence of characters, e.g. “hello” | 字符串:字符序列,如 “hello”

Choosing the correct data type affects memory usage and the range of values that can be stored. For example, an 8-bit integer can represent values from −128 to 127, while a 32-bit integer supports a much larger range.

选择正确的数据类型会影响内存使用和可存储值的范围。例如,8 位整数可表示 −128 到 127 的值,而 32 位整数支持更大范围。


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

Operators are symbols that perform operations on operands. Arithmetic operators include +, −, ×, ÷, integer division DIV, and modulus MOD. Relational operators such as =, ≠, <, >, ≤, ≥ compare values and return a Boolean result.

运算符是对操作数执行操作的符号。算术运算符包括 +、−、×、÷、整数除法 DIV 和取模 MOD。关系运算符如 =、≠、<、>、≤、≥ 比较值并返回布尔结果。

Logical operators AND, OR, and NOT combine Boolean expressions. In many languages, the order of operations follows BIDMAS/BODMAS, with parentheses used to override precedence.

逻辑运算符 AND、OR 和 NOT 组合布尔表达式。在许多语言中,运算顺序遵循 BIDMAS/BODMAS 规则,使用括号可以覆盖优先级。

result = (a + b) × c ÷ d MOD e

Modulus is particularly useful for testing divisibility, such as checking whether a number is even by evaluating n MOD 2 = 0. Integer division discards the remainder, which is useful when splitting values into whole units.

取模运算对于测试整除性特别有用,例如通过判断 n MOD 2 = 0 检查一个数是否为偶数。整数除法丢弃余数,在将值拆分为整数单位时非常有用。


3. Selection Statements (IF, ELSE, SWITCH) | 选择语句(IF、ELSE、SWITCH)

Selection allows a program to make decisions and execute different code paths based on conditions. The IF statement tests a Boolean expression; if it evaluates to true, the associated block is executed. An ELSE branch runs when the condition is false, and ELSE IF chains test multiple conditions in order.

选择语句允许程序根据条件做出决策并执行不同的代码路径。IF 语句测试布尔表达式;如果结果为真,则执行相关代码块。ELSE 分支在条件为假时运行,ELSE IF 链按顺序测试多个条件。

A SWITCH or CASE statement is an alternative to long IF…ELSE IF chains. It compares a variable against several constant values and executes the matching branch. This improves readability when there are many discrete options.

SWITCH 或 CASE 语句是长 IF…ELSE IF 链的替代方案。它将变量与多个常量值进行比较,并执行匹配的分支。当存在许多离散选项时,这能提高可读性。

  • IF x > 10 THEN … ENDIF | 如果 x 大于 10,则执行…
  • IF x > 10 THEN … ELSE … ENDIF | 如果 x 大于 10 执行…否则执行…
  • SWITCH day: CASE “Mon”: … CASE “Tue”: … DEFAULT: … | 根据 day 的值分别执行不同分支

Nested selection occurs when an IF statement appears inside another IF statement. While valid, deep nesting can make code harder to debug, so boolean operators are often used to simplify logic.

嵌套选择指一个 IF 语句出现在另一个 IF 语句内部。虽然有效,但过深的嵌套会使代码更难调试,因此常用布尔运算符简化逻辑。


4. Iteration: FOR, WHILE and DO-WHILE Loops | 迭代:FOR、WHILE 与 DO-WHILE 循环

Iteration repeats a block of code. A FOR loop is used when the number of iterations is known in advance, such as processing each element of an array. A WHILE loop repeats while a condition remains true, and is best when the number of iterations is uncertain.

迭代重复执行代码块。当迭代次数事先已知时使用 FOR 循环,例如处理数组中的每个元素。WHILE 循环在条件为真时重复,适用于迭代次数不确定的情况。

A DO-WHILE (or REPEAT-UNTIL) loop executes the block at least once before testing the condition. This guarantees one execution, which is useful for menu-driven programs.

DO-WHILE(或 REPEAT-UNTIL)循环在测试条件之前至少执行一次代码块。这保证至少执行一次,对于菜单驱动程序非常有用。

FOR i = 1 TO 10
  OUTPUT i × i
ENDFOR

Infinite loops occur when the condition never becomes false, so all loops must have a clear exit condition. Tracing variables through each iteration is a common exam skill.

当条件永远不为假时会出现无限循环,因此所有循环必须有明确的退出条件。逐次跟踪变量变化是常见考试技能。


5. Arrays, Lists and Records | 数组、列表与记录

An array is a collection of elements of the same data type stored in contiguous memory locations. Each element is accessed by an index, usually starting at 0 or 1 depending on the language. A list is a dynamic data structure that can grow and shrink at runtime.

数组是相同数据类型元素的集合,存储在连续的内存位置中。每个元素通过索引访问,索引通常根据语言从 0 或 1 开始。列表是一种动态数据结构,可以在运行时增大或缩小。

A record (or struct) groups related fields of different data types under one name. For example, a student record might contain name as string, age as integer, and grade as character. Records are the basis for object-oriented class design.

记录(或结构体)将不同数据类型的相关字段组合在一个名称下。例如,学生记录可能包含字符串类型的姓名、整数类型的年龄和字符类型的等级。记录是面向对象类设计的基础。

  • One-dimensional array: arr[0] = 5, arr[1] = 8, arr[2] = 2 | 一维数组:arr[0] = 5,arr[1] = 8,arr[2] = 2
  • Two-dimensional array: matrix[row][column] | 二维数组:matrix[行][列]
  • Record: Student.name, Student.age, Student.grade | 记录:学生.姓名,学生.年龄,学生.等级

Common array operations include traversal, insertion, deletion, searching, and sorting. Understanding how indices shift after insertion or deletion is critical for algorithmic thinking.

常见的数组操作包括遍历、插入、删除、搜索和排序。理解插入或删除后索引如何移动对于算法思维至关重要。


6. Subroutines: Procedures and Functions | 子程序:过程与函数

A subroutine is a named block of code that can be called from elsewhere in a program. Procedures perform a task but do not return a value, while functions perform a task and return a value to the caller.

子程序是有名称的代码块,可以从程序的其他位置调用。过程执行任务但不返回值,而函数执行任务并向调用者返回一个值。

Using subroutines supports modular programming: code is easier to test, debug, reuse, and maintain. Each subroutine should have a single clear purpose, as stated in its name and interface.

使用子程序支持模块化编程:代码更易于测试、调试、重用和维护。每个子程序应有单一明确的目的,并体现在其名称和接口中。

FUNCTION square(n : INTEGER) RETURNS INTEGER
  RETURN n × n
ENDFUNCTION

Local variables declared inside a subroutine exist only during its execution. Global variables are accessible throughout the program, but their overuse can introduce side effects and make debugging harder.

子程序内部声明的局部变量仅在其执行期间存在。全局变量在整个程序中可访问,但过度使用会引入副作用并使调试更困难。


7. Parameter Passing by Value and by Reference | 按值与按引用传递参数

Parameters allow subroutines to receive input data. Passing by value copies the argument’s value into a new local variable; changes inside the subroutine do not affect the original variable. This is the default behaviour in many languages.

参数允许子程序接收输入数据。按值传递将实参的值复制到一个新的局部变量中;子程序内部的更改不会影响原始变量。这是许多语言的默认行为。

Passing by reference passes the memory address of the argument, so the subroutine can modify the original variable. This is useful when a subroutine needs to update multiple values or return more than one result.

按引用传递传递实参的内存地址,因此子程序可以修改原始变量。当子程序需要更新多个值或返回多个结果时,这非常有用。

  • By value: FUNCTION addOne(x) uses a copy of x | 按值:FUNCTION addOne(x) 使用 x 的副本
  • By reference: PROCEDURE swap(a, b) modifies original a and b | 按引用:PROCEDURE swap(a, b) 修改原始的 a 和 b

In Python, integers and strings are immutable and behave like pass-by-value, while lists and dictionaries behave like pass-by-reference. In Java, primitive types are passed by value, but object references are passed by value of the reference.

在 Python 中,整数和字符串不可变,行为类似按值传递;列表和字典行为类似按引用传递。在 Java 中,基本类型按值传递,但对象引用按引用值的副本传递。


8. Recursion and the Call Stack | 递归与调用栈

Recursion is a technique where a subroutine calls itself to solve a smaller instance of the same problem. Every recursive algorithm must have a base case that stops the recursion and a recursive case that reduces the problem size.

递归是一种技术,子程序调用自身来解决同一问题的更小实例。每个递归算法必须有停止递归的基准情形和缩小问题规模的递归情形。

A classic example is calculating factorial: n! = n × (n−1)! with 0! = 1 as the base case. Another is the Fibonacci sequence, where each term is the sum of the two previous terms.

经典示例是计算阶乘:n! = n × (n−1)!,基准情形为 0! = 1。另一个是斐波那契数列,每个项是前两项之和。

FUNCTION factorial(n)
  IF n = 0 THEN RETURN 1
  ELSE RETURN n × factorial(n − 1)
ENDFUNCTION

Each recursive call is placed on the call stack, which stores return addresses and local variables. If the base case is missing or unreachable, stack overflow occurs. Iteration is often more memory-efficient than recursion, but recursion can express some problems more naturally.

每次递归调用都被放入调用栈,其中存储返回地址和局部变量。如果缺少或无法到达基准情形,就会发生栈溢出。迭代通常比递归更节省内存,但递归能更自然地表达某些问题。


9. Object-Oriented Programming: Classes and Objects | 面向对象编程:类与对象

A class is a blueprint that defines the attributes (data) and methods (functions) of an entity. An object is an instance of a class, created at runtime with its own attribute values. For example, class Car has attributes colour and topSpeed, and methods accelerate() and brake().

类是定义实体属性(数据)和方法(函数)的蓝图。对象是类的实例,在运行时创建并拥有自己的属性值。例如,类 Car 具有属性 colour 和 topSpeed,以及方法 accelerate() 和 brake()。

Encapsulation hides the internal state of an object and only exposes a controlled interface through methods. Attributes are usually declared private, with public getter and setter methods used to access them safely.

封装隐藏对象的内部状态,只通过方法公开受控接口。属性通常声明为私有,使用公共的 getter 和 setter 方法安全地访问它们。

  • Class: Student with attributes name, age, grade | 类:Student,具有属性 name、age、grade
  • Object: s1 = new Student(“Alex”, 17, “A”) | 对象:s1 = new Student(“Alex”, 17, “A”)
  • Method: s1.updateGrade(“B”) | 方法:s1.updateGrade(“B”)

Instantiation creates an object using a constructor method. Constructors initialise attribute values and may accept parameters. Multiple objects of the same class have identical structure but independent state.

实例化使用构造方法创建对象。构造方法初始化属性值并可以接收参数。同一类的多个对象具有相同结构但状态相互独立。


10. Inheritance, Polymorphism and Encapsulation | 继承、多态与封装

Inheritance allows a class to derive properties and methods from a parent class. The child class can add new members or override inherited methods. This promotes code reuse and models hierarchical relationships such as Animal → Mammal → Dog.

继承允许类从父类派生属性和方法。子类可以添加新成员或重写继承的方法。这促进了代码重用,并模拟了 Animal → Mammal → Dog 这样的层次关系。

Polymorphism means “many forms”. A method with the same name can behave differently depending on the object that invokes it. This is achieved through method overriding in subclasses and method overloading with different parameter lists.

多态意为“多种形态”。同名方法可以根据调用它的对象表现不同行为。这通过子类中的方法重写和不同参数列表的方法重载实现。

Encapsulation restricts direct access to an object’s internal data. It is implemented using access modifiers such as private, protected, and public. This protects data integrity and reduces dependencies between components.

封装限制对对象内部数据的直接访问。它通过访问修饰符(如 private、protected 和 public)实现。这保护了数据完整性并减少组件之间的依赖。

  • Inheritance: class Dog extends Animal | 继承:class Dog 继承自 Animal
  • Polymorphism: animal.makeSound() calls Dog.bark() or Cat.meow() | 多态:animal.makeSound() 调用 Dog.bark() 或 Cat.meow()
  • Encapsulation: private int age; public int getAge() | 封装:private int age;public int getAge()

These three principles, together with abstraction, are the foundation of object-oriented design. Edexcel questions often ask you to identify or apply them in code snippets and class diagrams.

这三个原则与抽象一起构成了面向对象设计的基础。Edexcel 考题经常要求你在代码片段和类图中识别或应用它们。


11. File Handling and Exception Handling | 文件处理与异常处理

File handling allows programs to read from and write to persistent storage. Common operations include opening a file in read, write, or append mode, processing records sequentially, and closing the file to release resources.

文件处理允许程序读写持久存储。常见操作包括以读取、写入或追加模式打开文件、顺序处理记录以及关闭文件以释放资源。

Exception handling manages runtime errors without crashing the program. A try block contains code that might fail, while a catch or except block handles specific errors such as file not found, division by zero, or index out of range.

异常处理在不使程序崩溃的情况下管理运行时错误。try 块包含可能失败的代码,而 catch 或 except 块处理特定错误,如文件未找到、除以零或索引超出范围。

  • OPEN file “data.txt” FOR READING | 打开文件 data.txt 进行读取
  • TRY … EXCEPT IOError … FINALLY close file | TRY … EXCEPT IOError … FINALLY 关闭文件

Using a finally block ensures that resources are released whether an error occurs or not. Robust programs validate input, handle exceptions gracefully, and give meaningful error messages to users.

使用 finally 块确保无论是否发生错误都能释放资源。健壮的程序会验证输入、妥善处理异常并向用户提供有意义的错误信息。


12. Algorithm Efficiency and Big O Notation | 算法效率与大 O 表示法

Algorithm efficiency measures how time and memory usage grow as input size increases. Big O notation describes the upper bound of this growth, ignoring constant factors and lower-order terms.

算法效率衡量时间和内存使用如何随输入规模增长。大 O 表示法描述这种增长的上界,忽略常数因子和低阶项。

Linear search: O(n)  |  Binary search: O(log n)  |  Bubble sort: O(n²)

Constant time O(1) is ideal: the algorithm takes the same time regardless of input size, such as accessing an array element by index. Linear time O(n) grows proportionally with input size.

常数时间 O(1) 是理想情况:无论输入大小如何,算法执行时间相同,例如按索引访问数组元素。线性时间 O(n) 与输入规模成比例增长。

Quadratic time O(n²) occurs in nested loops comparing every pair of elements. Logarithmic time O(log n) occurs when the problem space is halved each step, as in binary search. Choosing efficient algorithms is critical for large datasets.

二次时间 O(n²) 出现在比较每对元素的嵌套循环中。对数时间 O(log n) 出现在每步将问题空间减半的情况,如二分查找。选择高效算法对于大数据集至关重要。

  • O(1): array access by index | O(1):按索引访问数组
  • O(log n): binary search on a sorted array | O(log n):有序数组上的二分查找
  • O(n): linear search of an unsorted array | O(n):无序数组上的线性查找
  • O(n²): bubble sort worst case | O(n²):冒泡排序最坏情况

Understanding Big O helps you justify algorithm choice in exam questions and in your own programming project. It also links to data structure selection: hash tables provide O(1) average lookup, while balanced trees provide O(log n).

理解大 O 有助于你在考试题和自己的编程项目中证明算法选择的合理性。它也与数据结构选择相关:哈希表提供平均 O(1) 查找,而平衡树提供 O(log n)。


Published by TutorHao | Programming 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