📚 Edexcel A-Level Programming: Operators and Expressions | Edexcel A-Level 编程:运算符与表达式
In Edexcel A-Level Computer Science, operators are symbols that tell a program to perform specific actions on one or more operands. Understanding operators and expression evaluation is essential for writing correct pseudocode, predicting output, and answering Paper 2 programming questions.
在 Edexcel A-Level 计算机科学中,运算符是告诉程序对一个或多个操作数执行特定动作的符号。理解运算符和表达式求值对于写出正确的伪代码、预测输出以及回答 Paper 2 编程题至关重要。
1. Operator Basics | 运算符基础
An operator is a symbol that carries out a calculation or comparison. An operand is the value the operator acts on. An expression combines operators and operands to produce a single value. For example, in 3 + 5, + is the operator and 3, 5 are operands. Edexcel expects you to classify operators by category and apply them in pseudocode and a high-level language such as Python.
运算符是执行计算或比较的符号。操作数是运算符作用的值。表达式将运算符和操作数组合起来,产生一个单一的值。例如,在 3 + 5 中,+ 是运算符,3 和 5 是操作数。Edexcel 希望你按类别划分运算符,并在伪代码和 Python 等高级语言中应用它们。
There are five main categories of operators you need to know: arithmetic, relational, Boolean, assignment, and string operators. Each category has its own rules, but they all follow the same principle of taking inputs and returning an output value.
你需要掌握五类主要运算符:算术运算符、关系运算符、布尔运算符、赋值运算符和字符串运算符。每一类都有自己的规则,但它们都遵循相同的原则:接收输入并返回输出值。
2. Arithmetic Operators | 算术运算符
Arithmetic operators perform mathematical calculations. The main arithmetic operators are addition (+), subtraction (−), multiplication (*), real division (/), integer division (DIV or //), and modulus (MOD or %). These are used constantly in algorithms for totals, averages, remainders and indexing.
算术运算符执行数学计算。主要算术运算符包括加法 (+)、减法 (−)、乘法 (*)、实数除法 (/)、整除 (DIV 或 //) 和取模 (MOD 或 %)。这些运算符在算法中常用于求和、求平均值、求余数和索引。
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | addition | 7 + 2 | 9 |
| – | subtraction | 7 – 2 | 5 |
| * | multiplication | 7 * 2 | 14 |
| / | real division | 7 / 2 | 3.5 |
| DIV or // | integer division | 7 DIV 2 | 3 |
| MOD or % | remainder | 7 MOD 2 | 1 |
When a question asks for integer division or remainder, do not use real division. In pseudocode, write DIV and MOD clearly. In Python, use // for integer division and % for modulus.
当题目要求整除或求余数时,不要使用实数除法。在伪代码中,清楚地写出 DIV 和 MOD。在 Python 中,使用 // 进行整除,使用 % 取模。
3. Integer Division and Modulus | 整除与取模
DIV gives the whole-number quotient when one integer is divided by another, while MOD gives the remainder. These are especially useful in problems involving groups, cycles, digits, and array indexing. For example, 17 DIV 5 = 3 and 17 MOD 5 = 2 because 17 = 5 × 3 + 2. Many exam questions ask you to trace MOD in loops for even/odd detection or circular buffers.
DIV 给出两个整数相除时的整数商,而 MOD 给出余数。它们在涉及分组、循环、数字和数组索引的问题中特别有用。例如,17 DIV 5 = 3,17 MOD 5 = 2,因为 17 = 5 × 3 + 2。许多考试题要求你在循环中跟踪 MOD,用于判断奇偶或循环缓冲区。
n MOD 2 = 0 → even; n MOD 2 = 1 → odd
A classic exam scenario is extracting the last digit of an integer: lastDigit ← number MOD 10. To remove the last digit, use number ← number DIV 10. This is the basis for digit-sum algorithms and base conversion.
一个经典的考试场景是提取整数的最后一位:lastDigit ← number MOD 10。要移除最后一位,使用 number ← number DIV 10。这是数字求和算法和进制转换的基础。
4. Relational and Comparison Operators | 关系与比较运算符
Relational operators compare two values and return a Boolean result: TRUE or FALSE. The six common operators are =, ≠, <, >, ≤, ≥. In Edexcel pseudocode, you may see =, <>, <, >, <=, >=. These operators are used in conditions for IF statements, WHILE loops, and REPEAT loops.
关系运算符比较两个值并返回布尔结果:TRUE 或 FALSE。六个常见关系运算符是 =、≠、<、>、≤、≥。在 Edexcel 伪代码中,你可能看到 =、<>、<、>、<=、>=。这些运算符用于 IF 语句、WHILE 循环和 REPEAT 循环的条件中。
| Pseudocode | Python | Meaning |
|---|---|---|
| = | == | equal to |
| <> or != | != | not equal to |
| < | < | less than |
| > | > | greater than |
| <= | <= | less than or equal to |
| >= | >= | greater than or equal to |
Remember that a comparison produces a Boolean value, not a number. For example, 5 > 3 evaluates to TRUE, and you can assign that result to a Boolean variable.
请记住,比较产生的是布尔值,而不是数字。例如,5 > 3 的求值结果为 TRUE,你可以将该结果赋给一个布尔变量。
5. Boolean Logical Operators | 布尔逻辑运算符
Boolean operators combine or invert Boolean values. The three fundamental operators are AND, OR, and NOT. Their truth tables are essential. AND returns TRUE only when both operands are TRUE. OR returns TRUE when at least one operand is TRUE. NOT reverses a single Boolean value.
布尔运算符用于组合或取反布尔值。三个基本运算符是 AND、OR 和 NOT。它们的真值表非常重要。AND 仅在两个操作数都为 TRUE 时返回 TRUE。OR 在至少一个操作数为 TRUE 时返回 TRUE。NOT 对单个布尔值取反。
| 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 |
Compound conditions such as age >= 18 AND passed = TRUE require both parts to be true. In Python, write and, or, not in lowercase. In pseudocode, use AND, OR, NOT.
复合条件如 age >= 18 AND passed = TRUE 要求两个部分都为真。在 Python 中,使用小写的 and、or、not。在伪代码中,使用 AND、OR、NOT。
6. String Concatenation | 字符串连接
Concatenation joins strings end-to-end. In Edexcel pseudocode and Python, the plus sign + is often used. For example, “Hello” + ” ” + “World” produces “Hello World”. However, some pseudocode styles use the & operator or explicit CONCATENATE. You must know that mixing a string and an integer normally causes a type error unless the integer is converted first.
连接将字符串首尾相连。在 Edexcel 伪代码和 Python 中,通常使用加号 +。例如,”Hello” + ” ” + “World” 产生 “Hello World”。不过,有些伪代码风格使用 & 运算符或显式的 CONCATENATE。你必须知道,将字符串和整数混用通常会导致类型错误,除非先将整数转换。
name ← “Alice”
age ← 17
output ← name + ” is ” + str(age)
When concatenating numeric variables into strings, use a conversion function such as str() in Python or INT_TO_STRING in pseudocode. Similarly, convert strings to numbers with int() or float() before arithmetic.
将数值变量连接成字符串时,请使用转换函数,如 Python 中的 str() 或伪代码中的 INT_TO_STRING。同样,在进行算术运算前,应使用 int() 或 float() 将字符串转换为数字。
7. Assignment and Compound Assignment | 赋值与复合赋值
The assignment operator stores a value in a variable. Edexcel pseudocode uses the left arrow ←, while Python uses =. Compound assignment combines an arithmetic operation with assignment, such as +=, -=, *=, /=. For example, total ← total + score is equivalent to total += score.
赋值运算符将值存储到变量中。Edexcel 伪代码使用左箭头 ←,而 Python 使用 =。复合赋值将算术运算与赋值结合起来,例如 +=、-=、*=、/=。例如,total ← total + score 等价于 total += score。
count ← 0
count ← count + 1
count += 1
Do not confuse assignment with equality. In Python, = assigns a value, while == tests equality. In pseudocode, ← assigns and = tests equality. This is one
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课程辅导,国外大学本科硕士研究生博士课程论文辅导