📚 Edexcel A-Level Programming: Operators, Expressions and Constructs | 爱德思A-Level编程:运算符、表达式与程序结构
Mastering operators, expressions and program constructs is essential for Edexcel A-Level Programming questions, where you are often asked to trace, write or correct pseudocode. This revision guide explains the key concepts in a structured way, with Chinese translations to support bilingual learners and direct links to common exam-style tasks.
掌握运算符、表达式和程序结构是应对爱德思A-Level编程题的基础,考试经常要求你跟踪、编写或纠正伪代码。本复习指南以结构化的方式解释核心概念,并配有中文翻译,帮助双语学习者理解,同时直接关联常见考试题型。
1. Operators and Operands | 运算符与操作数
An operator is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational or logical operation. An operand is the value or variable on which the operator acts. For example, in the expression x + y, + is the operator, while x and y are operands.
运算符是告诉编译器或解释器执行特定数学、关系或逻辑运算的符号。操作数是运算符作用的值或变量。例如,在表达式 x + y 中,+ 是运算符,x 和 y 是操作数。
In Edexcel pseudocode, operators are written in a clear and language-independent style. You should be able to identify whether an expression is arithmetic, comparison, logical or string-based, because this affects the result and the data type returned.
在爱德思伪代码中,运算符以清晰且与语言无关的方式书写。你应当能够判断一个表达式属于算术、比较、逻辑还是字符串运算,因为这会影响到结果以及返回的数据类型。
2. Arithmetic Operators | 算术运算符
The main arithmetic operators in Edexcel A-Level Programming are + (addition), - (subtraction), * (multiplication), / (division), MOD (remainder after integer division) and DIV (integer division). Some contexts also use ^ for exponentiation.
爱德思A-Level编程中的主要算术运算符有 +(加)、-(减)、*(乘)、/(除)、MOD(整数除法后的余数)和 DIV(整数除法)。某些语境也使用 ^ 表示乘方。
When using DIV and MOD, both operands are usually integers. For example, 17 DIV 5 evaluates to 3, and 17 MOD 5 evaluates to 2, because 17 = 5 × 3 + 2.
使用 DIV 和 MOD 时,两个操作数通常是整数。例如,17 DIV 5 的结果为 3,17 MOD 5 的结果为 2,因为 17 = 5 × 3 + 2。
Be careful with the division operator /. In many pseudocode specifications, / produces a real result, so 7 / 2 may be 3.5, while 7 DIV 2 gives 3.
注意除法运算符 / 的使用。在许多伪代码规范中,/ 产生实数结果,因此 7 / 2 可能为 3.5,而 7 DIV 2 得到 3。
3. Comparison and Logical Operators | 比较与逻辑运算符
Comparison operators compare two values and return a Boolean result: = (equal to), ≠ or != (not equal to), < (less than), > (greater than), <= or ≤ (less than or equal to), and >= or ≥ (greater than or equal to).
比较运算符用于比较两个值并返回布尔结果:=(等于)、≠ 或 !=(不等于)、<(小于)、>(大于)、<= 或 ≤(小于等于)以及 >= 或 ≥(大于等于)。
Logical operators combine Boolean values: AND, OR and NOT. The result of an AND expression is true only when both operands are true; the result of an OR expression is true when at least one operand is true; NOT reverses a single Boolean value.
逻辑运算符用于组合布尔值:AND、OR 和 NOT。AND 表达式只有当两个操作数都为真时才为真;OR 表达式当至少一个操作数为真时为真;NOT 对单个布尔值取反。
4. Operator Precedence and Associativity | 运算符优先级与结合性
Operator precedence determines the order in which operations are evaluated when an expression contains multiple operators. In Edexcel-style pseudocode, the typical order from highest to lowest priority is: parentheses first, then arithmetic exponentiation, then multiplication/division/DIV/MOD, then addition/subtraction, then comparisons, then NOT, then AND, then OR.
运算符优先级决定一个包含多个运算符的表达式中的求值顺序。在爱德思风格的伪代码中,从高到低的典型优先级为:括号最先,其次是算术乘方,然后是乘/除/DIV/MOD,再是加/减,之后是比较运算,然后是 NOT,接着是 AND,最后是 OR。
Associativity tells you the direction of evaluation when two operators have the same precedence. Most arithmetic operators are left-associative, meaning a - b - c is evaluated as (a - b) - c. Exponentiation, if present, is usually right-associative.
结合性说明两个相同优先级运算符的计算方向。大多数算术运算符是左结合的,即 a - b - c 按 (a - b) - c 计算。乘方运算(如果出现)通常是右结合的。
x = 3 + 4 × 2 → x = 3 + 8 → x = 11
x = (3 + 4) × 2 → x = 7 × 2 → x = 14
Always use parentheses to make your intended order explicit, even when the default precedence would produce the same result. This reduces mistakes in exam answers.
始终使用括号来明确你想要的顺序,即使默认优先级也能得到相同结果。这样可以减少考试答案中的错误。
5. Assignment and Compound Assignment | 赋值与复合赋值
An assignment statement stores a value in a variable. In Edexcel pseudocode, the left arrow ← is often used, but many questions also use =. For example, total ← total + 1 means take the current value of total, add 1, and store the result back into total.
赋值语句将值存入变量。在爱德思伪代码中,常用左箭头 ←,但许多题目也使用 =。例如,total ← total + 1 表示取 total 的当前值,加上 1,然后将结果存回 total。
Do not confuse assignment with equality testing. In many programming languages, = can mean assignment or comparison depending on context, but in Edexcel questions you must read the pseudocode carefully. If a statement is written in the form IF x = 5 THEN, it is a comparison; if it is written as x = 5 on a line by itself, it is usually assignment.
不要混淆赋值与相等性测试。在许多编程语言中,= 根据语境可能表示赋值或比较,但在爱德思题目中你必须仔细阅读伪代码。如果语句写作 IF x = 5 THEN,它是一次比较;如果单独一行写作 x = 5,通常是赋值。
Compound assignment combines an arithmetic operation with assignment. Examples include score += 10, score -= 2, score *= 3 and score /= 2. These are not always used in Edexcel pseudocode, but you may see them in coding questions or Python-based tasks.
复合赋值将算术运算与赋值合并。示例包括 score += 10、score -= 2、score *= 3 和 score /= 2。这些在爱德思伪代码中不总是出现,但在编码题或基于 Python 的任务中可能会见到。
6. Bitwise and String Operators | 位运算与字符串运算符
Bitwise operators act on the binary representation of integers. Common bitwise operators are & (AND), | (OR), ^ (XOR) and ~ (NOT), plus shift operators << and >>. They are less frequent in Edexcel A-Level pseudocode but may appear in higher-level questions on low-level operations.
位运算符作用于整数的二进制表示。常见的位运算符有 &(与)、|(或)、^(异或)和 ~(非),以及移位运算符 << 和 >>。它们在爱德思A-Level伪代码中出现较少,但在关于底层操作的高阶题中可能出现。
String operators include concatenation, often represented by + or &. For example, firstName + " " + lastName joins two strings with a space. Some pseudocode specifications also have functions such as LENGTH(), LEFT(), RIGHT() and MID() to extract substrings.
字符串运算符包括连接,通常用 + 或 & 表示。例如,firstName + " " + lastName 用空格连接两个字符串。有些伪代码规范还提供 LENGTH()、LEFT()、RIGHT() 和 MID() 等函数来提取子串。
7. Sequence, Selection and Iteration | 顺序、选择与迭代
Every program can be built from three fundamental constructs: sequence, selection and iteration. Sequence means statements are executed one after another in the order written. Selection allows a choice between different paths using IF...THEN...ELSE...ENDIF or CASE statements. Iteration repeats a block of code using FOR, WHILE or REPEAT...UNTIL loops.
每个程序都可以由三种基本结构构建:顺序、选择和迭代。顺序意味着语句按照书写顺序逐条执行。选择使用 IF...THEN...ELSE...ENDIF 或 CASE 语句在不同路径之间进行选择。迭代使用 FOR、WHILE 或 REPEAT...UNTIL 循环重复执行代码块。
In Edexcel questions, you are often asked to trace how variables change in a loop. You should keep a trace table showing the line number, variable values and any output at each step. This is the safest way to answer iterations with nested loops and conditional statements.
在爱德思题目中,你经常被要求跟踪变量在循环中的变化。你应当维护一个跟踪表,显示每一步的行号、变量值以及任何输出。这是回答带有嵌套循环和条件语句的迭代题最稳妥的方法。
8. Boolean Expressions and Truth Tables | 布尔表达式与真值表
A Boolean expression evaluates to either TRUE or FALSE. Truth tables are used to show the result of a Boolean expression for every possible combination of input values. For two Boolean inputs A and B, the table for A AND B, A OR B and NOT A is:
布尔表达式的结果只能是 TRUE 或 FALSE。真值表用于展示布尔表达式在所有可能输入组合下的结果。对于两个布尔输入 A 和 B,A AND B、A OR B 和 NOT A 的真值表如下:
| 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 |
You should be able to build a truth table for any given Boolean expression, such as (A OR B) AND NOT C. Start by listing all input combinations, then evaluate sub-expressions step by step before combining them.
你应当能够为任何给定的布尔表达式构建真值表,例如 (A OR B) AND NOT C。首先列出所有输入组合,然后逐步计算子表达式,最后再组合它们。
9. Common Exam-Style Pitfalls | 常见考试陷阱
One common mistake is confusing DIV with /. If a question asks for the integer result of 7 / 2, you must decide whether the pseudocode intends real division or integer division. In many Edexcel questions, / returns real, while DIV returns integer.
一个常见错误是混淆 DIV 与 /。如果题目询问 7 / 2 的整数结果,你必须判断伪代码想要的是实数除法还是整数除法。在许多爱德思题目中,/ 返回实数,而 DIV 返回整数。
Another pitfall is forgetting that = in an IF condition is a comparison, not assignment. Writing IF x = 5 THEN does not set x to 5; it checks whether x currently equals 5. This distinction is often tested in trace-table questions.
另一个陷阱是忘记 IF 条件中的 = 是比较,而不是赋值。写 IF x = 5 THEN 并不会把 x 设为 5;它检查 x 当前是否等于 5。这一区别经常在跟踪表题中考查。
Be aware of operator precedence errors. For example, NOT A AND B is normally interpreted as (NOT A) AND B rather than NOT (A AND B). Always use parentheses when there is any risk of ambiguity.
注意运算符优先级错误。例如,NOT A AND B 通常被解释为 (NOT A) AND B 而不是 NOT (A AND B)。当存在任何歧义风险时,务必使用括号。
10. Practice Questions and Model Answers | 练习题与模型答案
Question 1: Given the following pseudocode, what are the final values of a and b?
问题1:给定以下伪代码,a 和 b 的最终值是什么?
a ← 10
b ← 3
a ← a DIV b
b ← a MOD 4
a ← a + b
Model answer: a DIV b evaluates to 10 DIV 3 = 3, so a = 3. Then a MOD 4 evaluates to 3 MOD 4 = 3, so b = 3. Finally a + b = 3 + 3 = 6, so a = 6, b = 3.
模型答案:a DIV b 计算为 10 DIV 3 = 3,所以 a = 3。接着 a MOD 4 计算为 3 MOD 4 = 3,所以 b = 3。最后 a + b = 3 + 3 = 6,因此 a = 6,b = 3。
Question 2: Construct a trace table for the following loop and state the output.
问题2:为以下循环构建跟踪表,并说明输出结果。
count ← 1
WHILE count ≤ 4
OUTPUT count * 3
count ← count + 1
ENDWHILE
Model answer: The loop runs for count = 1, 2, 3, 4. The outputs are 3, 6, 9, 12 in that order. The trace table should show count taking values 1, 2, 3, 4, 5 before the condition fails.
模型答案:循环在 count = 1, 2, 3, 4 时运行。输出依次为 3, 6, 9, 12。跟踪表应显示 count 依次取值 1、2、3、4、5,然后条件不再满足。
Question 3: Write a Boolean expression using AND and OR that is true when age is between 18 and 65 inclusive, or when student is true.
问题3:用 AND 和 OR 写出一个布尔表达式,当 age 在 18 到 65 之间(含)或者 student 为真时,该表达式为真。
Model answer: (age ≥ 18 AND age ≤ 65) OR student = TRUE
模型答案:(age ≥ 18 AND age ≤ 65) OR student = TRUE
Published by TutorHao | Programming Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导