Combined Operations in Programming: Arithmetic and Logical Expressions | 编程中的组合运算:算术与逻辑表达式

📚 Combined Operations in Programming: Arithmetic and Logical Expressions | 编程中的组合运算:算术与逻辑表达式

In A-Level programming, expressions often mix arithmetic, comparison, and logical operators to control program flow and perform calculations. Understanding how these combined operations are evaluated—according to precedence, associativity, and data types—is fundamental to writing correct and efficient code. This article breaks down the rules and demonstrates typical patterns you will encounter in the Edexcel specification, using Python-like pseudocode that mirrors exam-style questions.

在A-Level编程中,表达式经常混合使用算术、比较和逻辑运算符来控制程序流程和执行计算。理解这些组合运算如何根据优先级、结合性和数据类型进行求值,是写出正确且高效代码的基础。本文分解了这些规则,并展示了Edexcel考纲中会遇到的典型模式,使用类似Python的伪代码模拟考试风格的问题。

1. Operator Precedence: The Universal Order | 运算符优先级:通用求值顺序

When an expression contains several operators, the language follows a fixed precedence table. Arithmetic operators generally take priority over comparisons, and comparisons over logical operators. For example, in 3 + 5 > 2 × 4 AND 10 / 2 == 5, multiplication and division are evaluated first, then addition, then comparisons, and finally the logical AND. Using parentheses can override default precedence and make code easier to read.

当一个表达式包含多个运算符时,语言会遵循固定的优先级表。算术运算符通常优先于比较运算符,比较运算符优先于逻辑运算符。例如,在 3 + 5 > 2 × 4 AND 10 / 2 == 5 中,乘法和除法先被计算,然后是加法,接着是比较运算,最后是逻辑与。使用圆括号可以覆盖默认优先级,让代码更易读。

Category Operators Precedence (high to low)
Arithmetic ** (exponent), *, /, //, %, +, – Highest
Comparison ==, !=, <, >, <=, >= Middle
Logical NOT, AND, OR Lowest (NOT highest among them)

2. Arithmetic Operations: The Building Blocks | 算术运算:基本构建块

Standard arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), integer division (//), modulus (%), and exponentiation (**). In many exam pseudo-languages, division of integers returns a real number unless integer division is explicitly used. Modulus gives the remainder, useful for checking parity or cycling through values. Always consider whether operands are integers or floating-point numbers, as this affects precision and rounding.

标准算术运算符包括加 (+)、减 (-)、乘 (*)、除 (/)、整除 (//)、取模 (%) 和幂运算 (**)。在许多考试伪语言中,整数除法返回实数,除非显式使用整除。模运算给出余数,可用于检查奇偶性或循环遍历数值。始终要考虑操作数是整数还是浮点数,因为这会影响精度和舍入。

Examples: 17 % 5 = 2, 3² = 3**2 = 9, 7 // 2 = 3


3. Comparison Operators: Making Decisions | 比较运算符:做出判断

Comparison operators yield Boolean values TRUE or FALSE. They include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators have lower precedence than arithmetic operators, so a + b > c is interpreted as (a + b) > c without ambiguity. In combined expressions, each comparison must be valid on its own; chaining like a < b < c may be allowed in some languages but is best broken into two comparisons with AND for exam clarity.

比较运算符产生布尔值 TRUE 或 FALSE。它们包括等于 (==)、不等于 (!=)、大于 (>)、小于 (<)、大于等于 (>=) 和小于等于 (<=)。这些运算符的优先级低于算术运算符,因此 a + b > c 会被理解为 (a + b) > c,没有歧义。在组合表达式中,每个比较必须本身有效;像 a < b < c 这样的链式比较在某些语言中允许,但为了考试清晰最好用 AND 拆成两个比较。


4. Logical Operators: Combining Conditions | 逻辑运算符:组合条件

Logical operators—AND, OR, and NOT—are used to join or invert Boolean expressions. NOT has the highest precedence among logicals, followed by AND, then OR. For example, NOT age < 18 AND hasLicense is evaluated as (NOT (age < 18)) AND hasLicense. When mixing AND and OR, always parenthesise to avoid logic errors. Many marks are lost in exams due to misinterpreting a > 5 AND b < 10 OR c == 0.

逻辑运算符——AND、OR 和 NOT——用于连接或取反布尔表达式。NOT 在逻辑运算符中优先级最高,其次是 AND,最后是 OR。例如,NOT age < 18 AND hasLicense 会被求值为 (NOT (age < 18)) AND hasLicense。混合使用 AND 和 OR 时,务必加括号以避免逻辑错误。考试中很多分数都丢在误解 a > 5 AND b < 10 OR c == 0 这类表达式上。

A B A AND B A OR B NOT A
TRUE TRUE TRUE TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
FALSE TRUE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE TRUE

5. Short-Circuit Evaluation: Lazy Logic | 短路求值:惰性逻辑

Many programming languages, including those in Edexcel’s pseudo-code, evaluate logical expressions using short-circuiting. For expression1 AND expression2, if expression1 is FALSE, expression2 is not evaluated at all because the overall result must be FALSE. Similarly, for expression1 OR expression2, if expression1 is TRUE, the second part is skipped. This can be exploited to guard against errors, such as checking if a denominator is non-zero before division: IF b != 0 AND a / b > 2 THEN …

许多编程语言,包括Edexcel伪代码中的,都采用短路求值来评估逻辑表达式。对于 expression1 AND expression2,如果 expression1 为 FALSE,则根本不会计算 expression2,因为整体结果必定为 FALSE。类似地,对于 expression1 OR expression2,如果 expression1 为 TRUE,第二部分将被跳过。这可以用来避免错误,例如在除法前检查分母非零:IF b != 0 AND a / b > 2 THEN …


6. Mixing Arithmetic with Comparisons: Step-by-Step | 算术与比较混合:逐步拆解

Consider the expression 2 + 3 * 4 > 10. First, multiplication: 3 * 4 = 12. Then, addition: 2 + 12 = 14. Finally, comparison: 14 > 10 evaluates to TRUE. When brackets are introduced, the logic shifts: (2 + 3) * 4 > 10 becomes 5 * 4 > 10 → 20 > 10 → TRUE. Mastering these small drills prevents errors in more complex conditionals like WHILE index < 100 AND total + value * 2 <= limit.

考虑表达式 2 + 3 * 4 > 10。首先,乘法:3 * 4 = 12。然后,加法:2 + 12 = 14。最后,比较:14 > 10 得出 TRUE。当引入括号时,逻辑改变:(2 + 3) * 4 > 10 变成 5 * 4 > 10 → 20 > 10 → TRUE。掌握这类小练习可以避免在更复杂的条件中出错,例如 WHILE index < 100 AND total + value * 2 <= limit


7. Integer Division and Modulus in Combined Expressions | 组合表达式中的整除与取模

Integer division (//) and modulus (%) often appear together in problems involving digit extraction or time calculations. For instance, to check if a number n contains at least one even digit, you could write: n % 2 == 0 OR (n // 10) % 2 == 0 OR (n // 100) % 2 == 0. Remember that integer division truncates toward zero or negative infinity depending on the language; in exam pseudocode, it typically truncates toward zero for positive numbers.

整除 (//) 和取模 (%) 经常一起出现在涉及数位提取或时间计算的问题中。例如,检查数字 n 是否包含至少一个偶数数字,可以这样写:n % 2 == 0 OR (n // 10) % 2 == 0 OR (n // 100) % 2 == 0。记住整除根据语言会向零截断或向负无穷截断;在考试伪代码中,对于正数通常向零截断。

23 // 5 = 4, -7 // 2 = -3 (if truncating toward negative infinity) or -3 (commonly), check specification.


8. Data Type Conversions in Mixed Operations | 混合运算中的数据类型转换

When an expression mixes integers and reals, most languages perform implicit type conversion (coercion) to the more precise type—usually real. So 5 / 2 yields 2.5, not 2. However, if both operands are integers and integer division is used, the result is an integer. In logical contexts, some languages treat numbers as truthy: zero means FALSE, non-zero means TRUE. Combined operations like (a + b) AND c are thus allowed in weakly typed languages but are discouraged in Edexcel pseudocode; stick to explicit comparisons.

当表达式混合整数和实数时,大多数语言会隐式转换类型(强制)到更精确的类型——通常是实数。因此 5 / 2 得到 2.5,不是 2。然而,如果两个操作数都是整数且使用整除,结果为整数。在逻辑上下文中,某些语言将数字视为真值:零表示 FALSE,非零表示 TRUE。因此像 (a + b) AND c 这样的组合运算在弱类型语言中允许,但在Edexcel伪代码中不鼓励;应坚持使用显式比较。


9. Parentheses to Avoid Ambiguity | 用括号避免歧义

Parentheses are the programmer’s best tool for making combined operations explicit. The expression NOT (x > 5 AND y < 3) is very different from NOT x > 5 AND y < 3 because NOT binds tightly. In exam trace table questions, you must evaluate exactly what is written, so adding parentheses in your own code ensures intention matches execution. A good habit is to parenthesize every compound condition in IF and WHILE statements.

括号是程序员让组合运算明确化的最佳工具。表达式 NOT (x > 5 AND y < 3)NOT x > 5 AND y < 3 大不相同,因为 NOT 结合紧密。在考试追溯表题目中,你必须完全按照所写内容求值,因此在代码中加括号能确保意图与执行匹配。一个好习惯是在 IF 和 WHILE 语句中将每个复合条件都加上括号。


10. Common Exam Pitfalls and Trace Table Practice | 常见考试陷阱与追溯表练习

Exam questions frequently ask you to complete a trace table for an algorithm containing combined operations. Always work step by step, rewriting sub-expressions. Watch out for off-by-one errors with integer division, modulo, and short-circuit evaluation. For example, in a loop condition count < 10 AND numbers[count] % 2 == 0, if count goes out of bounds, the logical AND short-circuits, but careful array indexing must still be safe. Practice with past papers to recognise patterns.

考试题目经常要求你为含有组合运算的算法完成追溯表。务必逐步计算,重写子表达式。注意整除、取模和短路求值可能导致的一差错误。例如,在循环条件 count < 10 AND numbers[count] % 2 == 0 中,如果 count 超出边界,逻辑与会短路求值,但数组索引仍需安全。通过做往年试卷识别模式。


11. Boolean Algebra in Conditional Simplification | 条件简化中的布尔代数

Understanding Boolean identities helps simplify combined operations. De Morgan’s Laws state: NOT (A AND B) is equivalent to NOT A OR NOT B, and NOT (A OR B) is NOT A AND NOT B. This is useful when negating complex conditions. For instance, the opposite of score >= 50 AND age <= 18 is NOT (score >= 50 AND age <= 18), which is score < 50 OR age > 18. Recognizing these transforms can make code more readable and efficient.

理解布尔恒等式有助于简化组合运算。德摩根定律指出:NOT (A AND B) 等价于 NOT A OR NOT B,而 NOT (A OR B) 等价于 NOT A AND NOT B。这在否定复杂条件时很有用。例如,score >= 50 AND age <= 18 的反面是 NOT (score >= 50 AND age <= 18),等同于 score < 50 OR age > 18。识别这些变换能让代码更清晰高效。


12. Applying Combined Operations in Loop and Selection Structures | 在循环与选择结构中应用组合运算

Real-world Edexcel problems often embed combined operations inside WHILE loops and IF-THEN-ELSE structures. A typical validation check might be: WHILE age < 0 OR age > 120 DO OUTPUT “Invalid”. A nested selection could use: IF (mark >= 70 AND attendance > 80) OR (mark >= 60 AND extraCredit) THEN grade = ‘A’. Breaking these down into their components ensures correct evaluation and prevents logical flaws in your pseudocode.

实际Edexcel问题经常在 WHILE 循环和 IF-THEN-ELSE 结构中嵌入组合运算。典型的验证检查可能是:WHILE age < 0 OR age > 120 DO OUTPUT “Invalid”。嵌套选择可能使用:IF (mark >= 70 AND attendance > 80) OR (mark >= 60 AND extraCredit) THEN grade = ‘A’。将这些分解成各个组成部分可以确保求值正确,防止伪代码中出现逻辑缺陷。

Published by TutorHao | Programming Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version