📚 Operators in Programming | 编程中的操作符
Operators are special symbols that carry out computations on values and variables. They form the backbone of any program’s logic, allowing you to perform arithmetic, compare data, assign results, and combine Boolean conditions. In A-Level Computer Science, understanding the full range of operators and their precedence is essential for writing correct, efficient code and for interpreting pseudocode in exam questions. This article covers the main categories of operators found in modern programming languages, with examples in Python, Java, and pseudocode, aligning closely with the Edexcel specification.
操作符是对值和变量执行计算的特殊符号。它们构成了任何程序逻辑的基础,让你能够执行算术运算、比较数据、赋值结果以及组合布尔条件。在 A-Level 计算机科学中,理解所有类型的操作符及其优先级对于编写正确、高效的代码以及解读考试中的伪代码至关重要。本文涵盖了现代编程语言中主要类别的操作符,并以 Python、Java 和伪代码举例,紧密结合 Edexcel 规范。
1. Arithmetic Operators | 算术操作符
Arithmetic operators handle basic mathematical calculations. Most languages support addition (+), subtraction (-), multiplication (*), and division (/). Many also include integer division (// in Python, / in some contexts), modulus (%), and exponentiation (**). When used in expressions, these operators follow the standard mathematical order of operations (BIDMAS/BODMAS). In exam pseudocode, you may see DIV for integer division and MOD for modulus; always read the accompanying guide to clarify syntax.
算术操作符处理基本的数学计算。大多数语言支持加法(+)、减法(-)、乘法(*)和除法(/)。许多语言还包括整数除法(Python 中的 //)、取模(%)和乘方(**)。在表达式中使用时,这些操作符遵循标准的数学运算顺序(即先乘除后加减等规则)。在考试伪代码中,你可能会看到用 DIV 表示整数除法,用 MOD 表示取模;务必阅读附带的语法指南来明确写法。
sum = a + b product = a * b quotient = a / b remainder = a MOD b
Integer division discards the fractional part, while modulus returns the remainder after division. These are invaluable for tasks ranging from digit extraction to determining divisibility.
整数除法会丢弃小数部分,而取模返回除法后的余数。它们在从提取数字到判断整除性的各种任务中都十分有用。
2. Comparison (Relational) Operators | 比较(关系)操作符
Comparison operators evaluate the relationship between two values and return a Boolean result (true or false). The standard set includes: equal to (==), not equal to (!= or <>), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). Some languages, like JavaScript, also offer strict equality operators (=== and !==) that check type as well as value. For A-Level pseudocode, the symbol = is often used for both assignment and equality, relying on context to distinguish them. However, in Python and Java, == tests equality while = is assignment.
比较操作符评估两个值之间的关系,并返回布尔结果(真或假)。标准集合包括:等于(==)、不等于(!= 或 <>)、大于(>)、小于(<)、大于或等于(>=)以及小于或等于(<=)。一些语言(如 JavaScript)还提供了严格相等操作符(=== 和 !==),同时检查类型和值。在 A-Level 伪代码中,符号 = 通常既用于赋值也用于相等性比较,需要根据上下文来区分。而在 Python 和 Java 中,== 用于测试相等性,= 用于赋值。
if score >= 90: grade = ‘A’
These operators are crucial in selection (if statements) and iteration (while, for loops) because the condition evaluated must produce a Boolean value.
这些操作符在选择结构(if 语句)和迭代结构(while、for 循环)中至关重要,因为被评估的条件必须产生一个布尔值。
3. Logical (Boolean) Operators | 逻辑(布尔)操作符
Logical operators combine Boolean expressions to form more complex conditions. The three fundamental logical operators are AND, OR, and NOT. In many languages, these are represented as &&, ||, and ! (C, Java, JavaScript), while Python uses the words and, or, not directly. Truth tables define their behavior:
逻辑操作符将布尔表达式组合为更复杂的条件。三个基本的逻辑操作符是 AND、OR 和 NOT。在许多语言中,它们表示为 &&、|| 和 !(C、Java、JavaScript),而 Python 直接使用单词 and、or、not。真值表定义了它们的行为:
| 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 |
Short-circuit evaluation is an important optimization: in an AND expression, if the first operand is false, the second is not evaluated; in an OR expression, if the first operand is true, the second is skipped. This can affect code where the second operand has side effects.
短路求值是一个重要的优化:在 AND 表达式中,如果第一个操作数为假,则不会计算第二个操作数;在 OR 表达式中,如果第一个操作数为真,则会跳过第二个操作数。这可能会影响那些第二个操作数具有副作用的代码。
4. Assignment Operators | 赋值操作符
The basic assignment operator is = (or := in some pseudocode). It stores the value on the right-hand side into the variable on the left. In addition, compound assignment operators combine an arithmetic or bitwise operation with assignment: +=, -=, *=, /=, %=, etc. For example, x += 5 is equivalent to x = x + 5. These operators make code more concise and can be slightly more efficient because the variable is evaluated only once.
基本的赋值操作符是 =(或某些伪代码中的 :=)。它将右侧的值存储到左侧的变量中。此外,复合赋值操作符将算术或位运算与赋值结合起来:+=、-=、*=、/=、%= 等。例如,x += 5 等价于 x = x + 5。这些操作符使代码更简洁,并且可能稍微更高效,因为变量只被评估一次。
total = 0 total += price * quantity
Understanding assignment is critical, especially for A-Level tracing questions where you must track how a variable’s value changes across a loop or function call.
理解赋值至关重要,尤其是在 A-Level 的追踪类题目中,你必须跟踪变量在循环或函数调用过程中值的变化。
5. Increment and Decrement Operators | 自增与自减操作符
In languages such as C, Java, and C++, ++ and — operators increase or decrease a variable by 1. They can be used in prefix (++x) or postfix (x++) form. Prefix increments the variable before its value is used in the expression; postfix uses the current value and then increments. Python does not have these operators; you must use x += 1 or x = x + 1. Edexcel pseudocode may include them depending on the variant, so be prepared to interpret both styles.
在 C、Java 和 C++ 等语言中,++ 和 — 操作符将变量增加或减少 1。它们可以以前缀(++x)或后缀(x++)形式使用。前缀形式在使用变量的值之前递增;后缀形式先使用当前值再递增。Python 没有这些操作符;你必须使用 x += 1 或 x = x + 1。Edexcel 伪代码可能包含它们,具体取决于变体,因此要准备好解读两种风格。
count++ // postfix increment –index // prefix decrement
Tracing code with these operators can be tricky; always note whether the operation happens before or after the value is taken.
追踪包含这些操作符的代码可能会很棘手;务必注意操作是发生在取值之前还是之后。
6. Bitwise Operators | 位操作符
Bitwise operators act on individual bits of integer values. They are essential in low-level programming, cryptography, and graphics. The common bitwise operators are: AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). XOR is particularly useful for toggling bits and simple encryption. Shifts effectively multiply or divide by powers of two. Python also supports these operators, and they work on the binary representation of integers.
位操作符作用于整数值的各个位。它们在低级编程、密码学和图形处理中必不可少。常见的位操作符有:AND (&)、OR (|)、XOR (^)、NOT (~)、左移 (<<) 和右移 (>>)。XOR 在翻转位和简单加密中特别有用。移位实际上是对 2 的幂进行乘或除。Python 也支持这些操作符,它们作用于整数的二进制表示。
result = flags & MASK combined = bits | MORE_BITS shift_left = value << 2
Exam questions may ask you to evaluate expressions containing bitwise operators or to understand their role in masking, extracting, or setting specific bits.
考题可能要求你计算包含位操作符的表达式,或理解它们在掩码、提取或设置特定位中的作用。
7. Operator Precedence | 操作符优先级
When multiple operators appear in an expression, operator precedence determines the order of evaluation. The general hierarchy (from highest to lowest) is: parentheses; unary operators (like NOT, -); multiplicative (*, /, MOD, DIV); additive (+, -); relational (<, >, <=, >=); equality (==, !=); logical AND; logical OR; assignment. Most languages follow this pattern, but subtle differences exist. Always use parentheses to make the intended order explicit and avoid bugs.
当表达式中出现多个操作符时,操作符优先级决定了求值顺序。一般的层次结构(从高到低)是:括号;一元操作符(如 NOT、-);乘除类(*、/、MOD、DIV);加减类(+、-);关系类(<、>、<=、>=);相等类(==、!=);逻辑 AND;逻辑 OR;赋值。多数语言遵循此模式,但存在细微差异。务必使用括号来明确预期顺序,避免错误。
if (year % 4 == 0) AND (year % 100 != 0) OR (year % 400 == 0): leap = True
In the above, the AND is evaluated before the OR if precedence rules apply; adding parentheses around the OR part clarifies logic. Edexcel pseudocode often expects you to apply precedence correctly when evaluating expressions.
以上代码中,如果优先级规则适用,AND 会在 OR 之前计算;给 OR 部分加上括号可以明确逻辑。Edexcel 伪代码通常要求你在计算表达式时正确应用优先级。
8. String Operators | 字符串操作符
Strings in many languages support concatenation using the + operator and repetition using the * operator. Concatenating two strings joins them end-to-end; repeating a string n times with * duplicates it. These operators do not modify the original strings (strings are immutable in many languages), but return new string objects. Some languages also provide comparison operators for strings based on lexicographical order.
许多语言中的字符串支持使用 + 操作符进行连接,以及使用 * 操作符进行重复。连接两个字符串会将它们首尾相接;用 * 将字符串重复 n 次会复制它。这些操作符不会修改原始字符串(在许多语言中字符串是不可变的),而是返回新的字符串对象。一些语言还提供了基于字典顺序的字符串比较操作符。
full_name = first + ‘ ‘ + last border = ‘-‘ * 20
In Edexcel pseudocode, string concatenation is often written with the & symbol or simply by juxtaposition; check the accompanying guide. Understanding string manipulation is vital for tasks like formatting output or constructing messages.
在 Edexcel 伪代码中,字符串连接通常用 & 符号或直接并列书写;请查阅配套指南。理解字符串操作对于格式化输出或构造消息等任务至关重要。
9. Membership and Identity Operators (Python Specific) | 成员与身份操作符(Python 特有)
Python includes two additional categories that appear in many A-Level examples: membership operators (in, not in) test whether a value is present in a sequence (string, list, tuple, etc.). Identity operators (is, is not) check whether two variables refer to the same object in memory, not just equal values. While these are not universal, they illustrate higher-order operations and are frequently used in algorithm implementations.
Python 包含两个额外的类别,经常出现在许多 A-Level 示例中:成员操作符(in、not in)用于测试某个值是否存在于序列(字符串、列表、元组等)中。身份操作符(is、is not)检查两个变量是否引用内存中的同一个对象,而不仅仅是值相等。虽然这些并非所有语言通用,但它们展示了更高阶的操作,并在算法实现中频繁使用。
if target in data: … if a is None: …
When studying Python pseudocode in Edexcel, knowing these operators helps you read and write concise list-processing algorithms, such as searching for an element or checking if a node reference is null.
在学习 Edexcel 的 Python 伪代码时,了解这些操作符有助于你读写简洁的列表处理算法,例如搜索元素或检查节点引用是否为空。
10. Practical Usage and Exam Tips | 实际应用与应试技巧
In programming exams, you will often need to construct expressions that use a mix of operators. A common mistake is confusing = with ==. Remember: = assigns a value; == checks equality. When building compound conditions, test boundary cases. For example, if checking a valid range like 0 < value < 100, some languages require writing (value > 0) && (value < 100) explicitly; Python allows chaining: 0 < value < 100. Also, be mindful of integer division vs. float division: in Python, / always gives a float, while // gives an integer floor division.
在编程考试中,你经常需要构建混合使用多种操作符的表达式。常见的错误是混淆 = 与 ==。记住:= 是赋值;== 是检查相等性。在构建复合条件时,要测试边界情况。例如,若要检查 0 < value < 100 这样的有效范围,某些语言要求显式写作 (value > 0) && (value < 100);Python 允许链式表达:0 < value < 100。此外,注意整数除法与浮点除法:在 Python 中,/ 总是得到浮点数,而 // 进行整数向下取整除法。
if (age >= 18) AND (hasLicense == True): canDrive = True
Writing small practice programs that test each operator category will build confidence. In exam trace-table questions, proceed step-by-step and update all relevant variables after each line; do not skip operations due to assumed precedence – confirm using rules or parentheses.
编写一些测试每个操作符类别的小型练习程序会增强信心。在考试的手工执行表(trace table)问题中,要逐步进行,并在每一行后更新所有相关变量;不要因为假定的优先级而跳过操作——应使用规则或括号来确认。
Finally, always read the question’s provided pseudocode guide carefully: the precise symbols for integer division, exponentiation, or inequality may vary between exam series.
最后,务必仔细阅读题目中提供的伪代码指南:整数除法、乘方或不等号的具体符号在不同的考试季中可能有所不同。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导