📚 Operators and Expressions in Edexcel A Level Programming | Edexcel A Level 编程中的运算符与表达式
Expressions are the building blocks of every program. In Edexcel A Level Computer Science, you need to combine literals, variables, operators and function calls to produce values, control flow and Boolean logic. This article explains the operators you must know, how they are ranked by precedence, how data types affect the result of an operation, and how to evaluate expressions accurately in exam questions and in pseudocode that you write yourself.
表达式是每个程序的基本构造块。在 Edexcel A Level 计算机科学中,你需要组合字面量、变量、运算符和函数调用,以产生值、控制流程和布尔逻辑。本文解释你必须掌握的运算符、它们如何按优先级排序、数据类型如何影响运算结果,以及如何在考试题中、以及在你自己编写的伪代码中准确求值表达式。
n on the syntax of a particular real language. That means you must be precise about operator names such as DIV and MOD, about the difference between the operator = and the assignment arrow ←, and about the order in which operations are carried out.
本指南全部内容都针对 Edexcel 考纲。在 Edexcel 考试中,评分看的是你伪代码的逻辑,而不是某一门真实编程语言的语法。这意味着你必须准确使用 DIV、MOD 这类运算符名称,必须区分比较运算符 = 与赋值符号 ←,并且必须清楚运算执行的先后顺序。
1. Data Types and Literals | 数据类型与字面量
In programming, a literal is a fixed value written directly in the code. The value's type determines which operations are allowed on it and what type of result those operations produce. Edexcel pseudocode expects you to distinguish clearly between integer, real (float), Boolean, character and string data types, and to use the correct quotation marks for characters and strings.
在编程中,字面量是直接写在代码中的固定值。值的类型决定了允许对它进行哪些运算,以及这些运算会产生什么类型的结果。Edexcel 伪代码要求你明确区分整数、实数(浮点数)、布尔值、字符和字符串数据类型,并为字符和字符串使用正确的引号。
tatement, by contrast, is a complete instruction that does something, such as assigning a value with ←, calling a procedure, or controlling the flow of the program with IF or WHILE.
表达式是字面量、变量、运算符和函数调用的任意组合,它会产出一个值,例如 3 * price + 1 或 age >= 18。相比之下,语句是一条完整的指令,它执行某个动作,例如用 ← 赋值、调用过程,或用 IF、WHILE 控制程序流程。
Examples of literals: 42 (integer), 3.14 (real), True (Boolean), 'A' (character) and "hello" (string). Mixing incompatible types without casting can cause errors or unexpected results, and in an exam answer it will lose you marks even if the logic looks correct.
字面量示例:42(整数)、3.14(实数)、True(布尔值)、'A'(字符)和 "hello"(字符串)。不进行类型转换就混合不兼容类型会导致错误或意外结果,在考试作答中即使逻辑看起来正确也会失分。
| Data Type | Example Literal | Significance |
|---|---|---|
| Integer | 42, -7 | Whole numbers, no fractional part |
| Real / Float | 3.14, -0.5 | Numbers with decimal points |
| Boolean | True, False | Only two possible values |
| Character | 'A', '7' | Single symbol in single quotes |
| String | "hello", "123" | Sequence of characters |
Remember that the character '7' is not the same as the integer 7. The character is a symbol; the integer is a numeric quantity. This distinction is tested in Edexcel programming questions, especially when you read input from the user as a string and then need to convert it before doing arithmetic.
记住字符 '7' 与整数 7 不同。字符是符号,整数是数值量。这一区分在 Edexcel 编程题中会被考查,特别是在你把用户输入读成字符串、随后需要转换后再做算术运算的时候。
2. Arithmetic Operators | 算术运算符
cation (*), real division (/), integer division (DIV), modulus or remainder (MOD) and exponentiation (^). Real division always produces a real result, even when both operands are integers.
算术运算符接受数值操作数并返回数值结果。Edexcel 伪代码识别加(+)、减(-)、乘(*)、实数除法(/)、整除(DIV)、取模或求余(MOD)以及乘方(^)。实数除法总是产生实数结果,即使两个操作数都是整数。
ething from it. Unary minus binds very tightly, so -3 ^ 2 is read as -(3 ^ 2) = -9, not as (-3) ^ 2 = 9. This is a classic trap in expression-evaluation questions.
一元负号也是算术运算符,但它只接受一个操作数:在 -x 中,负号是取 x 的相反数,而不是从某个值中减去什么。一元负号的结合非常紧密,因此 -3 ^ 2 被读作 -(3 ^ 2) = -9,而不是 (-3) ^ 2 = 9。这是表达式求值题中的经典陷阱。
- + adds two numbers, or concatenates two strings.
- - subtracts the second operand from the first.
- * multiplies two numbers.
- / performs real division: 7 / 2 gives 3.5.
- DIV performs integer division and discards the fractional part: 7 DIV 2 gives 3.
- MOD gives the remainder: 7 MOD 2 gives 1.
- ^ raises the first operand to the power of the second: 2 ^ 10 gives 1024.
- + 将两个数相加,或连接两个字符串。
- - 从第一个操作数中减去第二个操作数。
- * 将两个数相乘。
- / 执行实数除法:7 / 2 得到 3.5。
- DIV 执行整除并舍弃小数部分:7 DIV 2 得到 3。
- MOD 求余数:7 MOD 2 得到 1。
- ^ 求第一个操作数的第二个操作数次幂:2 ^ 10 得到 1024。
In exam answers you must write the operator names DIV and MOD in capitals, and you should never use a slash when you mean integer division. Mark schemes frequently award a mark specifically for choosing DIV or MOD correctly, so choosing "/" and hoping the examiner is generous is a poor strategy.
在考试作答中,你必须用大写写出 DIV 和 MOD 这两个运算符名称,并且在需要整除时绝不要用斜杠。评分标准常常专门为正确选用 DIV 或 MOD 给分,因此写 "/" 然后指望考官宽容是很糟糕的策略。
3. Integer Division, MOD and Their Uses | 整除、取模及其用途
DIV and MOD are two halves of the same idea. For any two integers a and b where b ≠ 0, integer division gives the whole-number quotient and MOD gives the leftover remainder. Together they satisfy a simple identity that is worth memorising because it lets you check your own answers in the exam.
DIV 和 MOD 是同一个思想的两半。对于任意两个整数 a 和 b(b ≠ 0),整除给出整数商,MOD 给出剩下的余数。它们共同满足一个简单的恒等式,值得记住,因为它能让你在考场上自行检验答案。
a = (a DIV b) × b + (a MOD b)
se two operations mentally is essential for questions on number bases, hashing and array indexing.
计算示例:47 DIV 10 = 4,47 MOD 10 = 7,因为 4 × 10 + 7 = 47。同样地,100 DIV 7 = 14,100 MOD 7 = 2,因为 14 × 7 + 2 = 100。能够心算这两个运算,对于进制转换、哈希和数组索引类题目至关重要。
st digit of a number (n MOD 10), and wrapping an index around the end of an array. Typical uses of DIV include removing the last digit (n DIV 10), converting seconds into whole minutes, and calculating a row or column position from a single index.
MOD 的典型考试用途包括:判断一个数是偶数还是奇数(n MOD 2 = 0 表示偶数)、取出数字的最后一位(n MOD 10)、以及让索引在数组末尾循环回绕。DIV 的典型用途包括:去掉最后一位(n DIV 10)、把秒数换成整分钟数、以及由单个索引计算出行号或列号。
taken as -2, the remainder is -1. Always show the identity in your working so the examiner can follow your reasoning.
对负数操作数要小心。如果题目要求 -7 MOD 3,就用上面的恒等式:若把 -7 DIV 3 取为 -3,则 -7 MOD 3 = -7 - (-3 × 3) = 2;若取为 -2,则余数为 -1。一定要在解题过程中写出这个恒等式,让考官能跟上你的推理。
4. Relational (Comparison) Operators | 关系(比较)运算符
Relational operators compare two values and return a Boolean result, either True or False. They are the operators that turn a numeric or textual expression into a condition that a selection or iteration statement can test. Without them, no program could make a decision.
关系运算符比较两个值并返回布尔结果,即 True 或 False。正是这些运算符把数值或文本表达式变成选择语句、循环语句可以检测的条件。没有它们,程序就无法做出任何判断。
- = equal to - a single equals sign in Edexcel pseudocode means comparison, not assignment.
- <> not equal to - some languages write this as !=, but Edexcel uses the diamond.
- < less than and > greater than.
- <= less than or equal to and >= greater than or equal to.
- = 等于 —— 在 Edexcel 伪代码中,单个等号表示比较,而不是赋值。
- <> 不等于 —— 有些语言写作 !=,但 Edexcel 使用尖括号组合。
- < 小于,> 大于。
- <= 小于或等于,>= 大于或等于。
Comparisons only make sense between values of compatible types. Comparing the real 3.0 with the integer 3 gives True because the values are numerically equal; comparing the string "3" with the integer 3 is a type error. Edexcel examiners expect you to convert one of the operands, for example with INT or STRING, before comparing.
只有类型兼容的值之间比较才有意义。将实数 3.0 与整数 3 比较得到 True,因为它们在数值上相等;而将字符串 "3" 与整数 3 比较则是类型错误。Edexcel 考官期望你在比较之前转换其中一个操作数,例如使用 INT 或 STRING。
Strings can also be compared, character by character, using their order in the character set. "apple" < "banana" is True because a comes before b. This ordering property is useful when you build a sorting algorithm or when you validate that input falls within an alphabetical range.
字符串也可以比较,按字符集中的顺序逐字符进行。"apple" < "banana" 为 True,因为 a 在 b 之前。当你编写排序算法或验证输入是否落在某个字母区间内时,这种排序性质很有用。
5. Boolean (Logical) Operators | 布尔(逻辑)运算符
to build compound conditions from simple ones, such as age >= 18 AND hasLicence = True.
布尔运算符用于组合或取反布尔值。Edexcel 识别三个:AND、OR 和 NOT。它们用大写书写,允许你由简单条件构建复合条件,例如 age >= 18 AND hasLicence = True。
| 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 |
The key facts to remember are that AND returns True only when both operands are True, OR returns True when at least one operand is True, and NOT simply reverses a single Boolean value. De Morgan's laws are a useful shortcut: 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).
需要记住的关键事实是:AND 仅当两个操作数都为 True 时才返回 True;OR 只要至少一个操作数为 True 就返回 True;NOT 只是把单个布尔值取反。德摩根定律是很有用的捷径:NOT (A AND B) 等价于 (NOT A) OR (NOT B),NOT (A OR B) 等价于 (NOT A) AND (NOT B)。
In real languages, AND and OR often short-circuit: if the left operand of an AND is False, the right operand is never evaluated. This matters in Edexcel answers when the right operand could cause an error, such as an index out of range or a division by zero.
在真实语言中,AND 和 OR 常常具有短路特性:如果 AND 的左操作数为 False,右操作数就根本不会被求值。在 Edexcel 作答中,当右操作数可能导致错误(例如索引越界或除以零)时,这一点很重要。
ond part has no left operand. You must repeat the variable: age >= 18 AND age <= 65.
一个常见错误是写出 age >= 18 AND <= 65 这样的条件,这是无效的,因为后半部分没有左操作数。你必须重复变量:age >= 18 AND age <= 65。
6. String Operators and Functions | 字符串运算符与函数
Strings have their own operators and functions. The most important is concatenation, written with the + symbol in Edexcel pseudocode, which joins two strings end to end. "Hello, " + "world" produces "Hello, world".
字符串有自己的运算符和函数。最重要的是连接,在 Edexcel 伪代码中用 + 符号书写,它把两个字符串首尾相接。"Hello, " + "world" 得到 "Hello, world"。
Functions that return string information or new strings are also part of expression writing: LENGTH(s) returns the number of characters in a string, and substring or indexing operations return a single character or a shorter string. When you concatenate a number with a string, you must convert the number first, for example "Value: " + STRING(score).
返回字符串信息或新字符串的函数也属于表达式书写的一部分:LENGTH(s) 返回字符串中的字符数,子串或索引操作返回单个字符或更短的字符串。当你把数字与字符串连接时,必须先转换数字,例如 "Value: " + STRING(score)。
- Concatenation: "123" + "4" gives the string "1234".
- Numeric addition: 123 + 4 gives the integer 127.
- LENGTH("hello") gives 5.
- "5" + 5 is a type error in pseudocode unless the number is converted first.
- 连接运算:"123" + "4" 得到字符串 "1234"。
- 数值加法:123 + 4 得到整数 127。
- LENGTH("hello") 得到 5。
- 在伪代码中,"5" + 5 属于类型错误,除非先把数字转换。
The contrast between "123" + "4" = "1234" nding on the types of its operands, so always identify the operand types before you evaluate an expression.
"123" + "4" = "1234" 与 123 + 4 = 127 之间的对比,是整个考纲中最常被考查的类型意识要点之一。同一个 + 符号会根据操作数类型表现出不同行为,所以在求值表达式之前,始终要先确定操作数的类型。
7. Operator Precedence | 运算符优先级
Precedence defines the order in which operators are applied when an expression contains more than one. Edexcel follows a conventional order closely related to BODMAS: brackets first, then exponentiation, then multiplication and division operators, then addition and subtraction, then comparisons, then NOT, then AND, then OR.
优先级定义了当一个表达式含有多个运算符时它们的应用顺序。Edexcel 遵循与 BODMAS 密切相关的常规顺序:先括号,再乘方,再乘除类运算符,再加减,然后是比较,然后是 NOT,再是 AND,最后是 OR。
| Level | Operators | Associativity |
|---|---|---|
| 1 (highest) | ( ) function calls, unary - | Left to right |
| 2 | ^ | Right to left |
| 3 | * / DIV MOD | Left to right |
| 4 | + - | Left to right |
| 5 | = <> < > <= >= | Left to right |
| 6 | NOT | Right to left |
| 7 | AND | Left to right |
| 8 (lowest) | OR | Left to right |
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图