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

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

Operators are the building blocks of any programming solution. In Edexcel A-Level Programming, you must be able to read, write and evaluate expressions using arithmetic, relational, Boolean, string and bitwise operators. This article covers the exact skills needed for pseudocode questions, Python implementation and exam-style tracing tasks.

运算符是任何编程解决方案的基本构件。在 Edexcel A-Level 编程中,你必须能够阅读、编写和计算使用算术、关系、布尔、字符串和位运算符的表达式。本文涵盖伪代码题、Python 实现和考试风格追踪任务所需的具体技能。

Operators are not just symbols to memorise. They determine the order of evaluation, the type of result produced and the logical flow of decisions. A strong understanding here supports later topics such as selection, iteration, data structures and algorithm design.

运算符不仅仅是需要记忆的符号。它们决定了计算顺序、产生的结果类型以及决策的逻辑流程。牢固掌握这部分内容将帮助你学习后续的选择结构、迭代、数据结构和算法设计等主题。


1. Why Operators Matter in Edexcel Programming | 为什么运算符在 Edexcel 编程中重要

In the Edexcel A-Level specification, programming questions often ask you to trace code, identify the output of an expression, or write a condition that controls a loop or selection. A single mistake in operator precedence can change the entire result.

在 Edexcel A-Level 考试大纲中,编程题经常要求你追踪代码、确定表达式的输出,或编写控制循环或选择结构的条件。优先级的一个小错误就可能改变整个结果。

Examiners expect you to be confident with both mathematical operators and logical operators. You should be able to convert between a problem statement in English and a programming expression, such as ‘age is at least 18 and score is not lower than 70’.

考官希望你熟练掌握数学运算符和逻辑运算符。你应该能够在英语问题描述和编程表达式之间进行转换,例如 ‘年龄至少为 18 且分数不低于 70’。

This topic is also central to algorithm design. Conditions like while count < 10 or if total >= 100 depend on correct comparison operators and their return values.

本主题对算法设计也至关重要。诸如 while count < 10if total >= 100 之类的条件依赖于正确的比较运算符及其返回值。


2. Arithmetic Operators and Integer Division | 算术运算符与整数除法

The core arithmetic operators are addition +, subtraction -, multiplication *, division /, integer division DIV, and modulus MOD. In Python, integer division is written // and modulus is written %.

核心算术运算符包括加 +、减 -、乘 *、除 /、整数除 DIV 和取模 MOD。在 Python 中,整数除法写作 //,取模写作 %

Integer division gives the whole number part of a division result, while modulus gives the remainder. For example, 17 DIV 5 gives 3 and 17 MOD 5 gives 2.

整数除法给出除法结果的整数部分,而取模给出余数。例如,17 DIV 5 得到 3,17 MOD 5 得到 2。

  • 7 + 3 = 10
  • 7 - 3 = 4
  • 7 * 3 = 21
  • 7 / 3 = 2.333...
  • 7 DIV 3 = 2
  • 7 MOD 3 = 1

Be careful with negative numbers in integer division and modulus. In Edexcel pseudocode, MOD normally returns a remainder with the same sign as the dividend. Always check the context and the exact definition given in a question.

处理负数的整数除法和取模时要小心。在 Edexcel 伪代码中,MOD 通常返回与被除数符号相同的余数。请始终检查题目中给出的上下文和定义。

Arithmetic operators produce numeric results. These results can be stored in variables, used in conditions, or passed as arguments to functions.

算术运算符产生数值结果。这些结果可以存储在变量中、用于条件判断或作为参数传递给函数。


3. Relational Operators and Comparison | 关系运算符与比较

Relational operators compare two values and return a Boolean value: TRUE or FALSE. The main operators are equal to = or ==, not equal to <> or !=, less than <, greater than >, less than or equal to <=, and greater than or equal to >=.

关系运算符比较两个值并返回布尔值 TRUEFALSE。主要运算符包括等于 ===、不等于 <>!=、小于 <、大于 >、小于等于 <= 和大于等于 >=

In pseudocode, a single equals sign is often used for both assignment and comparison, which can be confusing. In Python, = means assignment and == means comparison. Edexcel questions usually make the distinction clear.

在伪代码中,单个等号常常同时用于赋值和比较,这可能造成混淆。在 Python 中,= 表示赋值,== 表示比较。Edexcel 题目通常会明确区分。

Expression Result
5 < 8 TRUE
12 >= 12 TRUE
9 != 4 TRUE
7 = 7.0 TRUE in Python but may be examined carefully

When comparing strings, relational operators usually use lexicographic order based on character codes. For example, 'A' < 'B' is true, but case differences can affect results.

比较字符串时,关系运算符通常使用基于字符编码的字典序。例如,'A' < 'B' 为真,但大小写差异可能影响结果。


4. Boolean Operators and Truth Tables | 布尔运算符与真值表

Boolean operators combine or modify Boolean values. The three fundamental operators are AND, OR and NOT. Some specifications also include XOR.

布尔运算符组合或修改布尔值。三个基本运算符是 ANDORNOT。一些考试大纲还包括 XOR

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

Short-circuit evaluation is an important concept. In many languages, if the first operand of AND is false, the second operand is not evaluated because the whole expression must be false. This can prevent runtime errors, such as dividing by zero.

短路求值是一个重要概念。在许多语言中,如果 AND 的第一个操作数为假,第二个操作数就不会被计算,因为整个表达式必定为假。这样可以防止运行时错误,例如除以零。

Boolean expressions often appear inside IF statements and WHILE loops. For example, IF score >= 60 AND score <= 100 THEN checks that score is in the valid range.

布尔表达式经常出现在 IF 语句和 WHILE 循环中。例如,IF score >= 60 AND score <= 100 THEN 检查分数是否在有效范围内。


5. Operator Precedence and Evaluation Order | 运算符优先级与计算顺序

Operator precedence determines which part of an expression is evaluated first. In most programming languages, the order from highest to lowest is: parentheses, arithmetic operators, relational operators, Boolean operators, and assignment.

运算符优先级决定了表达式的哪一部分先被计算。在大多数编程语言中,从高到低的顺序依次是:括号、算术运算符、关系运算符、布尔运算符和赋值。

Priority Operator group Examples
1 (highest) Parentheses ( )
2 Arithmetic * / DIV MOD
3 Arithmetic addition + -
4 Relational < > <= >= = !=
5 NOT NOT
6 AND AND
7 (lowest) OR OR

Because AND has higher precedence than OR, the expression A OR B AND C is evaluated as A OR (B AND C), not (A OR B) AND C. Use parentheses to make your intention explicit.

由于 AND 的优先级高于 OR,表达式 A OR B AND C 会按 A OR (B AND C) 计算,而不是按 (A OR B) AND C。请使用括号明确表达你的意图。

For example, evaluate 3 + 4 * 2. Multiplication is done first, giving 3 + 8 = 11. With parentheses, (3 + 4) * 2 gives 14.

例如,计算 3 + 4 * 2。先执行乘法,得到 3 + 8 = 11。使用括号 (3 + 4) * 2 则得到 14。


6. Assignment and Compound Assignment | 赋值与复合赋值

Assignment places a value into a variable. In pseudocode and Python, the left side is a variable and the right side is an expression. The value is calculated first, then stored.

赋值将值放入变量。在伪代码和 Python 中,左边是变量,右边是表达式。先计算值,然后再存储。

Compound assignment operators combine arithmetic with assignment. Examples include +=, -=, *= and /=. In pseudocode, you may see this written explicitly as total ← total + 5.

复合赋值运算符将算术运算与赋值结合。示例包括 +=-=*=/=。在伪代码中,你可能看到显式写成 total ← total + 5

  • x += 3 is the same as x = x + 3
  • count -= 1 is the same as count = count - 1
  • factor *= 2 is the same as factor = factor * 2

In Edexcel pseudocode, the left arrow is often used for assignment. You should be able to read this symbol correctly and use it consistently in your own written answers.

在 Edexcel 伪代码中,左箭头 通常用于赋值。你应当能够正确读取此符号,并在自己书写的答案中一致使用。

A common mistake is writing x + 1 = y. This is invalid because assignment must place a value into a variable, not an expression. Always put the variable on the left.

一个常见错误是写 x + 1 = y。这是无效的,因为赋值必须将值放入变量,而不是表达式。始终把变量放在左边。


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

String operators allow you to combine or manipulate text. The most important is concatenation, which joins two strings together. In pseudocode, this is often written as + or &; in Python it is +.

字符串运算符允许你组合或操作文本。最重要的是拼接,它将两个字符串连接在一起。在伪代码中,这通常写作 +&;在 Python 中写作 +

For example, 'Hello' + ' ' + 'World' gives 'Hello World'. The order matters, and spaces must be added explicitly if needed.

例如,'Hello' + ' ' + 'World' 得到 'Hello World'。顺序很重要,如果需要空格,必须显式添加。

String comparison is also tested. The expression 'a' < 'b' is true because ‘a’ has a lower character code than ‘b’. However, 'A' < 'a' is also true in ASCII because uppercase letters have lower codes than lowercase letters.

字符串比较也是考点。表达式 'a' < 'b' 为真,因为 ‘a’ 的字符编码小于 ‘b’。然而,在 ASCII 中 'A' < 'a' 也为真,因为大写字母的编码小于小写字母。

Some languages provide substring and length functions, but these are not operators. Edexcel questions usually focus on concatenation and comparison of strings in conditions.

一些语言提供子串和长度函数,但这些不是运算符。Edexcel 题目通常关注字符串的拼接和在条件中的比较。


8. Bitwise Operations for Low-Level Data | 面向底层数据的位运算

Bitwise operators act on individual bits of integer values. The main bitwise operators are AND, OR, XOR, NOT, left shift <<, and right shift >>.

位运算符作用于整数值的各个二进制位。主要位运算符包括 ANDORXORNOT、左移 << 和右移 >>

For example, in binary, 5 is 0101 and 3 is 0011. The bitwise AND gives 0101 AND 0011 = 0001, which is decimal 1. The bitwise OR gives 0111, which is decimal 7.

例如,在二进制中,5010130011。按位与得到 0101 AND 0011 = 0001,即十进制 1。按位或得到 0111,即十进制 7。

Left shift moves all bits to the left by a given number of places, filling with zeros on the right. Each left shift is equivalent to multiplying by 2. A right shift is equivalent to integer division by 2.

左移将所有位向左移动指定的位数,右侧补零。每次左移相当于乘以 2。右移相当于整数除以 2。

Bitwise operations are less common in basic Edexcel programming questions, but they appear in topics such as data representation, binary manipulation and low-level programming. You should recognise the symbols and know their logical behaviour.

位运算在基础 Edexcel 编程题中较少见,但会出现在数据表示、二进制操作和底层编程等主题中。你应该认识这些符号并了解其逻辑行为。


9. Common Errors and Exam Traps | 常见错误与考试陷阱

One classic trap is using a single equals sign when a comparison is intended. In Python, if x = 5 causes a syntax error, while if x == 5 is correct. In pseudocode, the meaning may depend on context.

一个经典陷阱是在需要比较时使用了单个等号。在 Python 中,if x = 5 会导致语法错误,而 if x == 5 才是正确的。在伪代码中,含义可能取决于上下文。

Another common mistake is ignoring precedence. The expression 2 + 6 * 3 is 20, not 24, because multiplication comes before addition. Always evaluate arithmetic before relational operators unless parentheses force a different order.

另一个常见错误是忽略优先级。表达式 2 + 6 * 3 是 20,而不是 24,因为乘法的优先级高于加法。除非括号强制改变顺序,否则总是先计算算术运算,再计算关系运算符。

Division by zero is a major runtime error. In a condition like IF x > 0 AND y / x > 5, short-circuit evaluation can protect you, but only if the language guarantees it. Do not rely on this unless the question states it.

除以零是一个严重的运行时错误。在像 IF x > 0 AND y / x > 5 这样的条件中,短路求值可以保护你,但只有在语言保证短路时才能依赖它。

Confusing AND with OR changes the logic completely. The condition x > 0 AND x < 10 is true only when both are true, while x > 0 OR x < 10 may be true for almost any value.

混淆 ANDOR 会完全改变逻辑。条件 x > 0 AND x < 10 仅当两者都为真时才为真,而 x > 0 OR x < 10 几乎对任何值都为真。


10. Worked Exam-Style Questions | 考试风格例题解析

Question: Evaluate 10 MOD 3 + 2 * 4. First evaluate multiplication: 2 * 4 = 8. Then modulus: 10 MOD 3 = 1. Then addition: 1 + 8 = 9.

问题:计算 10 MOD 3 + 2 * 4。首先计算乘法:2 * 4 = 8。然后计算取模:10 MOD 3 = 1。最后计算加法:1 + 8 = 9

Question: Given a = 5, b = 12, what is the value of a < b AND b MOD a = 2? First, a < b is 5 < 12, which is TRUE. Then, b MOD a is 12 MOD 5 = 2, so the second condition is also TRUE. Therefore the whole expression is TRUE AND TRUE = TRUE.

问题:给定 a = 5b = 12a < b AND b MOD a = 2 的值是什么?首先,a < b5 < 12,为 TRUE。然后,b MOD a12 MOD 5 = 2,因此第二个条件也为 TRUE。所以整个表达式为 TRUE AND TRUE = TRUE。

Question: Write a condition that is true when age is at least 18 and has a valid ID. The condition is age >= 18 AND hasValidID or age >= 18 AND id != '' depending on how the ID is represented.

问题:编写一个条件,当年龄至少为 18 且持有有效 ID 时为真。该条件是 age >= 18 AND hasValidIDage >= 18 AND id != '',具体

Published by TutorHao | A-Level 编程 Revision Series | aleveler.com

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

Comments

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

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