📚 Programming Operators (Edexcel A-Level) | 编程运算符(Edexcel A-Level)
Operators are fundamental building blocks in programming. They allow a program to perform calculations, compare values, assign data, combine strings, and make logical decisions. In the Edexcel A-Level Computer Science specification, you are expected to understand the behaviour of arithmetic, relational, logical, bitwise, and assignment operators, as well as their precedence in expressions.
运算符是编程中的基本构建块。它们使程序能够执行计算、比较值、分配数据、组合字符串以及做出逻辑判断。在 Edexcel A-Level 计算机科学考试大纲中,你需要理解算术运算符、关系运算符、逻辑运算符、位运算符和赋值运算符的行为,以及它们在表达式中的优先级。
1. What Are Operators? | 什么是运算符?
An operator is a symbol or keyword that performs an operation on one or more operands. For example, in the expression a + b, the plus sign + is the operator, while a and b are operands. Operators are classified by the number of operands they require: unary operators act on one operand, binary operators act on two, and ternary operators act on three.
运算符是对一个或多个操作数执行操作的符号或关键字。例如,在表达式 a + b 中,加号 + 是运算符,而 a 和 b 是操作数。运算符按所需操作数的数量分类:一元运算符作用于一个操作数,二元运算符作用于两个操作数,三元运算符作用于三个操作数。
In A-Level programming questions, you will often be asked to trace expressions and predict the final value of a variable. A clear understanding of how each operator works, including its return type and side effects, is therefore essential for both paper-based and practical programming tasks.
在 A-Level 编程题中,你经常需要跟踪表达式并预测变量的最终值。因此,清楚理解每个运算符的工作方式,包括其返回类型和副作用,对笔试和实践编程任务都至关重要。
2. Arithmetic Operators | 算术运算符
Arithmetic operators perform mathematical calculations. The most common ones are addition +, subtraction –, multiplication *, division /, integer division DIV, modulus MOD, and exponentiation ^. These operators are binary because they require two operands.
算术运算符执行数学计算。最常见的算术运算符包括加法 +、减法 –、乘法 *、除法 /、整数除法 DIV、取模 MOD 和幂运算 ^。这些运算符都是二元的,因为它们需要两个操作数。
For example, if x = 7 and y = 2, then x + y gives 9, x – y gives 5, x * y gives 14, and x / y gives 3.5 in languages that support real division. If integer division is used, the result may differ.
例如,如果 x = 7 且 y = 2,则 x + y 得到 9,x – y 得到 5,x * y 得到 14,x / y 在支持实数除法的语言中得到 3.5。如果使用整数除法,结果可能会有所不同。
x * y = 7 * 2 = 14
x / y = 7 / 2 = 3.5
3. Integer Division and Modulus | 整数除法与取模
Integer division, often written as DIV, returns the whole-number part of a division, discarding any remainder. The modulus operator, written as MOD, returns the remainder after division. These two operators are frequently tested in Edexcel exams because they allow you to extract digits, check divisibility, or cycle through arrays.
整数除法通常写作 DIV,返回除法运算的整数部分,丢弃任何余数。取模运算符写作 MOD,返回除法运算后的余数。这两个运算符在 Edexcel 考试中经常出现,因为它们可用于提取数字、检查整除性或循环遍历数组。
For example, 17 DIV 5 = 3 because 5 goes into 17 three times. 17 MOD 5 = 2 because after subtracting 15 from 17, the remainder is 2. This relationship is summarised by the formula:
例如,17 DIV 5 = 3,因为 5 可以被 17 整除 3 次。17 MOD 5 = 2,因为从 17 中减去 15 后,余数为 2。这种关系可以用以下公式总结:
a = (a DIV b) × b + (a MOD b)
In pseudocode, the exact keyword may vary, but the behaviour is consistent across the specification. You must also be careful when negative numbers are involved, because different languages define the sign of the result differently.
在伪代码中,具体的关键字可能有所不同,但行为在考试大纲中是一致的。你还必须注意涉及负数的情况,因为不同语言对结果符号的定义可能不同。
4. Relational / Comparison Operators | 关系/比较运算符
Relational operators compare two values and return a Boolean result: either TRUE or FALSE. The standard comparison operators are equal to =, not equal to ≠, less than <, greater than >, less than or equal to ≤, and greater than or equal to ≥. In many practical languages, equality is tested with == to avoid confusion with assignment.
关系运算符比较两个值并返回布尔结果:TRUE 或 FALSE。标准比较运算符包括等于 =、不等于 ≠、小于 <、大于 >、小于或等于 ≤ 以及大于或等于 ≥。在许多实际语言中,相等性判断使用 ==,以避免与赋值混淆。
For example, if age = 17, then the condition age >= 18 evaluates to FALSE. Comparison operators are heavily used in selection statements such as IF and in loop conditions such as WHILE.
例如,如果 age = 17,则条件 age >= 18 的值为 FALSE。比较运算符广泛用于选择语句(如 IF)和循环条件(如 WHILE)中。
When comparing strings, comparison is usually based on the internal character codes. This means “A” < “B” is often true because uppercase A has a lower code point than uppercase B. However, the exact ordering can vary, so you should not assume alphabetical order unless the specification states it.
在比较字符串时,比较通常基于内部字符编码。这意味着 “A” < “B” 通常为真,因为大写 A 的编码点低于大写 B。但具体顺序可能有所不同,因此除非考试大纲明确说明,否则不应假设按字母顺序排列。
5. Logical Operators | 逻辑运算符
Logical operators combine Boolean values or expressions. The three core logical operators are AND, OR, and NOT. AND returns true only when both operands are true; OR returns true when at least one operand is true; NOT inverts the Boolean value of its single operand.
逻辑运算符用于组合布尔值或布尔表达式。三个核心逻辑运算符是 AND、OR 和 NOT。AND 仅当两个操作数都为真时返回真;OR 在至少一个操作数为真时返回真;NOT 对其单个操作数的布尔值取反。
For example, the condition age >= 16 AND hasID = TRUE will only be true if both conditions are satisfied. The condition discountApplied OR loyaltyMember = TRUE will be true if either condition is true.
例如,条件 age >= 16 AND hasID = TRUE 只有在两个条件都满足时才为真。条件 discountApplied OR loyaltyMember = TRUE 在任一条件为真时即为真。
Short-circuit evaluation is an important behaviour of many languages: in an AND expression, if the left operand is false, the right operand is not evaluated because the overall result must be false. Similarly, in an OR expression, if the left operand is true, the right operand is not evaluated. This can prevent runtime errors such as division by zero.
短路求值是许多语言的一个重要行为:在 AND 表达式中,如果左操作数为假,则不会计算右操作数,因为整体结果必定为假。类似地,在 OR 表达式中,如果左操作数为真,则不会计算右操作数。这可以防止诸如除以零之类的运行时错误。
6. Bitwise Operators | 位运算符
Bitwise operators perform operations on the individual bits of integer values. The main bitwise operators are bitwise AND, bitwise OR, bitwise XOR, bitwise NOT, left shift <<, and right shift >>. They are used in low-level programming, data compression, graphics, permission flags, and efficient arithmetic.
位运算符对整数值的各个位执行操作。主要的位运算符包括按位 AND、按位 OR、按位 XOR、按位 NOT、左移 << 和右移 >>。它们用于底层编程、数据压缩、图形处理、权限标志以及高效算术运算。
For example, if a = 5 (binary 0101) and b = 3 (binary 0011), then a AND b = 1 because only the least significant bit is 1 in both numbers. The expression a OR b = 7 because any bit set in either number remains set in the result.
例如,如果 a = 5(二进制 0101)且 b = 3(二进制 0011),则 a AND b = 1,因为只有最低有效位在两个数中都为 1。a OR b = 7,因为在任一数中为 1 的位在结果中仍为 1。
Left shifting a number by one position is equivalent to multiplying it by 2, while right shifting is equivalent to integer division by 2. For example, 5 << 1 = 10 and 8 >> 2 = 2. These shortcuts are often used in performance-critical code.
将一个数左移一位相当于将其乘以 2,而右移一位相当于除以 2 的整数除法。例如,5 << 1 = 10,8 >> 2 = 2。这些快捷方式常用于性能关键的代码中。
7. Assignment Operators | 赋值运算符
The basic assignment operator = is used to store a value in a variable. It is important to understand that assignment is not a mathematical equality. The expression x = x + 1 means ‘increment the current value of x by 1’, not that the two sides are equal in the algebraic sense.
基本赋值运算符 = 用于将值存储到变量中。重要的是要理解赋值并非数学上的相等。表达式 x = x + 1 的意思是“将 x 的当前值增加 1”,而不是代数意义上的两边相等。
Compound assignment operators combine an arithmetic operation with assignment. For example, x += 3 is equivalent to x = x + 3, x -= 2 is equivalent to x = x – 2, x *= 4 is equivalent to x = x * 4, and x /= 2 is equivalent to x = x / 2. These forms are shorter and often clearer.
复合赋值运算符将算术运算与赋值相结合。例如,x += 3 等价于 x = x + 3,x -= 2 等价于 x = x – 2,x *= 4 等价于 x = x * 4,x /= 2 等价于 x = x / 2。这些写法更简短,通常也更清晰。
When tracing an assignment, always evaluate the right-hand side fully before assigning the result to the left-hand side. This ensures that the old value of a variable is used in the calculation if it appears on both sides.
在跟踪赋值语句时,总是先完整计算右侧表达式,再将结果赋给左侧变量。当变量同时出现在两侧时,这样可以确保计算中使用的是该变量的旧值。
8. String Concatenation | 字符串连接
String concatenation joins two or more strings into a single string. In many languages, the plus operator + is used for this purpose. For example, “Hello” + ” ” + “World” produces “Hello World”. Some languages use a dedicated operator such as & to avoid ambiguity with numeric addition.
字符串连接将两个或多个字符串合并为一个字符串。在许多语言中,加号 + 用于此目的。例如,“Hello” + ” ” + “World” 生成 “Hello World”。有些语言使用专用运算符(如 &)以避免与数值加法产生歧义。
When concatenating a string with a non-string value, many languages require an explicit conversion. For example, “Age: ” + 16 may cause a type error in a strongly typed language unless the integer 16 is first converted to the string “16”. Always check the data types required by the operator.
在将字符串与非字符串值连接时,许多语言要求显式转换。例如,在强类型语言中,“Age: ” + 16 可能导致类型错误,除非先将整数 16 转换为字符串 “16”。务必检查运算符要求的数据类型。
String concatenation is commonly used to build output messages, generate file paths, or construct database queries. In A-Level exams, you may be asked to trace a small program that builds a string through repeated concatenation inside a loop.
字符串连接常用于构建输出信息、生成文件路径或构造数据库查询。在 A-Level 考试中,你可能会遇到跟踪一个在循环中通过重复连接来构建字符串的小程序的问题。
9. Operator Precedence | 运算符优先级
Operator precedence determines the order in which operations are evaluated in an expression. If no parentheses are used, higher-precedence operators are evaluated first. In most languages, the order from highest to lowest is: parentheses, unary operators, arithmetic operators, relational operators, logical operators, and finally assignment.
运算符优先级决定表达式中运算的执行顺序。如果不使用括号,优先级较高的运算符先计算。在大多数语言中,从高到低的顺序是:括号、一元运算符、算术运算符、关系运算符、逻辑运算符,最后是赋值运算符。
| Precedence | Operators | Examples |
| Highest | Parentheses, unary NOT | (a + b), NOT flag |
| High | Arithmetic * / DIV MOD | x * y, a MOD b |
| Medium | Arithmetic + – | x + y, a – b |
| Lower | Relational < > ≤ ≥ = ≠ | a < b, x >= y |
| Lower still | Logical AND then OR | a AND b, x OR y |
| Lowest | Assignment | 更多咨询请联系16621398022(同微信)
CommentsMore posts |
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导