📚 Operators, Expressions and Precedence | 运算符、表达式与优先级
Operators are the core building blocks that let a program calculate, compare, combine, and make decisions. For Edexcel A Level programming, you need to identify each operator type, apply correct precedence, and trace expressions accurately.
运算符是让程序进行计算、比较、组合和决策的核心构建块。在 Edexcel A-Level 编程中,你需要识别每种运算符类型、正确应用优先级,并准确跟踪表达式求值过程。
1. What Is an Operator? | 什么是运算符?
An operator is a symbol or keyword that performs an action on one or more operands. Operands can be literals, variables, function calls, or entire sub-expressions.
运算符是对一个或多个操作数执行特定动作的符号或关键字。操作数可以是字面量、变量、函数调用,或者整个子表达式。
In pseudocode, a simple expression such as a + b contains the operator + and the two operands a and b. Operators are classified as unary, binary, or ternary depending on how many operands they act on.
在伪代码中,像 a + b 这样的简单表达式包含运算符 + 和两个操作数 a、b。根据作用的操作数数量不同,运算符可分为一元、二元或三元运算符。
Edexcel questions often require you to evaluate mixed expressions, so a clear mental model of operands and operators is essential before moving on to precedence.
Edexcel 考题常常要求你计算混合表达式,因此在对优先级进行深入之前,必须首先建立清晰的操作数与运算符心智模型。
2. Arithmetic Operators | 算术运算符
Arithmetic operators perform mathematical calculations. In Edexcel pseudocode you will commonly see +, −, ×, /, DIV, MOD, and ^.
算术运算符执行数学计算。在 Edexcel 伪代码中,常见的有 +、−、×、/、DIV、MOD 和 ^。
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 加法 | 3 + 2 = 5 |
| − | Subtraction | 减法 | 3 − 2 = 1 |
| × | Multiplication | 乘法 | 3 × 2 = 6 |
| / | Real division | 实数除法 | 7 / 2 = 3.5 |
| DIV | Integer division | 整数除法 | 7 DIV 2 = 3 |
| MOD | Remainder | 余数 | 7 MOD 2 = 1 |
| ^ | Exponentiation | 幂 | 2 ^ 3 = 8 |
In Python, real division uses /, integer floor division uses //, and remainder uses %. Edexcel pseudocode tends to use DIV and MOD, but the underlying meaning is the same.
在 Python 中,实数除法使用 /,整数向下取整除法使用 //,余数使用 %。Edexcel 伪代码通常使用 DIV 和 MOD,但底层含义相同。
Be careful with DIV and MOD when negative numbers are involved, because different languages may round differently.
当涉及负数时,要特别注意 DIV 和 MOD,因为不同语言可能有不同的舍入规则。
3. Comparison Operators | 比较运算符
Comparison operators compare two values and return a Boolean result, either TRUE or FALSE. The most common symbols are =, ≠, <, >, ≤, and ≥.
比较运算符比较两个值,返回布尔结果,即 TRUE 或 FALSE。最常见的符号是 =、≠、<、>、≤ 和 ≥。
In text-based programming languages, == often means equal, != means not equal, <= means less than or equal, and >= means greater than or equal.
在文本编程语言中,== 通常表示等于,!= 表示不等于,<= 表示小于或等于,>= 表示大于或等于。
When comparing strings, the computer uses character encoding order such as ASCII or Unicode. For example, “apple” < “banana” is TRUE because the character code for ‘a’ is smaller than the character code for ‘b’.
比较字符串时,计算机会使用字符编码顺序,例如 ASCII 或 Unicode。例如,“apple” < “banana” 为 TRUE,因为字符 ‘a’ 的编码小于字符 ‘b’ 的编码。
Edexcel trace-table questions often include comparison conditions such as score ≥ 70 or age ≠ 18, so make sure you can convert between symbolic and written form quickly.
Edexcel 跟踪表题型常常包含 score ≥ 70 或 age ≠ 18 这样的比较条件,因此要确保能快速在符号形式和文字形式之间转换。
4. Logical Operators | 逻辑运算符
Logical operators combine Boolean values and produce a single Boolean result. The key operators are NOT, AND, OR, and sometimes XOR.
逻辑运算符组合布尔值并产生单个布尔结果。关键运算符是 NOT、AND、OR,有时还有 XOR。
The NOT operator reverses a value: NOT TRUE becomes FALSE. The AND operator returns TRUE only if both operands are TRUE, while OR returns TRUE if at least one operand is TRUE.
NOT 运算符反转值:NOT TRUE 变为 FALSE。AND 运算符仅在两个操作数都为 TRUE 时返回 TRUE,而 OR 在至少一个操作数为 TRUE 时返回 TRUE。
| A | B | A AND B | A OR B | A XOR B |
|---|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE | FALSE |
| TRUE | FALSE | FALSE | TRUE | TRUE |
| FALSE | TRUE | FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE | FALSE | FALSE |
An expression such as x > 0 AND x < 10 is TRUE only when both comparisons are true. This is often used to test whether a value lies inside a range.
像 x > 0 AND x < 10 这样的表达式,只有当两个比较都为真时才为 TRUE。这常用于测试一个值是否在某个区间内。
Logical operators usually follow the precedence NOT before AND before OR. Parentheses should be used to make complex conditions unambiguous.
逻辑运算符通常遵循 NOT 优先于 AND,AND 优先于 OR 的规则。应使用括号使复杂条件更加明确。
5. Bitwise Operators | 位运算符
Bitwise operators act on the binary representation of integer values. The common bitwise operators are AND, OR, XOR, NOT, left shift <<, and right shift >>.
位运算符作用于整数值的二进制表示。常见的位运算符有 AND、OR、XOR、NOT、左移 << 和右移 >>。
Although high-level pseudocode questions may not always focus on bitwise operations, the shift operators have a very useful arithmetic pattern: each left shift multiplies the integer by 2, and each right shift performs floor division by 2.
虽然高级伪代码题目不一定总是关注位运算,但移位运算符有一个非常有用的算术规律:每左移一位,整数乘以 2;每右移一位,整数向下除以 2。
For example, the decimal number 5 is 0101 in binary. The expression 5 << 1 gives 1010, which is decimal 10. The expression 5 >> 1 gives 0010, which is decimal 2.
例如,十进制数 5 在二进制中是 0101。5 << 1 得到 1010,即十进制 10。5 >> 1 得到 0010,即十进制 2。
Shifting can be a very fast way to double or halve integers in low-level programming, but in Edexcel pseudocode you are more likely to be asked to explain the effect rather than write a full bit-level solution.
在低级编程中,移位是一种非常快速地使整数翻倍或减半的方法,但在 Edexcel 伪代码中,更常见的是要求你解释移位的作用,而不是编写完整的位级解决方案。
6. String and List Operators | 字符串与列表运算符
Some operators behave differently depending on the data type. The + operator can be used to concatenate strings, while the * operator can repeat a string or list.
某些运算符根据数据类型的不同会有不同的行为。+ 运算符可用于连接字符串,* 运算符可以重复字符串或列表。
For example, “ha” * 3 produces “hahaha”. If names1 and names2 are lists, then names1 + names2 creates a new list containing all elements of both.
例如,“ha” * 3 生成 “hahaha”。如果 names1 和 names2 是列表,那么 names1 + names2 会创建一个包含两者所有元素的新列表。
The membership operator IN is used to test whether a value exists inside a collection. For instance, ‘a’ IN “cat” returns TRUE, while ‘z’ IN “cat” returns FALSE.
成员运算符 IN 用于测试某个值是否存在于集合中。例如,‘a’ IN “cat” 返回 TRUE,而 ‘z’ IN “cat” 返回 FALSE。
Indexing and slicing are not strictly operators in all languages, but they are essential when manipulating strings and lists in Edexcel programming exercises.
索引和切片在所有语言中并不严格属于运算符,但在 Edexcel 编程练习中,它们是操作字符串和列表所必需的技能。
7. Assignment and Shortcut Operators | 赋值与复合赋值运算符
The assignment operator ← or = is used to store a value in a variable. In Edexcel pseudocode, ← is often preferred to avoid confusion with the equality test.
赋值运算符 ← 或 = 用于将值存入变量。在 Edexcel 伪代码中,常常使用 ← 以避免与相等测试混淆。
Shortcut assignment operators combine arithmetic with assignment. For example, x ← x + 5 can be written as x += 5 in many languages. Similar shortcuts include -=, *=, /=, and //=.
复合赋值运算符将算术运算与赋值结合。例如,x ← x + 5 在许多语言中可以写成 x += 5。类似的复合运算符还有 -=、*=、/= 和 //=。
A very common exam misconception is confusing = with ==. One stores a value, the other tests whether two values are equal.
一个非常常见的考试误区是混淆 = 与 ==。前者用于存储值,后者用于测试两个值是否相等。
When tracing code, always update the variable value immediately after an assignment statement, even if the right-hand side contains the same variable.
在跟踪代码时,只要执行了赋值语句,就要立即更新变量的值,即使右侧包含同一个变量。
8. Operator Precedence | 运算符优先级
Operator precedence determines the order in which parts of an expression are evaluated. Higher-precedence operators are applied before lower-precedence ones.
运算符优先级决定表达式中各部分的求值顺序。优先级高的运算符先于优先级低的运算符执行。
A typical Edexcel precedence order from highest to lowest is: parentheses, exponentiation, unary minus and NOT,
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课程辅导,国外大学本科硕士研究生博士课程论文辅导