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

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

In Edexcel A-Level Computer Science, operators and expressions form the foundation of programming logic. Without a solid understanding of how operators combine values, students cannot build algorithms, evaluate conditions, or manipulate data effectively.

在 Edexcel A-Level 计算机科学中,运算符与表达式是编程逻辑的基础。如果没有扎实掌握运算符如何组合数值,学生就无法有效地构建算法、判断条件或操作数据。


1. Overview of Operators and Expressions | 运算符与表达式概述

An operator is a symbol that tells the computer to perform a specific mathematical, relational or logical action. An expression is a combination of values, variables and operators that can be evaluated to produce a result.

运算符是一种符号,它告诉计算机执行特定的数学、关系或逻辑操作。表达式是值、变量和运算符的组合,可以被求值并产生结果。

In Edexcel pseudocode and Python, expressions such as (x + y) * 2 produce a single value after evaluation. The operands are the data items, while the operators define the action.

在 Edexcel 伪代码和 Python 中,诸如 (x + y) * 2 这样的表达式在求值后产生一个单一的值。操作数是数据项,而运算符定义了操作。


2. Arithmetic Operators | 算术运算符

Arithmetic operators perform basic calculations. The standard set includes + (addition), – (subtraction), * (multiplication), / (division), % (modulus), ** (exponentiation) and // (integer division) in Python.

算术运算符执行基本计算。标准集合包括 +(加法)、-(减法)、*(乘法)、/(除法)、%(取模)、**(幂)以及 Python 中的 //(整除)。

The modulus operator returns the remainder of a division, so 17 % 5 evaluates to 2. Integer division discards the fractional part, so 17 // 5 gives 3.

取模运算符返回除法的余数,因此 17 % 5 的结果是 2。整除运算舍弃小数部分,所以 17 // 5 得到 3。

In pseudocode, the same operations may be written as MOD and DIV, for example 17 MOD 5 = 2 and 17 DIV 5 = 3. Examiners accept both notations.

在伪代码中,相同的操作可能写作 MOD 和 DIV,例如 17 MOD 5 = 2,17 DIV 5 = 3。考官接受这两种表示法。


3. Comparison (Relational) Operators | 比较(关系)运算符

Comparison operators compare two values and return a Boolean result: true or false. Common operators are == (equal to), != (not equal to), >, <, >= and <=.

比较运算符比较两个值并返回布尔结果:真或假。常见运算符有 ==(等于)、!=(不等于)、>、<、>= 和 <=。

For example, the expression age >= 18 checks whether the variable age is at least 18. It is important not to confuse = (assignment) with == (equality check).

例如,表达式 age >= 18 检查变量 age 是否至少为 18。重要的是不要将 =(赋值)与 ==(相等性检查)混淆。

In Edexcel pseudocode, comparison may use = for equality and <> or != for not equal, depending on the context. Always follow the specification’s notation.

在 Edexcel 伪代码中,比较可能使用 = 表示相等,使用 <> 或 != 表示不相等,具体取决于上下文。务必遵循规范中的符号。


4. Logical Operators | 逻辑运算符

Logical operators combine Boolean expressions. The three fundamental ones are AND, OR and NOT. In Python they are written as and, or, not.

逻辑运算符组合布尔表达式。三个基本运算符是 AND、OR 和 NOT。在 Python 中写作 and、or、not。

The AND operator returns true only when both operands are true. For instance, (score >= 50) AND (score <= 60) is true for score equal to 55.

AND 运算符只有在两个操作数都为真时才返回真。例如,(score >= 50) AND (score <= 60) 在 score 等于 55 时为真。

The OR operator returns true when at least one operand is true. The NOT operator inverts the truth value, so NOT (x > 5) is true when x is 5 or less.

OR 运算符在至少一个操作数为真时返回真。NOT 运算符反转真值,因此当 x 小于或等于 5 时,NOT (x > 5) 为真。


5. Bitwise Operators | 位运算符

Bitwise operators act on binary representations of integers. They are less common in A-Level practical work but appear in theory topics such as data representation.

位运算符作用于整数的二进制表示。它们在 A-Level 实践工作中不常见,但在数据表示等理论主题中出现。

The operators include & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift) and >> (right shift). For example, 12 & 10 gives 8 because 1100 AND 1010 = 1000.

这些运算符包括 &(与)、|(或)、^(异或)、~(非)、<<(左移)和 >>(右移)。例如,12 & 10 得到 8,因为 1100 与 1010 相与等于 1000。

A left shift by n places multiplies the number by 2 to the power n, while a right shift performs integer division by 2 to the power n.

左移 n 位会将数字乘以 2 的 n 次方,而右移会执行除以 2 的 n 次方的整数除法。


6. String Concatenation and Membership Operators | 字符串连接与成员运算符

For strings, the + operator concatenates two strings end to end. For example, “Tutor” + “Hao” produces “TutorHao”. The * operator can repeat a string, so “A” * 3 gives “AAA”.

对于字符串,+ 运算符将两个字符串首尾相连。例如,”Tutor” + “Hao” 生成 “TutorHao”。* 运算符可以重复字符串,因此 “A” * 3 得到 “AAA”。

Membership operators test whether a substring or element appears in a sequence. In Python, in and not in return a Boolean result, such as “a” in “TutorHao” evaluating to true.

成员运算符测试子串或元素是否出现在序列中。在 Python 中,in 和 not in 返回布尔结果,例如 “a” in “TutorHao” 的值为真。

In pseudocode, membership may be expressed with words like CONTAINS or by using a linear search algorithm, but the concept remains the same.

在伪代码中,成员关系可以用 CONTAINS 等词或通过线性搜索算法来表示,但概念是相同的。


7. Compound Assignment Operators | 复合赋值运算符

Compound assignment operators combine an arithmetic or bitwise operation with assignment. Examples include +=, -=, *=, /=, %=, //= and **= in Python.

复合赋值运算符将算术或位运算与赋值结合起来。Python 中的示例包括 +=、-=、*=、/=、%=、//= 和 **=。

The statement x += 5 is shorthand for x = x + 5. These operators make code more concise and reduce the risk of typing the variable name twice.

语句 x += 5 是 x = x + 5 的简写。这些运算符使代码更简洁,并减少两次输入变量名的风险。

In Edexcel pseudocode, such shorthand may not be explicitly required, but recognising it helps when reading algorithm questions.

在 Edexcel 伪代码中,未必明确要求这种简写,但识别它有助于阅读算法题。


8. Operator Precedence and Associativity | 运算符优先级与结合性

Operator precedence determines the order in which different operators are evaluated in an expression. Generally, parentheses have the highest precedence, followed by exponentiation, multiplication/division, addition/subtraction, comparison and finally logical operators.

运算符优先级决定了表达式中不同运算符的计算顺序。一般来说,括号优先级最高,其次是幂、乘除、加减、比较,最后是逻辑运算符。

For example, 2 + 3 * 4 equals 14, not 20, because multiplication happens before addition. The expression (2 + 3) * 4 yields 20.

例如,2 + 3 * 4 等于 14,而不是 20,因为乘法先于加法。表达式 (2 + 3) * 4 则得到 20。

Associativity determines the evaluation order for operators of the same precedence. Most arithmetic operators are left-associative, so 20 // 5 // 2 is (20 // 5) // 2 = 2, whereas exponentiation is right-associative.

结合性决定了相同优先级运算符的计算顺序。大多数算术运算符是左结合的,因此 20 // 5 // 2 是 (20 // 5) // 2 = 2,而幂运算是右结合的。


9. Evaluating Expressions and Type Conversion | 表达式求值与类型转换

When evaluating an expression, the computer must convert operands to compatible types. Implicit type conversion (coercion) may occur, such as adding an integer and a float producing a float.

在计算表达式时,计算机必须将操作数转换为兼容的类型。可能会发生隐式类型转换(强制转换),例如整数与浮点数相加得到浮点数。

Explicit type conversion uses functions like int(), float() and str() in Python. For example, int(“42”) converts the string “42” to the integer 42.

显式类型转换使用 Python 中的 int()、float() 和 str() 等函数。例如,int(“42”) 将字符串 “42” 转换为整数 42。

A common mistake is attempting to concatenate a string and a number with + without conversion, which causes a TypeError. Always convert types explicitly when mixing data.

一个常见错误是尝试在没有转换的情况下用 + 连接字符串和数字,这会导致 TypeError。混合数据时务必显式转换类型。


10. Common Exam Mistakes and Tips | 考试常见错误与技巧

Many candidates confuse assignment (=) with equality (==) in conditions, writing if x = 5 instead of if x == 5. This is a syntax error in Python and a logic error in pseudocode.

许多考生在条件中将赋值(=)与相等(==)混淆,写出 if x = 5 而不是 if x == 5。这在 Python 中是语法错误,在伪代码中是逻辑错误。

Another error is ignoring operator precedence, leading to unexpected results. When in doubt, use parentheses to make the intended order explicit.

另一个错误是忽略运算符优先级,导致意外结果。如有疑问,使用括号明确所需顺序。

Practice tracing expressions for each operator type, especially integer division and modulus, because these often appear in programming and algorithm questions.

练习追踪每种运算符类型的表达式,尤其是整除和取模,因为它们经常出现在编程和算法题中。


Published by TutorHao | Programming 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