📚 Operators and Combined Expressions in A-Level Programming | A-Level编程中的运算符与组合表达式
In A-Level programming (Edexcel), mastering operators and understanding how they combine within expressions is essential for writing correct and efficient code. Operators are symbols that perform specific operations on one or more operands. When multiple operators appear together in a single expression, the rules of precedence and associativity determine the order of evaluation. This article explores arithmetic, relational, and Boolean operators, shows how they can be combined, and explains the evaluation logic that underpins programs written in languages like Python, Java, or pseudocode.
在A-Level编程(Edexcel)中,掌握运算符并理解它们在表达式中的组合方式,是写出正确高效代码的基础。运算符是对一个或多个操作数执行特定操作的符号。当多个运算符同时出现在一个表达式中时,优先级和结合性规则决定了求值的顺序。本文探讨算术运算符、关系运算符和布尔运算符,展示它们如何组合,并解释支撑Python、Java或伪代码等语言程序的求值逻辑。
1. Introduction to Operators | 运算符简介
Operators are the building blocks of expressions. In programming, we can classify them into arithmetic, relational, and logical categories. Each operator works on data values (operands) and produces a result. For example, the addition operator + adds two numbers, while the comparison operator > checks if one value is greater than another. Understanding the categories helps when constructing combined expressions that mix arithmetic with logical testing.
运算符是表达式的基本构件。在编程中,我们可以将其分为算术运算符、关系运算符和逻辑运算符几类。每个运算符作用于数据值(操作数)并产生结果。例如,加法运算符 + 将两个数相加,而比较运算符 > 则检查一个值是否大于另一个。理解这些类别有助于构造混合了算术和逻辑测试的组合表达式。
2. Arithmetic Operators | 算术运算符
The core arithmetic operators are + (addition), – (subtraction), * (multiplication), / (division), MOD (modulus), and DIV (integer division). These are used to perform mathematical calculations. In many languages, / gives a floating‑point result, while DIV or // (floor division) returns only the whole‑number part. Arithmetic operators form the basis of formulaic expressions that variables store.
核心算术运算符包括 +(加)、-(减)、*(乘)、/(除)、MOD(取模)和 DIV(整除)。它们用于执行数学计算。在许多语言中,/ 给出浮点结果,而 DIV 或 //(向下取整除法)只返回整数部分。算术运算符构成了变量存储的公式化表达式的基础。
3. Integer Division and Modulus | 整除与取模
Integer division discards any remainder, while modulus returns the remainder of a division. For instance, 17 DIV 5 yields 3, and 17 MOD 5 yields 2. These operations are particularly useful in algorithms that need to split quantities, check divisibility, or wrap around array indices. Combined with other arithmetic, they can solve problems like extracting digits from a number.
整除会丢弃余数,而取模则返回除法运算的余数。例如,17 DIV 5 得 3,17 MOD 5 得 2。这些操作在需要分割数量、检查整除性或循环使用数组索引的算法中特别有用。与其他算术运算结合,它们可以解决诸如从一个数字中提取数位的问题。
4. Relational (Comparison) Operators | 关系运算符
Relational operators compare two values and return a Boolean result (TRUE or FALSE). The standard set includes = (equal to), <> or != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to). They are frequently used in selection and iteration statements as conditions that control program flow.
关系运算符比较两个值并返回布尔结果(TRUE 或 FALSE)。标准集合包括 =(等于)、<> 或 !=(不等于)、<(小于)、>(大于)、<=(小于或等于)和 >=(大于或等于)。它们经常作为控制程序流程的条件出现在选择和迭代语句中。
5. Boolean Logical Operators | 布尔逻辑运算符
Boolean operators act on Boolean values and are primary tools for building complex conditions. The fundamental ones are AND, OR, and NOT. In many languages, AND is true only if both operands are true; OR is true if at least one operand is true; NOT simply negates the truth value. Combining relational and logical operators allows the expression of intricate decision logic.
布尔运算符作用于布尔值,是构建复杂条件的主要工具。基本的运算符有 AND、OR 和 NOT。在许多语言中,AND 只在两个操作数都为真时才为真;OR 只要至少一个操作数为真就为真;NOT 则直接取反真值。将关系运算符与逻辑运算符结合,可以表达复杂的决策逻辑。
6. Operator Precedence | 运算符优先级
When an expression contains different types of operator, precedence decides which operation is performed first. Arithmetic operators generally have higher precedence than relational ones, which in turn rank higher than logical operators. Within arithmetic, * / MOD DIV evaluate before + -. A typical precedence order from highest to lowest is: parentheses; arithmetic (unary followed by multiplicative, then additive); relational; NOT; AND; OR.
当一个表达式包含不同类型的运算符时,优先级决定哪个运算先执行。算术运算符通常比关系运算符优先级高,而关系运算符又优先于逻辑运算符。在算术运算中,* / MOD DIV 先于 + – 求值。典型的优先级从高到低的顺序是:括号;算术(一元,然后是乘除类,再是加减类);关系;NOT;AND;OR。
| Operator | 中文 | Precedence (high → low) |
|---|---|---|
| ( ) | 括号 | 1 (highest) |
| NOT, unary + – | 逻辑非/一元正负 | 2 |
| * / MOD DIV | 乘 除 取模 整除 | 3 |
| + – | 加 减 | 4 |
| < > <= >= = <> | 关系比较 | 5 |
| AND | 逻辑与 | 6 |
| OR | 逻辑或 | 7 (lowest) |
Note: exact ordering can vary slightly between languages, so always consult your specification’s pseudocode rules. / 注意:不同语言的确切顺序可能略有不同,请务必参考考纲中的伪代码规则。
7. Evaluating Combined Expressions | 组合表达式的求值
A combined expression mixes arithmetic, relational, and logical operators. For example: x + y > 10 AND z < 5. The arithmetic (x + y) is evaluated first, then the relational comparisons (> 10, < 5), and finally the logical AND. Step‑by‑step evaluation ensures that the intention of the condition matches the machine’s interpretation. Misreading precedence can lead to bugs that are hard to spot.
组合表达式混合了算术、关系与逻辑运算符。例如:x + y > 10 AND z < 5。先计算算术部分 (x + y),再进行关系比较 (> 10, < 5),最后执行逻辑 AND。逐步求值可确保条件的本意与机器的解释相一致。误读优先级可能导致难以发现的错误。
8. Associativity of Operators | 运算符的结合性
When two operators of the same precedence appear together, associativity decides the direction of evaluation. Most arithmetic and relational operators are left‑associative, meaning they group from left to right. For instance, a - b - c is evaluated as (a - b) - c. Unary operators and assignment are typically right‑associative. Understanding associativity removes ambiguity in expressions like a / b * c.
当两个优先级相同的运算符相邻时,结合性决定求值的方向。大多数算术和关系运算符都是左结合的,即从左向右分组。例如,a - b - c 的求值顺序是 (a - b) - c。一元运算符和赋值通常为右结合。理解了结合性,即可消除 a / b * c 这类表达式的歧义。
9. Using Parentheses to Control Order | 使用括号控制运算顺序
Parentheses override default precedence and associativity. Any sub‑expression enclosed in ( ) is evaluated first. Even when not strictly needed, adding parentheses can improve readability and prevent logical errors. For example, writing (age >= 18) AND (membership = TRUE) makes the condition clearer than relying solely on precedence. Good programmers use parentheses to make combined expressions self‑documenting.
括号可以覆盖默认的优先级和结合性。任何用 ( ) 括起来的子表达式都会优先求值。即使在不严格需要的时候,添加括号也能提高可读性,防止逻辑错误。例如,写成 (age >= 18) AND (membership = TRUE) 比单纯依赖优先级更能清晰地表达条件。优秀的程序员会利用括号让组合表达式自带说明性。
10. Common Pitfalls with Combined Operators | 组合运算符的常见陷阱
- Confusing = for ==: In many languages, = is assignment while == is equality test. Using = inside a condition often leads to a logical error or an unintended assignment. / 混淆 = 与 ==:在许多语言中,= 是赋值,== 才是相等测试。在条件中使用 = 常常导致逻辑错误或意外的赋值。
- Mixing AND/OR without parentheses: Without parentheses, AND binds tighter than OR, so
a OR b AND cmeansa OR (b AND c). This may not be what the programmer intended. / 不使用括号混用 AND/OR:在没有括号的情况下,AND 比 OR 结合得更紧密,因此a OR b AND c实际上相当于a OR (b AND c),这可能并非程序员的本意。 - Assuming left‑to‑right for all operators: Not all operators are left‑associative. Exponent, unary, and assignment operators often associate right‑to‑left. / 假设所有运算符都从左到右:并非所有运算符都是左结合。指数、一元和赋值运算符常常从右到左结合。
- Integer division truncation: In some languages, dividing two integers with / performs integer division, discarding the remainder. This can silently affect combined expressions. / 整除截断:在某些语言中,使用 / 对两个整数相除会执行整除并丢弃余数,这可能会悄然影响组合表达式的结果。
11. Practice Examples | 练习示例
Consider the following pseudocode and evaluate step by step. / 考虑下面的伪代码,逐步求值。
result ← (5 + 3 * 2) > 10 AND NOT (4 <= 2)
Step 1: inside first parentheses, multiplication has precedence: 3 * 2 = 6; then 5 + 6 = 11. / 第一步:第一对括号内乘法优先:3 * 2 = 6;然后 5 + 6 = 11。
Step 2: relational comparison: 11 > 10 is TRUE. / 第二步:关系比较:11 > 10 为 TRUE。
Step 3: second parentheses: 4 <= 2 is FALSE. / 第三步:第二对括号:4 <= 2 为 FALSE。
Step 4: NOT FALSE gives TRUE. / 第四步:NOT FALSE 得 TRUE。
Step 5: TRUE AND TRUE yields TRUE. / 第五步:TRUE AND TRUE 结果为 TRUE。
Thus the variable result holds TRUE. / 因此变量 result 的值为 TRUE。
Another example: total ← price * quantity + delivery. If price=10, quantity=3, delivery=5, the multiplication 10 * 3 = 30 happens first, then 30 + 5 = 35. Adding parentheses like price * (quantity + delivery) would change the outcome to 10 * 8 = 80, demonstrating how order matters. / 另一个例子:total ← price * quantity + delivery。若 price=10, quantity=3, delivery=5,首先计算乘法 10 * 3 = 30,然后 30 + 5 = 35。如加上括号变成 price * (quantity + delivery),结果将变为 10 * 8 = 80,这体现了顺序的重要性。
12. Conclusion | 结论
Operators form the nervous system of programming logic. A solid grasp of arithmetic, relational, and Boolean operators, together with the rules of precedence, associativity, and the careful use of parentheses, empowers you to write clear, predictable code. In A-Level exams, you will be expected to trace through combined expressions and construct conditions without ambiguity. Regular practice with mixed-operator expressions will build the confidence to handle any programming challenge.
运算符构成了编程逻辑的神经系统。牢牢掌握算术、关系和布尔运算符,并熟悉优先级、结合性规则以及谨慎使用括号,能让你写出清晰、可预测的代码。在A-Level考试中,你需要能够追踪组合表达式的求值过程,并构建无歧义的条件。通过经常练习混合运算符的表达式,你将培养出应对任何编程挑战的信心。
Published by TutorHao | Programming Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)