Operators, Precedence and Selection in Programming | 编程中的运算符、优先级与选择结构

📚 Operators, Precedence and Selection in Programming | 编程中的运算符、优先级与选择结构

In A-Level Computer Science, mastering operators, precedence and selection is essential for writing reliable, exam-ready programs. These building blocks control how expressions are evaluated and how decisions branch in code, especially in Edexcel Paper 2 and the non-exam assessment.

在 A-Level 计算机科学中,掌握运算符、优先级与选择结构是编写可靠、符合考试要求程序的关键。这些基础模块决定了表达式如何求值以及代码如何分支,尤其体现在 Edexcel 试卷二与非考试评估中。


1. Why Programming Fundamentals Matter | 为什么编程基础重要

Edexcel A-Level Computer Science tests programming through theory questions, tracing tasks and practical coding. A clear understanding of operators and selection helps you predict output, find logic errors and write concise algorithms.

Edexcel A-Level 计算机科学通过理论题、代码追踪题和实操编程来考查编程能力。清晰理解运算符和选择结构有助于你预测输出、发现逻辑错误并编写简洁的算法。

Operators appear in almost every expression, while selection is one of the three core programming constructs alongside sequence and iteration. Examiners expect precise use of symbols and correct control flow.

运算符几乎出现在每一个表达式中,而选择结构是与顺序和迭代并列的三大核心编程结构之一。考官要求使用准确的符号和正确的控制流程。


2. Operators in Programming | 编程中的运算符

An operator is a symbol that performs a computation on one or more operands. Operators are classified by the number of operands and by their function: arithmetic, relational, logical or bitwise.

运算符是对一个或多个操作数执行计算的符号。运算符可以按操作数的数量以及功能分类:算术、关系、逻辑或按位运算符。

At A-Level, you must be able to evaluate expressions using the correct operator and understand the data type returned. For example, arithmetic operators produce numeric results, while relational operators produce Boolean results.

在 A-Level 中,你必须能够使用正确的运算符对表达式求值,并理解返回的数据类型。例如,算术运算符产生数值结果,而关系运算符产生布尔结果。


3. Arithmetic Operators | 算术运算符

Arithmetic operators perform mathematical calculations. The core symbols are addition +, subtraction −, multiplication ×, division ÷, integer division and modulo subtraction.

算术运算符执行数学计算。核心符号包括加法 +、减法 −、乘法 ×、除法 ÷、整数除法和求余。

  • + adds two values: 3 + 5 = 8 | 加法:3 + 5 = 8
  • − subtracts the right operand from the left: 9 − 4 = 5 | 减法:9 − 4 = 5
  • × multiplies two values: 6 × 7 = 42 | 乘法:6 × 7 = 42
  • ÷ divides the left operand by the right: 15 ÷ 3 = 5 | 除法:15 ÷ 3 = 5
  • MOD returns the remainder after division: 17 MOD 5 = 2 | 求余:17 MOD 5 = 2
  • DIV performs integer division: 17 DIV 5 = 3 | 整数除法:17 DIV 5 = 3

Different languages use different symbols: Python uses // for integer division and % for remainder, while C-family languages use / and % . You must know the syntax required by the exam pseudocode.

不同语言使用不同符号:Python 使用 // 表示整数除法、% 表示求余,而 C 系语言使用 / 和 %。你必须了解考试伪代码所需的语法。


4. Relational and Boolean Operators | 关系与布尔运算符

Relational operators compare two values and return either TRUE or FALSE. They are essential in selection conditions because the IF statement only executes when the condition evaluates to TRUE.

关系运算符比较两个值并返回 TRUE 或 FALSE。它们在选择条件中至关重要,因为 IF 语句只有在条件为 TRUE 时才会执行。

Operator Meaning Example
== equal to x == 5
!= not equal to x != 5
> greater than x > 5
< less than x < 5
>= greater than or equal x >= 5
<= less than or equal x <= 5

Boolean operators combine multiple conditions: AND returns TRUE only when both sides are TRUE, OR returns TRUE when at least one side is TRUE, and NOT reverses the truth value.

布尔运算符组合多个条件:AND 仅在两侧都为 TRUE 时返回 TRUE,OR 在至少一侧为 TRUE 时返回 TRUE,NOT 反转真值。


5. Operator Precedence | 运算符优先级

Precedence determines the order in which operations are evaluated in a complex expression. Without a clear rule, expressions become ambiguous and produce different results.

优先级决定了复杂表达式中运算的计算顺序。如果没有明确规则,表达式会产生歧义并得到不同结果。

3 + 4 × 2 = 11

The multiplication is evaluated before the addition, so 4 × 2 gives 8, then 3 + 8 gives 11. If evaluated left-to-right, the result would incorrectly be 14.

乘法先于加法计算,因此 4 × 2 得到 8,然后 3 + 8 得到 11。如果从左到右计算,结果会错误地变成 14。

A common precedence order from highest to lowest is: parentheses, exponentiation, multiplication/division, addition/subtraction, relational operators, NOT, AND, OR.

常见的优先级从高到低为:括号、幂运算、乘法/除法、加法/减法、关系运算符、NOT、AND、OR。

You can always use brackets to force a specific evaluation order and make code easier to read, even when brackets are not strictly required.

你始终可以使用括号来强制指定计算顺序,即使括号并非严格必需,这也能使代码更易读。


6. Selection with IF Statements | IF 语句选择结构

Selection allows a program to make decisions and follow one path when a condition is true, otherwise another path. The simplest form is a single IF statement.

选择结构允许程序做出决策,当条件为真时执行一条路径,否则执行另一条路径。最简单的形式是单个 IF 语句。

IF age >= 18 THEN
OUTPUT “Adult”
END IF

The condition age >= 18 is evaluated first. If it is TRUE, the indented block runs; if FALSE, the block is skipped. This is a key part of Edexcel pseudocode tracing.

首先计算条件 age >= 18。如果为 TRUE,则运行缩进块;如果为 FALSE,则跳过该块。这是 Edexcel 伪代码追踪的关键部分。


7. ELSE and ELSE IF | ELSE 与 ELSE IF

ELSE provides an alternative path when the IF condition is FALSE. ELSE IF allows you to check a second condition if the first one fails, creating multiple branches.

ELSE 在 IF 条件为 FALSE 时提供替代路径。ELSE IF 允许在第一个条件不成立时检查第二个条件,从而创建多个分支。

IF score >= 90 THEN
OUTPUT “A”
ELSE IF score >= 80 THEN
OUTPUT “B”
ELSE
OUTPUT “C”
END IF

Only one branch executes because the conditions are evaluated in order. Once a condition is TRUE, the remaining ELSE IF and ELSE branches are ignored.

只有一个分支会执行,因为条件是按顺序计算的。一旦某个条件为 TRUE,其余的 ELSE IF 和 ELSE 分支都会被忽略。


8. Nested Selection and Logical Combination | 嵌套选择与逻辑组合

Nesting means placing one IF statement inside another. This works but can become difficult to trace. Logical operators often provide a cleaner alternative.

嵌套是指在一个 IF 语句内部放置另一个 IF 语句。这种方法可行,但追踪起来较困难。逻辑运算符通常能提供更清晰的替代方案。

IF age >= 18 AND age < 65 THEN
OUTPUT “Working age”
END IF

The logical AND combines two relational conditions, so you do not need two separate IF blocks. This reduces nesting depth and makes the intention explicit.

逻辑 AND 将两个关系条件组合在一起,因此你不需要两个独立的 IF 块。这降低了嵌套深度并使意图更加明确。


9. Switch / Case and Multiway Branching | Switch/Case 与多路分支

Some languages provide SWITCH or CASE for multiway branching when you compare the same variable against several constant values. It can be clearer than long chains of ELSE IF.

某些语言提供 SWITCH 或 CASE 用于多路分支,当你将同一个变量与多个常量值比较时,它比一长串 ELSE IF 更清晰。

SWITCH day
CASE 1: OUTPUT “Monday”
CASE 2: OUTPUT “Tuesday”
CASE 3: OUTPUT “Wednesday”
DEFAULT: OUTPUT “Other day”
END SWITCH

Python 3.10+ uses match-case for the same purpose. In pseudocode, Edexcel accepts both IF/ELSE and CASE structures as long as the logic is correct.

Python 3.10+ 使用 match-case 实现相同功能。在伪代码中,只要逻辑正确,Edexcel 接受 IF/ELSE 和 CASE 两种结构。


10. Tracing and Desk Checking | 追踪与桌面检查

Exam questions frequently ask you to complete a trace table. A trace table records the value of each variable and the outcome of each condition line by line.

考试题目经常要求你填写追踪表。追踪表逐行记录每个变量的值以及每个条件的结果。

For example, given x = 6, y = 2, the condition x / y > 2 AND y != 0 is evaluated as: x / y = 3, so 3 > 2 is TRUE; y != 0 is TRUE; TRUE AND TRUE gives TRUE.

例如,给定 x = 6,y = 2,条件 x / y > 2 AND y != 0 的计算过程为:x / y = 3,因此 3 > 2 为 TRUE;y != 0 为 TRUE;TRUE AND TRUE 得到 TRUE。

Update the trace table after every statement, not just at the end. This helps you spot when a variable changes and when a branch is taken or skipped.

在每一条语句之后更新追踪表,而不仅仅是在结束之后。这有助于你发现变量何时发生变化以及何时进入或跳过某个分支。


11. Common Pitfalls and Tips | 常见易错点与提示

One common error is using = instead of == in a condition. In most languages, = is assignment while == is comparison. Mixing them changes the logic or causes a syntax error.

一个常见错误是在条件中使用 = 而不是 ==。在大多数语言中,= 表示赋值,而 == 表示比较。混用会改变逻辑或导致语法错误。

Another pitfall is forgetting precedence in expressions such as 10 − 2 × 3. Many students incorrectly subtract first and get 24, but multiplication gives 10 − 6 = 4.

另一个易错点是忘记表达式中的优先级,例如 10 − 2 × 3。许多学生错误地先做减法得到 24,但乘法运算应为 10 − 6 = 4。

Avoid comparing floating-point values for exact equality, because 0.1 + 0.2 may not equal 0.3 exactly. Use a tolerance or compare ranges instead.

避免对浮点值进行完全相等比较,因为 0.1 + 0.2 可能并不精确等于 0.3。应使用容差或范围比较。

Always test boundary values such as age = 18 in an adult check. Boundary testing reveals off-by-one mistakes in conditions like age > 18 versus age >= 18.

始终测试边界值,例如在成年检查中测试 age = 18。边界测试可以发现 age > 18 与 age >= 18 这类条件中的差一错误。


12. Edexcel-Style Application | 爱德思风格应用

Consider this pseudocode: x = 10, y = 4, z = 6. IF x + y > z AND z − y > 1 THEN OUTPUT “Pass” ELSE OUTPUT “Fail”.

考虑以下伪代码:x = 10,y = 4,z = 6。IF x + y > z AND z − y > 1 THEN 输出 “Pass” ELSE 输出 “Fail”。

Evaluate: x + y = 14, so 14 > 6 is TRUE. z − y = 2, so 2 > 1 is TRUE. TRUE AND TRUE gives TRUE, so “Pass” is output.

计算过程:x + y = 14,因此 14 > 6 为 TRUE。z − y = 2,因此 2 > 1 为 TRUE。TRUE AND TRUE 得到 TRUE,因此输出 “Pass”。

Practise writing your own trace tables for similar questions. Use brackets to clarify complex logical expressions and always show your working in exam answers.

练习为类似问题编写你自己的追踪表。使用括号来明确复杂的逻辑表达式,并在考试答案中始终展示你的计算过程。

Published by TutorHao | Computer Science Revision Series | aleveler.com

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

Comments

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

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

Exit mobile version