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

Operators and Expressions in Programming | 编程中的运算符与表达式

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

📚 Operators and Expressions in Programming | 编程中的运算符与表达式

Operators and expressions are fundamental to writing correct programs in any language. Every value a program computes, every condition it tests and every variable it updates is built from operators applied to operands. In Edexcel A-Level Computer Science and in the programming project units of other boards, candidates must be able to use arithmetic, relational, Boolean and assignment operators confidently, and must understand how precedence rules determine the order of evaluation. This article explains each operator category with clear examples, common pitfalls, and exam-style tips.

运算符和表达式是编写正确程序的基础。程序计算出的每一个值、测试的每一个条件、更新的每一个变量,都由运算符作用于操作数构成。在 Edexcel A-Level 计算机科学以及其他考试局编程单元中,考生必须能够自信地使用算术运算符、关系运算符、布尔运算符和赋值运算符,并理解优先级规则如何决定求值顺序。本文通过清晰的示例、常见误区和考试技巧逐一讲解各类运算符。


1. Operators, Operands and Expressions | 运算符、操作数与表达式

An operator is a symbol or keyword that performs an operation on one or more values. The values it acts upon are called operands. An expression is any combination of operators, operands, variables and function calls that the language can evaluate to produce a single value. For example, in total = price * quantity + 5, the * and + symbols are operators, price, quantity and 5 are operands, and price * quantity + 5 is an expression that evaluates to one number.

运算符是对一个或多个值执行运算的符号或关键字,被运算的值称为操作数。表达式是运算符、操作数、变量和函数调用的任意组合,语言可以对其求值并得到一个单一的值。例如在 total = price * quantity + 5 中,* 与 + 是运算符,price、quantity 与 5 是操作数,而 price * quantity + 5 是一个求值后得到单个数值的表达式。

Syllabus specifications usually group operators into four families: arithmetic, relational (comparison), Boolean (logical) and assignment. Recognising which family an operator belongs to tells you what kind of value the expression will produce: a number, a truth value, or a stored result.

考纲通常把运算符分为四类:算术、关系(比较)、布尔(逻辑)和赋值。判断一个运算符属于哪一类,就能知道表达式会产生什么类型的值:数值、真值,还是被存回变量的结果。


2. Arithmetic Operators | 算术运算符

matical calculations on numeric values. The standard operators are addition (+), subtraction (-), multiplication (*), division (/), integer division (// or DIV), modulo (% ing on the language). Each returns a numeric result, but the exact value depends on whether the operands are integers or real numbers.

算术运算符对数值执行数学计算。标准运算符包括加(+)、减(-)、乘(*)、除(/)、整除(// 或 DIV)、取模(%)和幂运算(** 或 ^,取决于语言)。它们都返回数值结果,但具体值取决于操作数是整数还是实数。

In Python, the expression 7 + 3 * 2 evaluates to 13 because multiplication has higher precedence than addition. The expression (7 + 3) * 2 evaluates to 20, showing how parentheses change the order of calculation.

在 Python 中,表达式 7 + 3 * 2 的值为 13,因为乘法的优先级高于加法。表达式 (7 + 3) * 2 的值为 20,说明括号如何改变运算顺序。

a + b × c ≠ (a + b) × c

A very common exam question gives a short list of statements and asks for the final value of a variable after several arithmetic updates. Write the intermediate value of the variable after every line, not just the final answer, because method marks are awarded for correct tracing.

常见的考题会给出一小段语句,要求写出经过多次算术更新后变量的最终值。请逐行写出变量的中间值,而不要只写最终答案,因为过程分是按正确追踪给出的。


3. Operator Precedence and Order of Evaluation | 运算符优先级与求值顺序

Operator precedence defines which operator is applied first when an expression contains more than one. Most languages follow the familiar BIDMAS/PEMDAS ordering: brackets, indices (powers), division and multiplication, then addition and subtraction. Boolean operators are evaluated after all arithmetic operators, and assignment is evaluated last of all.

运算符优先级定义了一个表达式含有多个运算符时,先执行哪一个。大多数语言遵循熟悉的 BIDMAS/PEMDAS 顺序:括号、指数(幂)、乘除,然后加减。布尔运算符在所有算术运算符之后求值,赋值最后求值。

Level | 层级Operators | 运算符Associativity | 结合性
1 (highest) | 1(最高)( ) | ( )left to right | 从左到右
2 | 2 ^ | ^right to left | 从右到左
3 | 3* / // % | * / // %left to right | 从左到右
4 | 4+ - | + -left to right | 从左到右
5 | 5< > &lt;= &gt;= == != | < > &lt;= &gt;= == !=left to right | 从左到右
6 | 6NOT | NOTright to left | 从右到左
7 | 7AND | ANDleft to right | 从左到右
8 | 8OR | ORleft to right | 从左到右
9 (lowest) | 9(最低)= += -= etc. | = += -= 等right to left | 从右到左

Worked example: 2 + 3 * 4 ** 2 - 6 // 4 1. Finally addition and subtraction: 2 + 48 - 1 = 49. Never skip a step in an exam answer.

示例:2 + 3 * 4 ** 2 - 6 // 4。先算幂:4 ** 2 = 16。再按从左到右做乘法和整除:3 * 16 = 48,6 // 4 = 1。最后做加减:2 + 48 - 1 = 49。考试作答时不要跳过任何一步。

If two operators have the same precedence, associativity decides. 20 - 5 - 3 - 5) - 3 = 12, not 20 - (5 - 3) = 18. By contrast, exponentiation is right associative, so 2 3 2 = 2 ** 9 = 512.

如果两个运算符优先级相同,则由结合性决定。20 - 5 - 3 是 (20 - 5) - 3 = 12,而不是 20 - (5 - 3) = 18。相反,幂运算为右结合,因此 2 3 2 = 2 ** 9 = 512。


4. Relational Operators | 关系运算符

Relational operators compare two values and return a Boolean result, which is either True or False. They are the building blocks of every selection statement (IF, CASE) and every loop condition (WHILE, FOR).

关系运算符比较两个值,并返回布尔结果,即 True 或 False。它们是所有选择结构(IF、CASE)和循环条件(WHILE、FOR)的基本构件。

Comparisons return a Boolean, not a number, so 5 > 3 evaluates to True and can be stored in a Boolean variable or used directly inside an IF statement. Mixing types causes errors: comparing a string to an integer, such as "5" > 3, is a type error in Python even though it may look harmless.

比较运算返回布尔值而非数字,因此 5 > 3 求值为 True,可以存入布尔变量或直接用在 IF 语句中。混用类型会导致错误:把字符串与整数比较,例如 "5" > 3,在 Python 中虽然看起来无害,却会触发类型错误。

(x ≥ 10) AND (x ≤ 20) ⇔ 10 ≤ x ≤ 20

Note that in most languages you cannot write the chained form directly in code; you must combine two comparisons with AND, even though mathematically the shorthand is valid.

注意,在大多数语言中不能直接在代码里写上述链式形式,必须用 AND 连接两个比较,尽管数学上这种简写是成立的。


5. Boolean (Logical) Operators | 布尔(逻辑)运算符

Boolean operators combine or invert truth values. The three standard operators are AND, OR and NOT. 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.

布尔运算符用于组合或反转真值。三种标准运算符是 AND、OR 和 NOT。仅当两个操作数都为 True 时 AND 才为 True;只要有一个操作数为 True,OR 就为 True;NOT 反转单个真值。

ABA AND BA OR BNOT A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue

Many languages use short-circuit evaluation: in A AND B, if A is False the whole expression is already False, so B is never evaluated. In A OR B, if A is True the result is already True. This matters when B contains a function call or a division that could fail.

许多语言采用短路求值:在 A AND B 中,若 A 为 False,整个表达式已确定为 False,因此 B 不会被求值。在 A OR B 中,若 A 为 True,结果已确定为 True。当 B 中含有函数调用或可能出错的除法时,这一点非常关键。

Exam questions often ask you to simplify a Boolean expression or to substitute values. Always substitute numeric comparisons first, then apply NOT, then AND, then OR.

考题常要求化简布尔表达式或代入取值。请先代入数值比较,再依次应用 NOT、AND、OR。


6. Assignment Operators | 赋值运算符

The assignment operator stores a value in a variable. In pseudocode it is written as ← t-hand side must be a variable, never an expression, because you cannot assign to a computed value.

赋值运算符把值存入变量。伪代码中写作 ← 或 =,Python、Java 与 C 中写作 =。左侧必须是变量,绝不能是表达式,因为你无法给一个计算结果赋值。

广告 / Sponsored

A classic trap is confusing = with ==. The single equals sign assigns; the double equals sign compares. Writing if x = 5 in Python is a syntax error, but in some C-family languages it is legal and silently assigns 5, producing a program that always takes the same branch.

经典陷阱是把 = 与 == 混淆。单等号是赋值,双等号是比较。在 Python 中写 if x = 5 是语法错误,但在某些 C 系语言中它是合法的,会静默地把 5 赋给 x,导致程序永远走同一分支。

x = x + 1 ⇔ x += 1

Another subtlety is that assignment is an expression in many languages, returning the assigned value, which is why a = b = c = 0 works and assigns 0 to all three variables right to left.

另一个细节是:在许多语言中赋值本身也是表达式,会返回被赋的值,因此 a = b = c = 0 成立,并从右到左把 0 赋给三个变量。


7. String, Character and Concatenation Operators | 字符串、字符与拼接运算符

Not all operators work on numbers. The plus sign performs concatenation when both operands are strings: "Hello" + " " + "World" produces "Hello World". Some languages also allow repetition with *, so "ab" * 3 gives "ababab".

并非所有运算符都作用于数值。当两个操作数都是字符串时,加号执行拼接:"Hello" + " " + "World" 得到 "Hello World"。某些语言还允许用 * 做重复,例如 "ab" * 3 得到 "ababab"。

String comparison uses character codes, so uppercase letters sort before lowercase ones in ASCII. That means "Zebra" < "apple" evaluates to True in Python, a result that surprises many candidates.

字符串比较基于字符编码,因此在 ASCII 中大写字母排在小写字母之前。这意味着在 Python 中 "Zebra" < "apple" 的结果是 True,这一点常让考生意外。


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

Examiners award marks for correct working, so trace expressions methodically. Apply this five-step routine to any expression, however complicated it looks.

考官会根据正确处理步骤给分,因此要有条理地追踪表达式。无论表达式看起来多复杂,都可以套用下面这个五步流程。

Worked example: evaluate NOT (3 + 4 * 2 > 10) OR (8 // 3 = 2) OT True is False. Second bracket: 8 // 3 = 2, which is True. Finally False OR True = True.

示例:求 NOT (3 + 4 * 2 > 10) OR (8 // 3 = 2)。先算括号内:4 * 2 = 8,再 3 + 8 = 11。比较:11 > 10 为 True。NOT True 为 False。第二个括号:8 // 3 = 2,为 True。最后 False OR True = True。

When a question asks you to state the value of a variable after a loop, build a trace table with one column per variable and one row per iteration. Trace tables are examinable in their own right and are the safest way to avoid arithmetic slips.

当题目要求写出循环结束后变量的值时,请建立追踪表,每个变量一列,每次迭代一行。追踪表本身就是考点,也是避免算错的最稳妥方式。


9. Type, Division and Modulo Pitfalls | 类型、除法与取模常见陷阱

Most marks lost on this topic come from type and division errors rather than from not knowing the operators. Understanding the difference between real division and integer division, and between positive and negative operands, removes most of those losses.

本主题失分大多源于类型与除法错误,而不是不认识运算符。弄清实数除法与整除的区别,以及正负操作数的差异,就能消除大部分失分。

−17 ÷ 5 = −3 remainder −2 ⇔ −17 = 5 × (−3) + (−2)

The identity a = (a // b) * b + (a % b) holds for every language and every pair of integers, and it is a quick way to check your own arithmetic when a question looks odd.

恒等式 a = (a // b) * b + (a % b) 对所有语言、所有整数对都成立,当题目看起来奇怪时,可以用它快速校验自己的计算。

Modulo is not only for remainders. It is the standard test for divisibility (n % 2 = 0 checks even numbers), for cycling through a fixed set of values, and for extracting the last digit of a number.

取模不只用于求余数。它是判断整除性的标准方法(n % 2 = 0 判断偶数),也用于在固定集合中循环取值,以及提取数字的最后一位。


10. Boolean Expressions, Truth Tables and De Morgan's Laws | 布尔表达式、真值表与德摩根定律

Complex conditions are often tested by asking you to complete a truth table. Build one row for every possible combination of the input variables: two variables need four rows, three variables need eight rows. Fill in intermediate columns for each bracket, then the final column.

复杂条件常通过要求补全真值表来考查。为输入变量的每一种组合建一行:两个变量需要四行,三个变量需要八行。先为每个括号填中间列,再填最终列。

De Morgan's laws let you push a NOT inside brackets, which often simplifies an expression dramatically:

德摩根定律可以把 NOT 推进括号内部,往往能大幅化简表达式:

NOT (A AND B) ⇔ (NOT A) OR (NOT B)

NOT (A OR B) ⇔ (NOT A) AND (NOT B)

Applying De Morgan's laws also helps when you rewrite a WHILE loop as an UNTIL loop, or negate an IF condition to create the ELSE branch. Examiners frequently set this as a two-mark question.

在把 WHILE 循环改写为 UNTIL 循环,或对 IF 条件取反以构造 ELSE 分支时,德摩根定律同样有用。考官经常把它设为 2 分题。

Remember that AND distributes over OR and vice versa, but the two forms differ: A AND (B OR C) ⇔ (A AND B) OR (A AND C), while A OR (B AND C) ⇔ (A OR B) AND (A OR C).

请记住 AND 对 OR 可分配,反之亦然,但两种形式不同:A AND (B OR C) ⇔ (A AND B) OR (A AND C),而 A OR (B AND C) ⇔ (A OR B) AND (A OR C)。


11. Exam-Style Questions and Common Errors | 考试题型与常见错误

Typical exam tasks on this topic fall into four shapes: evaluate an expression, complete a trace table, simplify a Boolean expression, and identify an error in a given piece of code. Prepare a checklist of the errors examiners plant deliberately.

本主题的典型考题有四种形式:求表达式值、补全追踪表、化简布尔表达式、找出给定代码中的错误。请针对考官故意埋设的错误准备一份清单

广告 / Sponsored
【原版引进】正版管乐队标准化训练教程 乐队总谱及教师用书1 第一册 布鲁斯皮尔森 上海音乐出版社 管弦乐练习音乐曲谱子…
音乐 备考教辅 · 正版现货 · 学习备考通用
查看详情 / View on Taobao
广告 / Sponsored
二十世纪戏剧大师表演方法系列全3册 迈斯纳方法训练教程上下+论文集 何雁 上海戏剧学院教材书籍演员表演训练 中国戏剧出…
戏剧 备考教辅 · 正版现货 · 学习备考通用
查看详情 / View on Taobao

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

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