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

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

Programming revolves around processing data, and operators are the building blocks that let us transform and compare that data. This article covers the operator types, precedence rules, truth tables, and common exam pitfalls required by the Edexcel A-Level Computer Science specification.

编程围绕数据处理展开,而运算符是让我们能够转换和比较数据的基本构件。本文涵盖 Edexcel A-Level 计算机科学大纲要求的运算符类型、优先级规则、真值表和常见考试陷阱。

1. Operator Categories | 运算符分类

An operator is a symbol or keyword that performs an operation on one or more operands. For example, in the expression a + b, + is the operator and a, b are operands. Operators can be unary, binary, or ternary depending on the number of operands they take.

运算符是对一个或多个操作数执行操作的符号或关键字。例如,在表达式 a + b 中,+ 是运算符,a、b 是操作数。根据操作数的数量,运算符可以是一元、二元或三元运算符。

The main operator categories tested in Edexcel A-Level Computer Science are arithmetic, comparison, logical, bitwise, assignment, and string operators. Each category has its own rules, and mixing them often causes errors in exam answers.

Edexcel A-Level 计算机科学考查的主要运算符类别包括算术、比较、逻辑、位、赋值和字符串运算符。每个类别都有自己的规则,混用它们常常会导致考试答案出错。


2. Arithmetic Operators | 算术运算符

Arithmetic operators perform standard mathematical calculations. The most common ones are addition (+), subtraction (-), multiplication (*), division (/), integer division (DIV), and modulo (MOD). In pseudocode, Edexcel often uses DIV and MOD; in Python, integer division is // and modulo is %.

算术运算符执行标准数学计算。最常见的有加(+)、减(-)、乘(*)、除(/)、整除(DIV)和取模(MOD)。在伪代码中,Edexcel 常用 DIV 和 MOD;在 Python 中,整除是 //,取模是 %。

Operator Meaning Example Result
+ Addition 7 + 2 9
Subtraction 7 – 2 5
* Multiplication 7 * 2 14
/ Division 7 / 2 3.5
DIV Integer division 7 DIV 2 3
MOD Modulo 7 MOD 2 1

In arithmetic expressions, the data types of operands matter. If both operands are integers, integer division may be used automatically in some languages, while division always produces a real result in others.

在算术表达式中,操作数的数据类型非常重要。如果两个操作数都是整数,某些语言可能会自动使用整除,而在其他语言中除法总是产生实数结果。


3. Division, Integer Division and Modulo | 除法、整除与取模

Division can be ordinary division returning a real number, or integer division returning the whole-number quotient without the remainder. Modulo returns the remainder. These are essential for problems involving digit extraction, even/odd checks, and cyclic indexing.

除法可以是返回实数的普通除法,也可以是返回整数商且不含余数的整除。取模返回余数。这些在涉及数字提取、奇偶判断和循环索引的问题中至关重要。

In many algorithms, DIV and MOD are used together. For a two-digit integer n, the tens digit is n DIV 10 and the units digit is n MOD 10. For example, 57 DIV 10 = 5 and 57 MOD 10 = 7.

在许多算法中,DIV 和 MOD 一起使用。对于一个两位整数 n,十位数字是 n DIV 10,个位数字是 n MOD 10。例如,57 DIV 10 = 5,57 MOD 10 = 7。

A common exam task checks whether a number is even: if n MOD 2 = 0 then the number is even; otherwise it is odd. This condition must use MOD, not division, because division would give a real quotient.

常见的考试任务是检查一个数是否为偶数:如果 n MOD 2 = 0,则该数为偶数;否则为奇数。该条件必须使用 MOD,而不是除法,因为除法会得到实数商。


4. Comparison / Relational Operators | 比较 / 关系运算符

Comparison operators compare two values and return a Boolean result (TRUE or FALSE). The standard set includes equal to (= or ==), not equal to (≠ or !=), greater than (>), less than (<), greater than or equal to (≥ or >=), and less than or equal to (≤ or <=).

比较运算符比较两个值并返回布尔结果(TRUE 或 FALSE)。标准集合包括等于(= 或 ==)、不等于(≠ 或 !=)、大于(>)、小于(<)、大于等于(≥ 或 >=)和小于等于(≤ 或 <=)。

A single ‘=’ in many languages is assignment, while ‘==’ is comparison, a common exam trap. In Edexcel pseudocode, comparison often uses ‘=’ and assignment uses ‘←’, so the context makes the meaning clear.

在许多语言中,单个 ‘=’ 是赋值,而 ‘==’ 是比较,这是一个常见的考试陷阱。在 Edexcel 伪代码中,比较常使用 ‘=’,赋值使用 ‘←’,因此上下文使含义清晰。

Comparison expressions are used in selection and iteration statements. For example, IF score >= 80 THEN grade ← ‘A’. The result is always a Boolean value. Be careful to use ‘=’ or ‘==’ consistently as specified by the question.

比较表达式用于选择和迭代语句。例如,IF score >= 80 THEN grade ← ‘A’。结果始终是布尔值。注意按照题目要求一致使用 ‘

Published by TutorHao | A-Level 编程 Revision Series | aleveler.com

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

Comments

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

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