📚 Programming Fundamentals: Data Types, Operators and Control Structures | 编程基础:数据类型、运算符与控制结构
In Edexcel A Level Computer Science, programming questions require a clear understanding of how data is stored, how operations manipulate that data, and how control structures determine the flow of execution. This article consolidates the core programming concepts you will need for Paper 2, including data types, operators, selection, iteration, subroutines, recursion, and file handling. Mastering these fundamentals allows you to trace pseudocode accurately and write robust solutions under exam conditions.
在 Edexcel A Level 计算机科学考试中,编程题要求你清晰理解数据如何存储、运算符如何操作数据以及控制结构如何决定执行流程。本文整合了 Paper 2 所需的核心编程概念,包括数据类型、运算符、选择、迭代、子程序、递归和文件处理。掌握这些基础知识能让你在考试中准确追踪伪代码并写出稳健的解题方案。
1. Programming Paradigms and Language Types | 编程范式与语言类型
A programming paradigm is a fundamental style of writing code. The most common paradigms in the Edexcel specification are imperative, object-oriented, and functional. Imperative programming focuses on giving the computer a sequence of commands to change state, while object-oriented programming organises code around objects that combine data and behaviour.
编程范式是编写代码的基本风格。Edexcel 大纲中最常见的范式包括命令式、面向对象和函数式。命令式编程侧重于给计算机一系列命令以改变状态,而面向对象编程围绕对象组织代码,将数据和行为结合在一起。
Edexcel pseudocode is largely imperative and procedural. You are expected to write algorithms using sequence, selection, and iteration, and to know how these constructs translate into high-level languages such as Python or Java. Understanding paradigms helps you recognise different solution styles in exam questions.
Edexcel 伪代码主要是命令式和过程式的。考试要求你能够使用顺序、选择和迭代来编写算法,并知道这些结构如何转换为 Python 或 Java 等高级语言。理解范式有助于你在考题中识别不同的解题风格。
2. Data Types and Variables | 数据类型与变量
A variable is a named storage location whose value can change during program execution. Every variable has a data type that defines the kind of value it can hold. The core data types in Edexcel pseudocode are integer, real, Boolean, character, and string.
变量是一个命名的存储位置,其值在程序执行期间可以改变。每个变量都有一个数据类型,定义了它可以保存的值的种类。Edexcel 伪代码中的核心数据类型包括整数、实数、布尔值、字符和字符串。
- Integer stores whole numbers, e.g. 3, -12, 0. 整数存储整数,例如 3、-12、0。
- Real stores numbers with fractional parts, e.g. 3.14, -0.5. 实数存储带小数部分的数字,例如 3.14、-0.5。
- Boolean stores only TRUE or FALSE. 布尔值只存储 TRUE 或 FALSE。
- Character stores a single symbol such as ‘A’ or ‘7’. 字符存储单个符号,例如 ‘A’ 或 ‘7’。
- String stores a sequence of characters, e.g. “hello”. 字符串存储字符序列,例如 “hello”。
When declaring variables in pseudocode, Edexcel often uses the DECLARE keyword, for example DECLARE age : INTEGER. Strong typing means that a variable cannot normally change type after declaration, although Python is dynamically typed and allows reassignment to a different type.
在伪代码中声明变量时,Edexcel 通常使用 DECLARE 关键字,例如 DECLARE age : INTEGER。强类型意味着变量在声明后通常不能改变类型,尽管 Python 是动态类型的,允许重新赋值为不同类型。
3. Constants and Literals | 常量与字面量
A constant is similar to a variable, but its value cannot be changed once assigned. Constants are useful for values that remain fixed throughout a program, such as VAT_RATE or PI. Using constants improves readability and reduces the chance of errors caused by repeating magic numbers.
常量与变量类似,但一旦赋值就不能更改其值。常量适用于在整个程序中保持固定的值,例如 VAT_RATE 或 PI。使用常量可以提高可读性,并减少因重复出现魔法数字而导致的错误。
A literal is a fixed value written directly in the source code, such as 42, 3.14, ‘A’, or “hello”. Literals are often used to initialise variables or constants. For example, the statement DECLARE MAX_SCORE : INTEGER = 100 assigns the integer literal 100 to the constant MAX_SCORE.
字面量是直接写在源代码中的固定值,例如 42、3.14、’A’ 或 “hello”。字面量通常用于初始化变量或常量。例如,语句 DECLARE MAX_SCORE : INTEGER = 100 将整数字面量 100 赋给常量 MAX_SCORE。
4. Arithmetic Operators | 算术运算符
Arithmetic operators perform mathematical calculations on numeric data. The standard operators in Edexcel pseudocode are addition, subtraction, multiplication, and division. Integer division and modulus are also important for many algorithms.
算术运算符对数值数据执行数学计算。Edexcel 伪代码中的标准运算符包括加法、减法、乘法和除法。整数除法和取模运算在许多算法中也很重要。
- + addition 加法
- – subtraction 减法
- * multiplication 乘法
- / division (real result) 除法(实数结果)
- DIV integer division (quotient) 整数除法(商)
- MOD modulus (remainder) 取模(余数)
For example, 7 / 2 yields 3.5, while 7 DIV 2 yields 3, and 7 MOD 2 yields 1. These distinctions are essential when tracing pseudocode that uses whole-number arithmetic, such as finding the number of coins in a change-making problem.
例如,7 / 2 得到 3.5,而 7 DIV 2 得到 3,7 MOD 2 得到 1。在追踪使用整数算术的伪代码时,这些区别至关重要,例如在找零问题中计算硬币数量。
The standard order of operations applies, often remembered as BIDMAS or BODMAS: brackets, indices, division and multiplication, addition and subtraction. Use brackets to make the intended order explicit in complex expressions.
标准运算顺序适用,通常用 BIDMAS 或 BODMAS 记忆:括号、指数、除法和乘法、加法和减法。在复杂表达式中,应使用括号明确表达预期顺序。
5. Comparison and Boolean Operators | 比较运算符与布尔运算符
Comparison operators compare two values and produce a Boolean result. The Edexcel pseudocode operators are: = (equal to), ≠ (not equal to), < (less than), > (greater than), ≤ (less than or equal to), and ≥ (greater than or equal to).
比较运算符比较两个值并产生布尔结果。Edexcel 伪代码中的运算符为:=(等于)、≠(不等于)、<(小于)、>(大于)、≤(小于等于)和 ≥(大于等于)。
Boolean operators combine Boolean expressions. The three fundamental operators are AND, OR, and NOT. AND returns TRUE only if both operands are TRUE; OR returns TRUE if at least one operand is TRUE; NOT inverts the Boolean value.
布尔运算符组合布尔表达式。三个基本运算符是 AND、OR 和 NOT。AND 仅当两个操作数都为 TRUE 时返回 TRUE;OR 当至少一个操作数为 TRUE 时返回 TRUE;NOT 反转布尔值。
A truth table summarises the behaviour of these operators:
真值表总结了这些运算符的行为:
| A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE | FALSE |
| TRUE | FALSE | FALSE | TRUE | FALSE |
| FALSE | TRUE | FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE | FALSE | TRUE |
When combining multiple Boolean expressions, use brackets to avoid ambiguity. For example, the condition (age ≥ 18) AND (age ≤ 65) is TRUE for all values of age between 18 and 65 inclusive.
组合多个布尔表达式时,应使用括号避免歧义。例如,条件 (age ≥ 18) AND (age ≤ 65) 对于 18 到 65 之间(含两端)的所有 age 值均为 TRUE。
6. Sequence, Selection and Iteration | 顺序、选择与迭代
All imperative programs are built from three basic control structures: sequence, selection, and iteration. Sequence means statements execute one after another in the order written. This is the default flow of control.
所有命令式程序都由三种基本控制结构构建:顺序、选择和迭代。顺序意味着语句按编写顺序一条接一条执行。这是默认的控制流程。
Selection allows the program to choose between different paths based on a condition. Edexcel pseudocode uses IF…THEN…ELSE…ENDIF statements. A simple example:
选择允许程序根据条件在不同路径之间进行选择。Edexcel 伪代码使用 IF…THEN…ELSE…ENDIF 语句。一个简单例子:
IF marks ≥ 50 THEN Grade ← ‘Pass’ ELSE Grade ← ‘Fail’ ENDIF
Iteration repeats a block of code. Edexcel pseudocode includes three forms: WHILE…ENDWHILE (pre-condition loop), REPEAT…UNTIL (post-condition loop), and FOR…ENDFOR (count-controlled loop). Selecting the correct loop structure is a common exam skill.
迭代重复一段代码。Edexcel 伪代码包括三种形式:WHILE…ENDWHILE(前置条件循环)、REPEAT…UNTIL(后置条件循环)和 FOR…ENDFOR(计数控制循环)。选择正确的循环结构是一项常见的考试技能。
A WHILE loop checks the condition before each iteration, so the body may never execute if the condition is initially FALSE. A REPEAT loop executes the body at least once, because the condition is checked after the body.
WHILE 循环在每次迭代前检查条件,因此如果条件最初为 FALSE,循环体可能根本不执行。REPEAT 循环至少执行一次循环体,因为条件在循环体之后才检查。
7. Subroutines: Procedures and Functions | 子程序:过程与函数
A subroutine is a named block of code that can be called from elsewhere in a program. Subroutines support modular programming, reduce duplication, and make code easier to test and debug. Edexcel distinguishes between procedures and functions.
子程序是一个命名的代码块,可以从程序的其他地方调用。子程序支持模块化编程、减少重复,并使代码更易于测试和调试。Edexcel 区分过程和函数。
A procedure performs a task but does not return a value. A function performs a task and returns exactly one value. In pseudocode, procedures are declared using PROCEDURE, and functions are declared using FUNCTION.
过程执行任务但不返回值。函数执行任务并返回一个值。在伪代码中,过程用 PROCEDURE 声明,函数用 FUNCTION 声明。
For example, a procedure to display a greeting might be:
例如,显示问候语的过程可能是:
PROCEDURE greet(name) OUTPUT ‘Hello ‘ + name ENDPROCEDURE
A function to calculate the square of a number could be:
计算一个数平方的函数可能是:
FUNCTION square(n) RETURN n × n ENDFUNCTION
8. Parameters and Return Values | 参数与返回值
Parameters are variables listed in a subroutine declaration that receive values from the calling code. There are two main parameter passing methods: by value and by reference. By value passes a copy of the data, so changes inside the subroutine do not affect the original variable. By reference passes the memory address, so changes do affect the original.
参数是子程序声明中列出的变量,用于接收来自调用代码的值。参数传递主要有两种方法:按值传递和按引用传递。按值传递传递数据的副本,因此子程序内部的更改不会影响原始变量。按引用传递传递内存地址,因此更改会影响原始变量。
In Edexcel pseudocode, parameters are usually listed in parentheses after the subroutine name. For example, PROCEDURE updateScore(score) declares one parameter named score. The return value of a function is sent back using the RETURN statement.
在 Edexcel 伪代码中,参数通常列在子程序名称后的括号中。例如,PROCEDURE updateScore(score) 声明了一个名为 score 的参数。函数的返回值通过 RETURN 语句发送回去。
When tracing subroutine calls, you must carefully match arguments to parameters in order. The number and types of arguments should match the parameter list. A common exam mistake is to forget that local variables inside a subroutine are separate from variables with the same name elsewhere in the program.
追踪子程序调用时,必须仔细按顺序将实参与形参匹配。实参的数量和类型应与形参列表匹配。一个常见的考试错误是忘记子程序内部的局部变量与程序中其他位置同名变量是相互独立的。
9. Recursion | 递归
Recursion is a technique where a function calls itself to solve a smaller instance of the same problem. A recursive subroutine must have at least one base case, which stops the recursion, and one recursive case, which breaks the problem down and calls itself with modified arguments.
递归是一种函数调用自身以解决同一问题较小实例的技术。递归子程序必须至少有一个基本情况(停止递归)和一个递归情况(分解问题并以修改后的参数调用自身)。
A classic example is the factorial function. The base case is factorial(0) = 1. The recursive case is factorial(n) = n × factorial(n – 1) for n > 0. This can be written in Edexcel pseudocode as:
一个经典例子是阶乘函数。基本情况是 factorial(0) = 1。递归情况是对于 n > 0,factorial(n) = n × factorial(n – 1)。用 Edexcel 伪代码可写为:
FUNCTION factorial(n) IF n = 0 THEN RETURN 1 ELSE RETURN n × factorial(n – 1) ENDIF ENDFUNCTION
Recursion can be elegant but must be used carefully to avoid infinite recursion. Each recursive call should bring the algorithm closer to the base case. Recursion also uses stack memory, so deeply recursive calls can cause a stack overflow.
递归可以很优雅,但必须谨慎使用以避免无限递归。每次递归调用都应使算法更接近基本情况。递归也使用栈内存,因此深度递归调用可能导致栈溢出。
10. File Handling and Exception Handling | 文件处理与异常处理
File handling allows programs to read data from and write data to persistent storage. Edexcel pseudocode uses statements such as OPEN, READ, WRITE, and CLOSE. Files can be opened in read mode, write mode, or append mode.
文件处理允许程序从持久存储中读取数据和向其中写入数据。Edexcel 伪代码使用 OPEN、READ、WRITE 和 CLOSE 等语句。文件可以以读取、写入或追加模式打开。
A typical pseudocode sequence to read a text file might be:
读取文本文件的典型伪代码序列可能是:
OPEN ‘data.txt’ FOR READ
WHILE NOT EOF
READ line
OUTPUT line
ENDWHILE
CLOSE ‘data.txt’
Exception handling deals with runtime errors such as division by zero, file not found, or invalid user input. Edexcel pseudocode may use TRY…EXCEPT…ENDTRY blocks to catch and handle these errors gracefully instead of letting the program crash.
异常处理处理运行时错误,例如除零、文件未找到或无效的用户输入。Edexcel 伪代码可能使用 TRY…EXCEPT…ENDTRY 块来捕获并优雅地处理这些错误,而不是让程序崩溃。
Understanding file handling and exceptions is important for the Paper 2 scenario-based programming questions, where you may be asked to write robust code that validates input and handles missing files.
理解文件处理和异常处理对于 Paper 2 中基于场景的编程题非常重要,在这些题目中,你可能需要编写验证输入并处理缺失文件的稳健代码。
Published by TutorHao | Programming Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导