Operators in Programming | 编程运算符

📚 Operators in Programming | 编程运算符

Operators are fundamental building blocks in any programming language. They allow you to perform calculations, compare values, combine conditions, and manipulate data at the bit level. A-Level Edexcel Computer Science expects you to understand both the syntax and semantics of common operators, and how to use them effectively within expressions.

运算符是任何编程语言中的基本构建块。它们允许你执行计算、比较数值、组合条件并在位级别操作数据。A-Level Edexcel 计算机科学要求你理解常见运算符的语法和语义,以及如何有效地在表达式中使用它们。


1. Introduction to Operators | 运算符简介

An operator is a symbol that tells the compiler or interpreter to perform specific mathematical, relational or logical manipulations. For instance, the plus sign (+) is an arithmetic operator that adds two operands. An expression such as a + b consists of the operator + and two operands, a and b.

运算符是一个符号,告诉编译器或解释器执行特定的数学、关系或逻辑运算。例如,加号(+)是一个算术运算符,用于将两个操作数相加。表达式 a + b 由运算符 + 和两个操作数 a、b 组成。

Understanding the different categories of operators and how they interact is essential for writing correct and efficient programs. In A-Level Edexcel pseudocode, operators follow a well-defined set of rules that mirror those found in high-level languages like Python or Java.

理解不同类别的运算符以及它们如何相互作用,对于编写正确高效的程序至关重要。在 A-Level Edexcel 伪代码中,运算符遵循一套明确定义的规则,这些规则与 Python 或 Java 等高级语言的规则相似。


2. Arithmetic Operators | 算术运算符

Arithmetic operators are used to carry out mathematical operations on numeric operands. The most common ones are addition (+), subtraction (-), multiplication (*) and division (/). In Edexcel pseudocode, integer division and modulus are expressed using DIV and MOD respectively.

算术运算符用于对数值操作数执行数学运算。最常见的包括加法(+)、减法(-)、乘法(*)和除法(/)。在 Edexcel 伪代码中,整数除法和取模分别使用 DIV 和 MOD 表示。

For example, the expression 7 DIV 2 evaluates to 3, because integer division discards the remainder. On the other hand, 7 MOD 2 yields 1, as it returns the remainder of the division. Exponentiation may be represented by the caret symbol (^) in some pseudocode variants.

例如,表达式 7 DIV 2 的值为 3,因为整数除法会丢弃余数。而 7 MOD 2 的结果为 1,因为它返回除法的余数。在某些伪代码变体中,指数运算可能用脱字符号(^)表示。

When an expression contains multiple arithmetic operators, they are evaluated according to standard mathematical rules unless parentheses are used to change the order.

当一个表达式包含多个算术运算符时,除非使用括号改变顺序,否则将按照标准数学规则进行求值。


3. Relational Operators | 关系运算符

Relational operators compare two values and return a Boolean result (TRUE or FALSE). In Edexcel pseudocode, the equal to operator is written as ==, while not equal to is !=. The remaining operators are greater than (>), less than (<), greater than or equal to (>=) and less than or equal to (<=).

关系运算符用于比较两个值并返回布尔结果(TRUE 或 FALSE)。在 Edexcel 伪代码中,等于运算符写作 ==,不等于写作 !=。其余的运算符包括大于(>)、小于(<)、大于等于(>=)和小于等于(<=)。

It is crucial not to confuse the assignment operator (=) with equality (==). Assignment is used to store a value in a variable, whereas equality tests whether two values are the same. A common programming error is writing IF x = 5 THEN instead of IF x == 5 THEN.

关键是要注意不要混淆赋值运算符(=)和相等比较符(==)。赋值用于将值存储到变量中,而相等比较符则测试两个值是否相同。一个常见的编程错误是写成 IF x = 5 THEN 而不是 IF x == 5 THEN。

Relational expressions often serve as conditions in selection (IF) and repetition (WHILE, FOR) structures, controlling the flow of a program.

关系表达式通常用作选择结构(IF)和循环结构(WHILE、FOR)中的条件,从而控制程序的流程。


4. Logical Operators | 逻辑运算符

Logical operators are used to combine or negate Boolean expressions. The three fundamental operators are AND, OR and NOT. They follow the rules of propositional logic and are often employed to express complex conditions.

逻辑运算符用于组合或否定布尔表达式。三个基本运算符是 AND、OR 和 NOT。它们遵循命题逻辑的规则,常用于表达复杂的条件。

The AND operator returns TRUE only if both operands are TRUE. The OR operator returns TRUE if at least one operand is TRUE. The NOT operator toggles the truth value: NOT TRUE is FALSE, and NOT FALSE is TRUE.

AND 运算符仅在两个操作数都为 TRUE 时返回 TRUE。OR 运算符在至少一个操作数为 TRUE 时返回 TRUE。NOT 运算符会翻转真值:NOT TRUE 为 FALSE,NOT FALSE 为 TRUE。

Truth tables can help clarify the behaviour of these operators. For instance, TRUE AND FALSE evaluates to FALSE. In examination questions, you may be asked to simplify a logical expression using identity laws such as De Morgan’s laws.

真值表有助于阐明这些运算符的行为。例如,TRUE AND FALSE 的结果是 FALSE。在考试题中,你可能被要求使用恒等律(如德摩根定律)来简化逻辑表达式。


5. Bitwise Operators | 位运算符

Bitwise operators act on the binary representations of integers. They treat each bit of the operands individually. The most common bitwise operators are AND (&), OR (|), XOR (^), NOT (~), left shift (<<) and right shift (>>).

位运算符作用于整数的二进制表示。它们分别处理操作数的每一个比特位。最常见的位运算符包括按位与(&)、按位或(|)、按位异或(^)、按位取反(~)、左移(<<)和右移(>>)。

Suppose we apply 5 & 3. The binary representation of 5 is 0101, and 3 is 0011. The bitwise AND yields 0001, which is 1 in decimal. In contrast, 5 | 3 produces 0111 (7).

假设我们计算 5 & 3。5 的二进制表示为 0101,3 为 0011。按位与的结果是 0001,即十进制的 1。而 5 | 3 产生 0111(7)。

Left shift (<<) moves all bits to the left by a specified number of places, filling vacancies with zeros. This is equivalent to multiplying by 2 raised to the shift count. Right shift (>>) does the opposite, often performing integer division by powers of two.

左移(<<)将所有位向左移动指定数目的位置,空位补零。这相当于乘以 2 的移动位数次方。右移(>>)则相反,通常执行除以 2 的幂次的整数除法。

Bitwise operators are heavily used in low-level programming, networking and cryptography, and Edexcel specifications expect you to interpret simple bit manipulations.

位运算符广泛用于底层编程、网络和密码学,Edexcel 考纲要求你能够解释简单的位操作。


6. Assignment Operators | 赋值运算符

The basic assignment operator is the equals sign =. It stores the value of the expression on its right into the variable on its left. For example, total = price * quantity assigns the product to total.

基本赋值运算符是等号 =。它将右侧表达式的值存储到左侧变量中。例如,total = price * quantity 将乘积赋给 total。

Many languages offer compound assignment operators that combine arithmetic or bitwise operations with assignment. Examples include +=, -=, *=, /=, &=, and |=.
The statement x += 3 is equivalent to x = x + 3.

许多语言提供了复合赋值运算符,将算术或位运算与赋值结合在一起。例如 +=、-=、*=、/=、&= 和 |=。
语句 x += 3 等价于 x = x + 3。

Using compound assignment can make code more concise. However, it is important to ensure that the variable being updated has already been declared and initialised.

使用复合赋值可以使代码更简洁。但必须确保被更新的变量已经声明并初始化。


7. Increment and Decrement Operators | 自增与自减运算符

In languages like C, C++, Java and JavaScript, the increment (++) and decrement (–) operators add or subtract 1 from a variable. They can appear before (prefix) or after (postfix) the variable, affecting the order of evaluation.

在 C、C++、Java 和 JavaScript 等语言中,自增(++)和自减(–)运算符将变量的值加 1 或减 1。它们可以出现在变量之前(前缀)或之后(后缀),从而影响求值顺序。

In Edexcel pseudocode, these operators may not be explicitly used. Instead, expressions such as counter = counter + 1 are standard. Nevertheless, being aware of prefix vs. postfix semantics enriches your understanding of programming concepts.

在 Edexcel 伪代码中,这些运算符可能不会被显式使用。通常使用 counter = counter + 1 这样的表达式。尽管如此,了解前缀和后缀的语义可以加深你对编程概念的理解。

In prefix form (++x), the variable is incremented before its value is used in the surrounding expression. In postfix form (x++), the original value is used first, and then the increment occurs.

在前缀形式(++x)中,变量先增加,然后才在周围的表达式中使用。在后缀形式(x++)中,先使用原始值,然后才进行自增。


8. Conditional (Ternary) Operator | 条件运算符

The conditional operator, often called the ternary operator, provides a compact way to choose between two values based on a Boolean condition. In C-style syntax it is written as condition ? value_if_true : value_if_false.

条件运算符,通常称为三元运算符,提供了一种根据布尔条件在两个值之间进行选择的简洁方式。在 C 风格语法中,它写作 condition ? value_if_true : value_if_false。

For instance, max = (a > b) ? a : b assigns the larger of a and b to max. While not always present in Edexcel pseudocode, this operator appears in many high-level languages and is a useful tool for writing concise conditional assignments.

例如,max = (a > b) ? a : b 将 a 和 b 中的较大者赋给 max。虽然 Edexcel 伪代码不一定包含该运算符,但它出现在许多高级语言中,是编写简洁条件赋值的有用工具。

The same logic can be expressed with an IF-ELSE structure, but the ternary operator often improves readability when the condition and outcomes are simple.

相同的逻辑可以用 IF-ELSE 结构表达,但当条件及其结果较为简单时,三元运算符通常可以提高可读性。


9. Operator Precedence | 运算符优先级

When multiple operators appear in an expression, evaluation follows a predefined precedence hierarchy. Operators with higher precedence are evaluated before those with lower precedence. For operators on the same level, associativity (left-to-right or right-to-left) determines the order.

当一个表达式中出现多个运算符时,求值遵循预定义的优先级层次。优先级较高的运算符先于优先级较低的运算符进行求值。对于同一优先级的运算符,由结合性(从左到右或从右到左)决定顺序。

The table below summarises the precedence of common operators in descending order. Edexcel examinations often test your ability to predict the outcome of an expression based on these rules.

下表按降序总结了常见运算符的优先级。Edexcel 考试常常测试你根据这些规则预测表达式结果的能力。

Precedence Level Operator(s) Associativity
1 (highest) Parentheses ( ) Left-to-right
2 Unary minus, NOT, ~ Right-to-left
3 *, /, DIV, MOD Left-to-right
4 +, – Left-to-right
5 <<, >> Left-to-right
6 <, <=, >, >= Left-to-right
7 ==, != Left-to-right
8 & (bitwise AND) Left-to-right
9 ^ (bitwise XOR) Left-to-right
10 | (bitwise OR) Left-to-right
11 AND (logical) Left-to-right
12 OR (logical) Left-to-right
13 (lowest) =, +=, -=, etc. Right-to-left

Notice that parentheses are the highest priority and can be used to override the default precedence. For instance, 3 + 4 * 2 equals 11 because multiplication is done first, whereas (3 + 4) * 2 equals 14.

请注意,括号具有最高优先级,可用于覆盖默认优先级。例如,3 + 4 * 2 等于 11,因为乘法先进行;而 (3 + 4) * 2 等于 14。


10. Combining Operators in Expressions | 在表达式中组合运算符

Real-world programs often involve expressions that mix arithmetic, relational and logical operators. Consider the condition: IF score > 80 AND age < 18 THEN. This expression combines a relational operator (>) and (<) with a logical AND.

现实世界的程序经常涉及混合了算术、关系和逻辑运算符的表达式。考虑条件:IF score > 80 AND age < 18 THEN。该表达式将关系运算符(> 和 <)与逻辑 AND 组合在一起。

The evaluation order is determined by precedence: relational operators are evaluated before logical AND. Therefore, the two comparisons are performed first, and then AND ties their results. Using parentheses such as (score > 80) AND (age < 18) can improve clarity even if not strictly required by precedence.

求值顺序由优先级决定:关系运算符在逻辑 AND 之前求值。因此,先执行两个比较,然后 AND 连接它们的结果。使用括号,例如 (score > 80) AND (age < 18),即使优先级没有要求,也可以提高清晰度。

When arithmetic results are used in relational tests, arithmetic takes precedence. For example: IF price * quantity > 100 THEN. The product is calculated first, then compared to 100.

当算术结果用于关系测试时,算术优先级更高。例如:IF price * quantity > 100 THEN。首先计算乘积,然后与 100 比较。


11. Common Pitfalls | 常见陷阱

A frequent exam mistake is writing == as =. Using a single equals sign in a condition may accidentally assign a value rather than compare. In many languages this is syntactically valid but logically incorrect. Edexcel pseudocode requires == for equality testing in conditions.

一个常见的考试错误是将 == 写成 =。在条件中使用单个等号可能会意外地进行赋值而不是比较。在许多语言中这在语法上是合法的,但逻辑上是错误的。Edexcel 伪代码要求在条件中使用 == 进行相等测试。

Integer division can cause unexpected truncation. For example, (3 / 2) * 2 yields 2, not 3, if integer division discards the fraction. To avoid this, understand the data types involved or use explicit type casts where appropriate.

整数除法可能导致意外的截断。例如,如果整数除法丢弃小数部分,(3 / 2) * 2 的结果是 2 而不是 3。为避免这种情况,请理解所涉及的数据类型或在适当时使用显式类型转换。

Short-circuit evaluation is an optimisation where the second operand of a logical AND or OR is not evaluated if the first operand determines the outcome. For AND, if the left side is FALSE, the right side is skipped. This behaviour can affect expressions with side effects, so it must be considered when debugging.

短路求值是一种优化:如果第一个操作数已经决定了逻辑 AND 或 OR 的结果,则不会对第二个操作数求值。对于 AND,如果左侧为 FALSE,则跳过右侧。此行为可能会影响带有副作用的表达式,因此在调试时必须加以考虑。

Operator precedence errors often arise when bitwise operators are mixed with comparison operators. For example, x & 3 == 3 is interpreted as x & (3 == 3) because equality has higher precedence than bitwise AND in many languages. Using parentheses is strongly recommended to avoid ambiguity.

当位运算符与比较运算符混合时,常常会发生优先级错误。例如,x & 3 == 3 会被解释为 x & (3 == 3),因为在许多语言中相等比较的优先级高于按位与。强烈建议使用括号以避免歧义。


12. Summary | 总结

Operators form the backbone of program logic. Being fluent in arithmetic, relational, logical and bitwise operators enables you to construct precise expressions and algorithms. A-Level Edexcel Computer Science places particular emphasis on the correct use of operators in pseudocode and on predicting the output of expressions.

运算符是程序逻辑的支柱。熟练掌握算术、关系、逻辑和位运算符,使你能够构建精确的表达式和算法。A-Level Edexcel 计算机科学特别强调在伪代码中正确使用运算符以及预测表达式输出。

Always pay close attention to operator precedence, use parentheses for clarity, and avoid confusing assignment with equality. With regular practice, operator usage becomes second nature, giving you confidence in both theory and practical programming tasks.

始终密切关注运算符优先级,使用括号以提高清晰度,并避免将赋值与相等混淆。通过定期练习,运算符的使用将变得得心应手,使你在理论考试和实际编程任务中充满信心。

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