← 返回资讯屏轩教育 · 行业资讯

Operators, Expressions and Precedence in Programming | 编程中的运算符、表达式与优先级

2026-08-29 14:51 · 本文部分内容由AI辅助创作

📚 Operators, Expressions and Precedence in Programming | 编程中的运算符、表达式与优先级

In Edexcel A-Level Computer Science, understanding operators and how expressions are evaluated is essential for writing correct pseudocode and for tracing algorithms. Operators tell the processor what to do with values; precedence decides the order in which those operations happen. Get the order wrong and a program that looks correct will still produce the wrong answer. This article covers the operators you need to know, their precedence, and common exam-style pitfalls, with worked examples you can reproduce by hand.

在 Edexcel A-Level 计算机科学中,理解运算符以及表达式如何求值,对于编写正确的伪代码和追踪算法至关重要。运算符告诉处理器对数值做什么操作,而优先级决定这些操作的执行顺序。顺序一旦弄错,一个看起来正确的程序仍会输出错误结果。本文介绍你需要掌握的运算符、它们的优先级以及考试中常见的易错点,并配有可以手算复现的例题。


1. Operators as Building Blocks | 运算符是构建模块

Operators are special symbols or keywords that tell the computer to perform a specific operation on one or more values. The values they act on are called operands, and an operator plus its operands forms an expression. In A-Level programming, you must be able to use arithmetic, relational, Boolean, string, and assignment operators in pseudocode and in a chosen high-level language. A single expression may mix several of these categories, which is exactly why precedence matters.

运算符是告诉计算机对一个或多个值执行特定操作的特殊符号或关键字。被操作的值称为操作数,运算符加上它的操作数构成一个表达式。在 A-Level 编程中,你必须能够在伪代码和所选高级语言中使用算术、关系、布尔、字符串和赋值运算符。单个表达式可能混合多个类别,这正是优先级之所以重要的原因。

Understanding how these operators interact in an expression is a core skill for tracing and writing algorithms. Examiners frequently ask you to state the final value of a mixed expression, or to rewrite an expression using brackets so that it is easier to read.

理解这些运算符在表达式中如何相互作用,是追踪和编写算法的核心技能。考官经常要求你写出混合表达式的最终值,或者用括号重写表达式使其更易读。


2. Arithmetic Operators | 算术运算符

slash / performs real division and can produce a decimal answer, whereas DIV returns the whole-number quotient and MOD returns the remainder after whole-number division. Integer division and remainder are closely related: for any positive integers a and b, a = (a DIV b) × b + (a MOD b).

算术运算符用于数学计算。在 Edexcel 伪代码中,常用符号是 +、-、*、/、DIV 和 MOD。单个斜杠 / 执行实数除法,可能产生小数结果;而 DIV 返回整数商,MOD 返回整数除法后的余数。整除与取余密切相关:对任意正整数 a 和 b,有 a = (a DIV b) × b + (a MOD b)。

OperatorMeaningExampleResult
+Addition 加7 + 29
-Subtraction 减7 - 25
*Multiplication 乘7 * 214
/Real division 实数除法7 / 23.5
DIVInteger division 整除17 DIV 53
MODRemainder 取余17 MOD 52
- (unary)Negation 取负-8 + 3-5

ber of seconds into minutes and seconds, extracting individual digits of a number, and wrapping an index around the end of a list.

DIV 和 MOD 在考题中出现频率极高。典型用法包括:用 n MOD 2 = 0 判断奇偶;把总秒数转换为分和秒;提取一个数的各位数字;以及让索引在列表末尾回绕。

Total seconds = 367 → 367 DIV 60 = 6 minutes, 367 MOD 60 = 7 seconds

Note that MOD with negative operands is language dependent, so in pseudocode questions assume non-negative operands unless the question states otherwise. Also remember that division by zero is undefined and must be guarded with an IF statement in any robust algorithm.

注意,负数取余的结果因语言而异,所以在伪代码题中除非题目另有说明,应假定操作数非负。另外要记住,除以零是未定义的,任何健壮的算法都必须用 IF 语句加以防范。


3. Relational Operators | 关系运算符

Relational operators compare two values and return a Boolean result, either TRUE or FALSE. They are the engine of every selection statement, every loop condition, and every validation check. The most common mistake is confusing the assignment symbol with the equality operator: assignment stores a value, comparison asks a question.

关系运算符比较两个值并返回布尔结果,即 TRUE 或 FALSE。它们是所有选择语句、循环条件和验证检查的引擎。最常见的错误是把赋值符号与相等运算符混淆:赋值是存储一个值,比较则是提出一个问题。

OperatorMeaningExampleResult
=Equal to 等于5 = 5TRUE
<>Not equal to 不等于5 <> 5FALSE
<Less than 小于5 < 5FALSE
>Greater than 大于5 > 5FALSE
&lt;=Less than or equal 小于等于5 &lt;= 5TRUE
&gt;=Greater than or equal 大于等于5 &gt;= 6FALSE

gt;= 0 AND mark <= 100 rather than 0 <= mark <= 100, because most languages evaluate the second form from left to right and produce a type error.

边界检查是经典应用。一个有效的分数可能满足 mark &gt;= 0 AND mark &lt;= 100。在伪代码中通常写成 mark &gt;= 0 AND mark &lt;= 100,而不是 0 &lt;= mark &lt;= 100,因为大多数语言会从左到右求值第二种写法,从而产生类型错误。


4. Boolean Operators | 布尔运算符

Boolean operators combine truth values. AND is TRUE only when both operands are TRUE; OR is TRUE when at least one operand is TRUE; NOT reverses a single truth value. These three operators allow you to build compound conditions such as range checks, login validation, and game state rules.

布尔运算符组合真值。只有当两个操作数都为 TRUE 时,AND 才为 TRUE;只要有一个操作数为 TRUE,OR 就为 TRUE;NOT 则反转单个真值。这三种运算符让你能够构建复合条件,例如范围检查、登录验证和游戏状态规则。

ABA AND BA OR BNOT A
TRUETRUETRUETRUEFALSE
TRUEFALSEFALSETRUEFALSE
FALSETRUEFALSETRUETRUE
FALSEFALSEFALSEFALSETRUE

De Morgan's laws are worth memorising because they let you simplify conditions and spot equivalent expressions: NOT (A AND B) is equivalent to (NOT A) OR (NOT B), and NOT (A OR B) is equivalent to (NOT A) AND (NOT B). A question that asks you to rewrite a condition usually expects exactly this transformation.

德摩根定律值得记住,因为它能让你化简条件并识别等价表达式:NOT (A AND B) 等价于 (NOT A) OR (NOT B),NOT (A OR B) 等价于 (NOT A) AND (NOT B)。当题目要求你重写条件时,通常期待的正是这种变换。

NOT (A AND B) ⇌ (NOT A) OR (NOT B)


5. String Operators and Functions | 字符串运算符与函数

Strings are sequences of characters, and although they look simple they behave differently from numbers. The main string operator is concatenation, which joins two strings end to end. In Edexcel pseudocode concatenation is usually written with + or with the ampersand symbol, so "Hello" + "World" becomes "HelloWorld" with no automatic space added.

广告 / Sponsored

字符串是字符序列,虽然看起来简单,但它的行为与数字不同。主要的字符串运算符是连接,它把两个字符串首尾相接。在 Edexcel 伪代码中,连接通常写作 + 或使用 & 符号,所以 "Hello" + "World" 得到 "HelloWorld",系统不会自动加空格。

The relational operators also work on strings. In most languages they compare character by character using a character code order, which means "Zebra" comes before "apple" because uppercase letters have lower codes than lowercase letters. If a question needs case-insensitive matching, convert both sides to the same case first.

关系运算符同样适用于字符串。在大多数语言中,它们按字符编码逐字符比较,因此 "Zebra" 会排在 "apple" 前面,因为大写字母的编码小于小写字母。如果题目需要不区分大小写的匹配,应先把两边都转换成同一种大小写。

"Zebra" < "apple" → TRUE (uppercase code 90 < lowercase code 97)


6. Assignment Operators | 赋值运算符

An assignment operator stores a value in a variable. In Edexcel pseudocode it is drawn as a left arrow, so the statement x ← 5 means "put the value 5 into x". Many languages use a single equals sign instead. Whichever notation you use, remember that assignment is a statement, not an expression: it does not return a value that you can compare.

赋值运算符把值存入变量。在 Edexcel 伪代码中它画成左箭头,因此语句 x ← 5 表示"把值 5 放进 x"。许多语言则使用单个等号。无论采用哪种表示法,都要记住赋值是语句而非表达式:它不返回可供比较的值。

The right-hand side of an assignment is always evaluated first, and only then is the result stored on the left. This rule explains the counter idiom: if x currently holds 4, then x ← x + 1 evaluates x + 1 to 5 and only afterwards overwrites x with 5. Judges in tracing questions love this line because it appears in nearly every loop.

赋值语句总是先计算右侧,然后把结果存入左侧。这条规则解释了计数器的惯用法:如果 x 当前是 4,那么 x ← x + 1 先把 x + 1 算成 5,之后才用 5 覆盖 x。追踪题中特别爱考这一行,因为它几乎出现在每个循环里。


7. Operator Precedence | 运算符优先级

Precedence is the set of rules that decides which operator is applied first when an expression contains several of them. Without precedence, 3 + 4 * 2 would be ambiguous; with precedence it is unambiguously 11 rather than 14, because multiplication binds more tightly than addition. Precedence is applied to the whole expression before the expression is evaluated left to right.

优先级是一套规则,用来决定表达式中含有多个运算符时先执行哪一个。没有优先级,3 + 4 * 2 就会有歧义;有了优先级,它明确等于 11 而不是 14,因为乘法的结合力强于加法。优先级作用于整个表达式,先确定结合顺序,再从左到右求值。

LevelOperator(s)Associativity
1 (highest)( ) brackets 括号inner to outer 由内向外
2- unary minus, NOT 取负、NOTright to left 从右向左
3* , / , DIV , MODleft to right 从左向右
4+ , - binary 加、减left to right 从左向右
5= , <> , < , > , &lt;= , &gt;=left to right 从左向右
6ANDleft to right 从左向右
7 (lowest)ORleft to right 从左向右

Three consequences are worth stating explicitly. First, arithmetic is always completed before comparisons, which is why n + 1 > 10 compares the sum rather than the original n. Second, comparisons are completed before AND and OR, which is why a < b AND b < c means (a < b) AND (b < c). Third, AND binds more tightly than OR, so TRUE OR FALSE AND FALSE evaluates to TRUE OR (FALSE AND FALSE), giving TRUE.

有三条推论值得明确说明。第一,算术总是先于比较完成,所以 n + 1 > 10 比较的是和而不是原来的 n。第二,比较先于 AND 和 OR 完成,所以 a < b AND b < c 的意思是 (a < b) AND (b < c)。第三,AND 的结合力强于 OR,因此 TRUE OR FALSE AND FALSE 先算 TRUE OR (FALSE AND FALSE),结果为 TRUE。


8. Evaluating Expressions Step by Step | 逐步求值表达式

The safest way to answer an expression question is to evaluate one operator at a time and write the result underneath, exactly as you would in a trace table. Apply the highest-precedence operator still remaining, replace it with its value, and repeat. This method is slow but nearly error-proof, and it earns method marks even when the final answer is wrong.

回答表达式题最稳妥的方法是每次只计算一个运算符,并把结果写在下面,就像做追踪表一样。找出当前优先级最高且尚未计算的运算符,用它的值替换,然后重复。这个方法慢,但几乎不会出错,而且即使最终答案错了也能拿到过程分。

17 MOD 5 + 2 * 3 = 2 + 2 * 3 = 2 + 6 = 8

ge everything. Compare 2 + 3 * 4 = 14 with (2 + 3) * 4 = 20, and compare 10 - 4 - 3 = 3 (left to right) with 10 - (4 - 3) = 9. Subtraction and division are not associative, so left-to-right order genuinely matters.

在上例中,MOD 与 * 处于同一优先级,因此按从左到右顺序计算:先算 17 MOD 5,再算 2 * 3。括号可以改变一切。比较 2 + 3 * 4 = 14 与 (2 + 3) * 4 = 20,再比较 10 - 4 - 3 = 3(从左到右)与 10 - (4 - 3) = 9。减法和除法不满足结合律,所以从左到右的顺序确实重要。


9. Parentheses, Readability and Rewriting | 括号、可读性与改写

re not strictly required. A condition such as age &gt;= 18 AND age &lt;= 65 AND postcode = "SW1" is far easier to check when written as (age &gt;= 18 AND age &lt;= 65) AND postcode = "SW1". Brackets cost nothing at runtime and they document your intention.

由于优先级是隐形的,专业程序员即使并非必须也会加括号。像 age &gt;= 18 AND age &lt;= 65 AND postcode = "SW1" 这样的条件,写成 (age &gt;= 18 AND age &lt;= 65) AND postcode = "SW1" 会容易检查得多。括号在运行时没有成本,却能记录你的意图。

Exam questions sometimes ask you to remove unnecessary brackets while preserving meaning, or to add brackets to show evaluation order. Both directions test the same knowledge. A useful check is to evaluate the original and the rewritten version with two or three sets of sample values; if any pair of results differs, your rewrite is wrong.

考题有时要求你在保持含义不变的前提下删除多余括号,或者添加括号以显示求值顺序。两个方向考查的是同一份知识。一个有用的检查方法是用两三组样本值分别计算原式和改写式;只要有一组结果不同,你的改写就是错的。


10. Type Mismatch and Common Errors | 类型不匹配与常见错误

Every operator expects operands of particular data types, and mixing them causes errors. Adding a string to an integer, comparing a Boolean with a number, or storing a real value in an integer variable will all fail or silently truncate. Strongly typed languages catch these problems at compile time; weakly typed languages let them through until runtime.

广告 / Sponsored
25年新版 Cambridge IGCSE O Level Economics Coursebook with Dig…
经济 备考教辅 · 正版现货 · 学习备考通用
查看详情 / View on Taobao参考价 ¥438.0
广告 / Sponsored
剑桥国际Alevel 商务经管类教材(2023考纲版) Cambridge International AS & A …
商科 备考教辅 · 正版现货 · 学习备考通用
查看详情 / View on Taobao参考价 ¥178.0

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

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao