📚 Operators and Expressions in Programming | 编程中的运算符与表达式
Operators are the building blocks of computational expression: they combine values, compare data, control logic and manipulate binary patterns. In Edexcel A Level Computer Science, you must be able to evaluate expressions precisely, apply correct precedence, and predict the data type and result of an operation under the pseudocode rules used in the exam.
运算符是计算表达式的基本构件:它们组合数值、比较数据、控制逻辑并操纵二进制位模式。在 Edexcel A Level 计算机科学考试中,你必须能够准确计算表达式、应用正确的优先级,并根据考试使用的伪代码规则预测运算的结果和数据类型。
1. The Role of Operators in Programs | 运算符在程序中的作用
An operator is a symbol or keyword that tells the computer to perform a specific operation on one or more operands. Operators turn static data into dynamic computation; without them a program cannot calculate, compare, loop or make decisions.
运算符是告诉计算机对一个或多个操作数执行特定操作的符号或关键字。运算符把静态数据变成动态计算;没有它们,程序就无法计算、比较、循环或作出决策。
In Edexcel pseudocode, the main operator families are arithmetic, assignment, relational, Boolean and bitwise. Each family has precise syntax, operand types and result types, and a single expression may combine several families.
在 Edexcel 伪代码中,主要运算符类别包括算术、赋值、关系、布尔和位运算符。每一类都有明确的语法、操作数类型和结果类型,一个表达式可以组合多个类别。
For example, the expression score + bonus * 2 uses arithmetic operators, while score ≥ 60 AND attendance ≥ 80 uses relational and Boolean operators. Reading expressions correctly is therefore an essential exam skill.
例如,表达式 score + bonus * 2 使用算术运算符,而 score ≥ 60 AND attendance ≥ 80 使用关系和布尔运算符。因此,正确阅读表达式是一项基本考试技能。
2. Arithmetic Operators and Integer Division | 算术运算符与整数除法
Arithmetic operators perform numerical calculations. The common symbols are addition +, subtraction −, multiplication × or *, division /, integer division DIV or //, exponentiation ^ or **, and remainder MOD or %.
算术运算符执行数值计算。常见符号包括加法 +、减法 −、乘法 × 或 *、除法 /、整数除法 DIV 或 //、乘方 ^ 或 **,以及求余 MOD 或 %。
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | addition 加 | 7 + 3 | 10 |
| − | subtraction 减 | 7 − 3 | 4 |
| × | multiplication 乘 | 7 × 3 | 21 |
| / | real division 实数除法 | 7 / 2 | 3.5 |
| DIV | integer quotient 整数商 | 7 DIV 2 | 3 |
| ^ | power 乘方 | 2 ^ 3 | 8 |
In pseudocode, DIV gives the integer part of a division. For example, 17 DIV 5 is 3 because 5 fits into 17 three whole times.
在伪代码中,DIV 给出除法的整数部分。例如,17 DIV 5 等于 3,因为 5 可以完整地放入 17 三次。
A very common exam mistake is to treat / and DIV as identical. In many languages and in Edexcel pseudocode, / returns a real result, while DIV returns an integer result.
一个常见的考试错误是将 / 和 DIV 视为相同。在许多语言和 Edexcel 伪代码中,/ 返回实数结果,而 DIV 返回整数结果。
17 DIV 5 = 3
17 / 5 = 3.4
Exponentiation has higher precedence than division and multiplication, so 2 × 3 ^ 2 means 2 × 9, not 6 ^ 2.
乘方优先于除法和乘法,因此 2 × 3 ^ 2 表示 2 × 9,而不是 6 ^ 2。
3. Modulo and Its Applications | 模运算及其应用
The modulo operator MOD returns the remainder after integer division. For non-negative integers, a MOD b gives the leftover part when a is divided by b.
模运算符 MOD 返回整数除法后的余数。对于非负整数,a MOD b 给出 a 除以 b 后的剩余部分。
17 MOD 5 = 2
The modulo operator is extremely useful for testing divisibility. A number is even if n MOD 2 = 0 and odd if n MOD 2 ≠ 0.
模运算符在判断整除性时非常有用。如果 n MOD 2 = 0,则数字为偶数;如果 n MOD 2 ≠ 0,则为奇数。
It is also used to wrap values around a fixed range, such as converting a 24-hour clock reading into a 12-hour clock face. The expression hour MOD 12 maps 0, 12 and 24 to 0, while 13 maps to 1 and 23 maps to 11.
它还可用于将值环绕到固定范围,例如将 24 小时制时间转换为 12 小时制表盘。hour MOD 12 将 0、12 和 24 映射为 0,13 映射为 1,23 映射为 11。
When working with negative numbers, the sign of MOD can vary between languages. Edexcel pseudocode questions usually focus on non-negative integers, so you should state the remainder as a non-negative value unless the question says otherwise.
对于负数,不同语言中 MOD 的符号可能不同。Edexcel 伪代码题通常关注非负整数,因此除非题目另有说明,你应将余数表示为非负值。
Modulo also appears in hashing, extraction of digits, and cycling through arrays. For example, to access an array of length 10 repeatedly, index MOD 10 keeps the index in the range 0 to 9.
模运算还出现在哈希、提取数字和循环访问数组中。例如,要重复访问长度为 10 的数组,index MOD 10 可以将索引保持在 0 到 9 之间。
4. Assignment and Compound Assignment Operators | 赋值与复合赋值运算符
Assignment stores a value in a variable. In pseudocode, this may be written as variable ← value or variable = value, depending on style. In Python, = is assignment and == is equality comparison.
赋值用于将值存储到变量中。在伪代码中,可以写作 variable ← value 或 variable = value,具体取决于风格。在 Python 中,= 表示赋值,== 表示相等比较。
A key exam skill is to distinguish assignment from comparison. If a question asks whether x = 5, the meaning depends on context: in assignment, the value 5 is copied into x; in comparison, the expression asks whether x currently equals 5.
一个关键的考试技能是区分赋值和比较。如果题目中出现 x = 5,其含义取决于上下文:在赋值中,值 5 被复制到 x;在比较中,表达式询问 x 当前是否等于 5。
Compound assignment operators update a variable using its current value. Examples include count ← count + 1, count += 1 in Python, or count++ in languages such as Java and C.
复合赋值运算符使用变量当前值更新该变量。例如 count ← count + 1、Python 中的 count += 1,或 Java 及 C 等语言中的 count++。
These shorthand forms are useful in loops and accumulators. They do not normally appear as separate operators in Edexcel pseudocode, but you should understand how they are evaluated step by step.
这些简写形式在循环和累加器中很有用。它们通常不作为 Edexcel 伪代码中的独立运算符出现,但你应理解它们如何逐步计算。
x ← x + 5 means add 5 to the current value of x, then store the result back in x.
x ← x + 5 的意思是:将 x 当前值加 5,然后将结果存回 x。
5. Relational and Comparison Operators | 关系运算符与比较运算符
Relational operators compare two values and return a Boolean result: true or false. They are essential in selection statements such as IF and in loop conditions such as WHILE.
关系运算符比较两个值并返回布尔结果:真或假。它们在 IF 等选择语句和 WHILE 等循环条件中至关重要。
| Operator | Meaning | Example that is true |
|---|---|---|
| = | equal to 等于 | 6 = 6 |
| ≠ | not equal to 不等于 | 7 ≠ 4 |
| > | greater than 大于 | 9 > 8 |
| < | less than 小于 | 3 < 5 |
| ≥ | greater than or equal to 大于等于 | 6 ≥ 6 |
| ≤ | less than or equal to 小于等于 | 4 ≤ 4 |
When comparing strings, relational operators usually work in alphabetical or lexicographic order. For example, “apple” <
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课程辅导,国外大学本科硕士研究生博士课程论文辅导