📚 A-Level Edexcel Programming Operators: Arithmetic, Relational, Boolean and String | A-Level Edexcel 编程运算符:算术、关系、布尔与字符串运算
Programming operators are essential building blocks in any program. In the Edexcel A-Level Computer Science specification, you need to use and understand arithmetic, relational, Boolean and string operators confidently in both pseudocode and a chosen programming language such as Python.
编程运算符是任何程序的基本构建块。在 Edexcel A-Level 计算机科学考试大纲中,你需要自信地在伪代码以及 Python 等所选编程语言中使用和理解算术、关系、布尔和字符串运算符。
1. Arithmetic Operators | 算术运算符
Arithmetic operators perform mathematical calculations on numeric operands. The standard arithmetic operators in Edexcel pseudocode are + (addition), – (subtraction), * (multiplication), / (division), MOD (remainder), DIV (integer division) and ^ (exponentiation).
算术运算符对数字操作数执行数学计算。Edexcel 伪代码中的标准算术运算符包括 +(加法)、-(减法)、*(乘法)、/(除法)、MOD(取余)、DIV(整数除法)和 ^(乘方)。
For example, the expression 5 + 2 * 3 evaluates to 11, not 21, because multiplication has higher precedence than addition. You must always apply operator precedence rules.
例如,表达式 5 + 2 * 3 的计算结果是 11,而不是 21,因为乘法的优先级高于加法。你必须始终应用运算符优先级规则。
The table below summarises the main arithmetic operators and their meanings.
下表总结了主要算术运算符及其含义。
| Operator | Name / 名称 | Example / 示例 |
|---|---|---|
| + | Addition / 加法 | 5 + 2 = 7 |
| – | Subtraction / 减法 | 5 – 2 = 3 |
| * | Multiplication / 乘法 | 5 * 2 = 10 |
| / | Division / 除法 | 5 / 2 = 2.5 |
| DIV | Integer division / 整数除法 | 5 DIV 2 = 2 |
| MOD | Remainder / 取余 | 5 MOD 2 = 1 |
| ^ | Exponentiation / 乘方 | 5 ^ 2 = 25 |
2. Integer Division and Modulo | 整数除法与取余
DIV and MOD are very common in Edexcel exam questions. DIV returns the whole number part of a division, while MOD returns the remainder.
DIV 和 MOD 在 Edexcel 考试题中非常常见。DIV 返回除法的整数部分,而 MOD 返回余数。
For instance, 17 DIV 5 = 3 and 17 MOD 5 = 2, because 5 goes into 17 three times with a remainder of 2. These operators are particularly useful when working with digits or repeated patterns.
例如,17 DIV 5 = 3,17 MOD 5 = 2,因为 5 可以整除 17 三次,余数为 2。这些运算符在处理数字位或重复模式时特别有用。
In Python, integer division is written as // and modulo as %. Therefore, 17 // 5 gives 3 and 17 % 5 gives 2.
在 Python 中,整数除法写作 //,取余写作 %。因此,17 // 5 得到 3,17 % 5 得到 2。
A classic exam question asks how to extract the last digit of a number. Since n MOD 10 returns the final digit, this is a direct application of the modulo operator.
经典的考试题会问如何提取一个数的最后一位数字。由于 n MOD 10 返回最后一位数字,这就是取余运算符的直接应用。
3. Relational Operators | 关系运算符
Relational operators compare two values and return a Boolean result: TRUE or FALSE. The Edexcel pseudocode uses =, ≠, <, >, ≤ and ≥.
关系运算符比较两个值并返回布尔结果:TRUE 或 FALSE。Edexcel 伪代码使用 =、≠、<、>、≤ 和 ≥。
These operators are used in IF statements, WHILE loops and REPEAT loops to control the flow of a program. For example, IF score ≥ 70 THEN PRINT ‘Distinction’ uses a relational operator inside a selection statement.
这些运算符用于 IF 语句、WHILE 循环和 REPEAT 循环中,以控制程序的流程。例如,IF score ≥ 70 THEN PRINT ‘Distinction’ 在选择语句中使用了关系运算符。
The table below lists all relational operators with examples.
下表列出了所有关系运算符及示例。
| Operator | Meaning / 含义 | Example / 示例 |
|---|---|---|
| = | Equal to / 等于 | 5 = 5 → TRUE |
| ≠ | Not equal to / 不等于 | 5 ≠ 3 → TRUE |
| < | Less than / 小于 | 3 < 5 → TRUE |
| > | Greater than / 大于 | 7 > 4 → TRUE |
| ≤ | Less than or equal / 小于等于 | 4 ≤ 4 → TRUE |
| ≥ | Greater than or equal / 大于等于 | 6 ≥ 5 → TRUE |
4. Boolean Operators | 布尔运算符
Boolean operators combine two or more Boolean expressions. The three fundamental Boolean operators are AND, OR and NOT, and Edexcel also expects you to understand XOR.
布尔运算符组合两个或多个布尔表达式。三个基本布尔运算符是 AND、OR 和 NOT,Edexcel 还要求你理解 XOR。
AND returns TRUE only if both operands are TRUE. OR returns TRUE if at least one operand is TRUE. NOT reverses the truth value of its operand. XOR returns TRUE when exactly one operand is TRUE.
AND 仅当两个操作数都为 TRUE 时返回 TRUE。OR 在至少一个操作数为 TRUE 时返回 TRUE。NOT 反转其操作数的真值。XOR 在恰好一个操作数为 TRUE 时返回 TRUE。
For example, the condition age ≥ 18 AND passed = TRUE is only TRUE when both conditions are met. Practical programming uses Boolean operators in compound IF statements and WHILE loop guards.
例如,条件 age ≥ 18 AND passed = TRUE 只有在两个条件都满足时才为 TRUE。实际编程中,布尔运算符用于复合 IF 语句和 WHILE 循环条件中。
Truth tables are a useful way to summarise Boolean operators. You should be able to complete a truth table for AND, OR, NOT and XOR quickly in the exam.
真值表是总结布尔运算符的有用方法。你应该能够在考试中快速完成 AND、OR、NOT 和 XOR 的真值表。
5. String Operations | 字符串运算
String operators allow you to manipulate text. The most common string operation is concatenation, which joins two strings together using the + operator in most languages.
字符串运算符允许你操作文本。最常见的字符串运算是连接(concatenation),在大多数语言中使用 + 运算符将两个字符串连接在一起。
In pseudocode, concatenation can be written with + or sometimes with &. For example, fullName ← firstName + ‘ ‘ + lastName joins three strings into one. In Python, the same is achieved with the + operator.
在伪代码中,连接可以用 + 或有时用 & 表示。例如,fullName ← firstName + ‘ ‘ + lastName 将三个字符串连接为一个。在 Python 中,使用 + 运算符实现相同功能。
Other string operations include finding the length of a string, accessing individual characters by index, and extracting substrings. In Python, len(text), text[0] and text[1:4] are commonly used.
其他字符串操作包括求字符串长度、通过索引访问单个字符以及提取子串。在 Python 中,常用 len(text)、text[0] 和 text[1:4]。
Remember that strings must be enclosed in quotation marks. The expression ‘5’ + ‘3’ gives the string ’53’, not the number 8, because both operands are text, not numbers.
请记住,字符串必须用引号括起来。表达式 ‘5’ + ‘3’ 得到字符串 ’53’,而不是数字 8,因为两个操作数都是文本而不是数字。
6. Operator Precedence | 运算符优先级
Operator precedence determines the order in which operations are evaluated in an expression. Without a clear understanding of precedence, you will make calculation errors in the exam.
运算符优先级决定了表达式中运算的求值顺序。如果不清楚优先级规则,你会在考试中犯计算错误。
In Edexcel pseudocode, the order is: parentheses first, then exponentiation, then multiplication, division, DIV and MOD, then addition and subtraction. Relational operators are evaluated next, followed by NOT, then AND, then OR.
在 Edexcel 伪代码中,顺序为:首先是括号,然后是乘方,接着是乘法、除法、DIV 和 MOD,然后是加法和减法。关系运算符随后求值,其次是 NOT,然后是 AND,最后是 OR。
Consider the expression below.
考虑下面的表达式。
(2 + 3) * 4 ^ 2 = 5 * 16 = 80
Without parentheses, the expression 2 + 3 * 4 ^ 2 evaluates to 2 + 3 * 16 = 2 + 48 = 50. This shows how dramatically parentheses can change a result.
如果没有括号,表达式 2 + 3 * 4 ^ 2 的计算结果为 2 + 3 * 16 = 2 + 48 = 50。这表明括号可以极大改变计算结果。
7. Assignment and Expressions | 赋值与表达式
Assignment stores a value in a variable. In Edexcel pseudocode, assignment is shown with a left-pointing arrow ←, while Python uses the equals sign =.
赋值将值存储在变量中。在 Edexcel 伪代码中,赋值用左箭头 ← 表示,而 Python 使用等号 =。
An expression is any combination of values, variables and operators that evaluates to a single value. For example, area ← length * width evaluates the expression on the right and assigns the result to the variable area on the left.
表达式是值、变量和运算符的任意组合,它们计算出一个单一的值。例如,area ← length * width 计算右侧表达式,并将结果赋给左侧变量 area。
Be careful: in pseudocode, = is a relational operator, not assignment. Do not write x = x + 1 as a comparison; use x ← x + 1 to increment by one.
请注意:在伪代码中,= 是关系运算符,而不是赋值运算符。不要将 x = x + 1 写成比较;应使用 x ← x + 1 表示加一。
In Python, assignment uses =, so x = x + 1 is perfectly valid. You must switch between pseudocode and Python notation without confusion.
在 Python 中,赋值使用 =,因此 x = x + 1 是完全有效的。你必须能够在伪代码和 Python 表示法之间切换而不混淆。
8. Operators in Pseudocode and Python | 伪代码与 Python 中的运算符
Edexcel questions may ask you to write pseudocode or to interpret a short Python program. You need to recognise the equivalent operators in both notations.
Edexcel 考试题可能要求你编写伪代码或解释简短的 Python 程序。你需要识别两种表示法中等价的运算符。
The table below shows the most important differences between pseudocode operators and Python operators.
下表显示了伪代码运算符与 Python 运算符之间最重要的区别。
| Pseudocode / 伪代码 | Python | Meaning / 含义 |
|---|---|---|
| ^ | ** | Exponentiation / 乘方 |
| DIV | // | Integer division / 整数除法 |
| MOD | % | Remainder / 取余 |
| = | == | Equal comparison / 等于比较 |
| ≠ | != | Not equal / 不等于 |
| AND | and | Logical AND / 逻辑与 |
| OR | or | Logical OR / 逻辑或 |
| NOT | not | Logical NOT / 逻辑非 |
Always use the notation required by the question. If the question says ‘write in pseudocode’, do not write Python-specific operators like // or **.
始终使用题目要求的表示法。如果题目要求“用伪代码编写”,不要写 // 或 ** 这类 Python 专用运算符。
9. Common Exam Pitfalls | 常见考试易错点
One common mistake is confusing assignment with comparison. In pseudocode, = means equality, not assignment, so use ← to assign values.
一个常见错误是混淆赋值与比较。在伪代码中,= 表示相等关系,而不是赋值,因此应使用 ← 来赋值。
Another pitfall is ignoring integer division. Many students write 5 / 2 = 2.5 when the question asked for 5 DIV 2, which gives 2. Always check whether the operator is / or DIV.
另一个易错点是忽略整数除法。许多学生在题目要求 5 DIV 2 时写出 5 / 2 = 2.5,而后者结果为 2。务必检查运算符是 / 还是 DIV。
Misapplying precedence is also common. For example, 8 + 2 * 3 equals 14, not 30. Use parentheses whenever the order is ambiguous or important.
错误地应用优先级也很常见。例如,8 + 2 * 3 等于 14,而不是 30。只要顺序有歧义或很重要,就使用括号。
Finally, remember that string and numeric operations are different. ’10’ + ‘5’ gives ‘105’, while 10 + 5 gives 15. Do not mix data types without explicit conversion.
最后,请记住字符串运算和数值运算不同。’10’ + ‘5’ 得到 ‘105’,而 10 + 5 得到 15。不要在没有显式转换的情况下混合数据类型。
10. Exam-Style Examples | 考试风格示例
Let us look at two typical Edexcel-style questions on operators. First, evaluate the expression below when a = 10, b = 3 and c = 2.
让我们看两道典型的 Edexcel 风格运算符题目。首先,当 a = 10、b = 3 和 c = 2 时,计算下列表达式。
(a DIV b) + (a MOD b) * c
Substitute the values: (10 DIV 3) + (10 MOD 3) * 2 = 3 + 1 * 2 = 3 + 2 = 5. The correct answer is 5.
代入数值:(10 DIV 3) +
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课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply