📚 Operators and Expressions in Edexcel A-Level Programming | Edexcel A-Level 编程中的运算符与表达式
In Edexcel A-Level Computer Science, programming questions often depend on a precise understanding of operators and how expressions are evaluated. This article explains arithmetic, relational, logical and assignment operators, along with precedence, type conversion and common exam pitfalls. The content is suitable for both Paper 2 algorithm questions and practical programming tasks.
在 Edexcel A-Level 计算机科学中,编程题往往取决于对运算符及表达式求值方式的准确理解。本文讲解算术、关系、逻辑和赋值运算符,以及优先级、类型转换和常见考试误区。内容适合 Paper 2 算法题和实际编程任务。
1. What Are Operators and Expressions? | 什么是运算符与表达式?
An operator is a symbol that tells the computer to perform a specific operation on one or more operands. An expression is a sequence of operands and operators that can be evaluated to produce a single value.
运算符是告诉计算机对一个或多个操作数执行特定操作的符号。表达式是由操作数和运算符组成的序列,它可以被求值并产生一个单一的值。
For example, in the expression x + y * 2, the operands are x, y and 2, while the operators are + and *. The order in which operators are applied is governed by precedence rules, which we will explore later.
例如,在表达式 x + y * 2 中,操作数是 x、y 和 2,运算符是 + 和 *。运算符应用的顺序由优先级规则决定,我们稍后会深入探讨。
2. Arithmetic Operators | 算术运算符
cel pseudocode and Python, the main arithmetic operators are +, -, *, /, DIV or //, MOD or %, and exponentiation such as ^ or **.
算术运算符执行数学计算。在 Edexcel 伪代码和 Python 中,主要的算术运算符包括 +、-、*、/、DIV 或 //、MOD 或 %,以及幂运算如 ^ 或 **。
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 2 | 7 |
| - | Subtraction | 5 - 2 | 3 |
| * | Multiplication | 5 * 2 | 10 |
| / | True division | 5 / 2 | 2.5 |
| // or DIV | Integer division | 5 // 2 | 2 |
| % or MOD | Modulo (remainder) | 5 % 2 | 1 |
| ** or ^ | Exponentiation | 5 ** 2 | 25 |
In Edexcel pseudocode, you may see DIV and MOD written as words instead of symbols. The DIV operator gives the whole-number part of a division, while MOD gives the remainder.
在 Edexcel 伪代码中,你可能会看到 DIV 和 MOD 以单词形式出现,而不是符号。DIV 运算符给出除法中的整数部分,MOD 给出余数。
3. Relational / Comparison Operators | 关系(比较)运算符
Relational operators compare two values and return a Boolean result: either TRUE or FALSE. These operators are essential for building conditions in selection and iteration statements.
关系运算符比较两个值并返回布尔结果:TRUE 或 FALSE。这些运算符对于在选择和循环语句中构建条件至关重要。
| Operator | Meaning | Example | Evaluates to |
|---|---|---|---|
| == | Equal to | 5 == 5 | TRUE |
| != | Not equal to | 5 != 5 | FALSE |
| < | Less than | 3 < 5 | TRUE |
| > | Greater than | 3 > 5 | FALSE |
| <= | Less than or equal to | 5 <= 5 | TRUE |
| >= | Greater than or equal to | 5 >= 5 | TRUE |
A common mistake is writing = when you mean ==. The single equals sign is assignment, while the double equals sign is a comparison for equality.
一个常见错误是当你想表达 == 时却写成了 =。单个等号是赋值,双等号才是相等比较。
4. Logical Operators | 逻辑运算符
Logical operators combine Boolean expressions. The three main logical operators are AND, OR and NOT. They follow the rules of Boolean algebra and are used in compound conditions such as age >= 18 AND status == 'active'.
逻辑运算符用于组合布尔表达式。三个主要的逻辑运算符是 AND、OR 和 NOT。它们遵循布尔代数规则,并用于复合条件,例如 age >= 18 AND status == 'active'。
| A | B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| FALSE | FALSE | FALSE | FALSE | TRUE |
| FALSE | TRUE | FALSE | TRUE | TRUE |
| TRUE | FALSE | FALSE | TRUE | FALSE |
| TRUE | TRUE | TRUE | TRUE | FALSE |
Short-circuit evaluation is often examined: for A AND B, if A is FALSE then B is not evaluated; for A OR B, if A is TRUE then B is not evaluated. This can prevent runtime errors in expressions such as x != 0 AND 10 / x > 2.
短路求值经常被考查:对于 A AND B,如果 A 为 FALSE,则不会求值 B;对于 A OR B,如果 A 为 TRUE,则不会求值 B。这可以防止诸如 x != 0 AND 10 / x > 2 这样的表达式出现运行时错误。
5. Assignment Operators and Compound Assignment | 赋值运算符与复合赋值
The basic assignment operator is = in most languages. It assigns the value on the right to the variable on the left. For example, score = 10 stores 10 in the variable score.
在大多数语言中,基本赋值运算符是 =。它将右侧的值赋给左侧的变量。例如,score = 10 将 10 存储到变量 score 中。
Compound assignment operators combine an arithmetic operation with assignment. For instance, x += 5 is equivalent to x = x + 5. Similarly, x *= 2 means x = x * 2, and x //= 3 means x = x // 3.
复合赋值运算符将算术运算与赋值结合起来。例如,x += 5 等价于 x = x + 5。类似地,x *= 2 表示 x = x * 2,而 x //= 3 表示 x = x // 3。
+=add and assign-=subtract and assign*=multiply and assign/=divide and assign//=integer divide and assign%=modulo and assign**=exponentiate and assign
These operators make code shorter and are often used inside loops to update counters or totals.
这些运算符使代码更简洁,并且常用于循环内部更新计数器或总计。
6. Operator Precedence and Associativity | 运算符优先级与结合性
Operator precedence determines the order in which operations are evaluated in an expression. Higher precedence operators are applied before lower precedence operators. When two operators have the same precedence, associativity determines whether evaluation is left-to-right or right-to-left.
运算符优先级决定表达式中运算的求值顺序。优先级较高的运算符先于优先级较低的运算符应用。当两个运算符具有相同优先级时,结合性决定求值是从左到右还是从右到左。
The typical precedence order, from highest to lowest, is shown below:
从高到低的典型优先级顺序如下:
- Parentheses
()— highest priority - Exponentiation
**or^— right-to-left associative - Unary positive and negative
+x,-x - Multiplication, division, integer division, modulo
* / // %— left-to-right - Addition and subtraction
+ -— left-to-right - Relational comparisons
< > <= >= == != - Logical NOT
NOT - Logical AND
AND - Logical OR
OR - Assignment operators
= += -= *= /=— lowest priority, right-to-left
For example, 2 + 3 * 4 luates the multiplication first, giving 2 + 12 = 14. To change the order, use parentheses: (2 + 3) * 4 = 20.
例如,2 + 3 * 4 先计算乘法,得到 2 + 12 = 14。要改变顺序,请使用括号:(2 + 3) * 4 = 20。
7. Type Conversion in Expressions | 表达式中的类型转换
Expressions often mix different data types, such as integers, real numbers and strings. Type conversion changes a value from one data type to another. Implicit conversion happens automatically, while explicit conversion is written by the programmer using functions like int(), float() and str().
表达式经常混合不同的数据类型,如整数、实数和字符串。类型转换将值从一种数据类型更改为另一种。隐式转换自动发生,而显式转换由程序员使用 int()、float() 和 str() 等函数编写。
For example, int(3.9) truncates the decimal part and returns 3, not 4. float(7) returns 7.0. The expression "3" + "4" produces the string "34", but int("3") + int("4") produces the integer 7.
例如,int(3.9) 会截去小数部分并返回 3,而不是 4。float(7) 返回 7.0。表达式 "3" + "4" 生成字符串 "34",但 int("3") + int("4") 生成整数 7。
In integer division, the result is an integer even if the operands are integers. This can cause unexpected truncation: 1 / 2 is 0.5 in true division, but 1 // 2 is 0, and 1 % 2 is 1.
Published by TutorHao | A-Level 编程 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

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