📚 Operators in Programming: An Edexcel A-Level Guide | 编程中的运算符:Edexcel A-Level 指南
In A-Level Programming, operators are the building blocks of expressions. They let a program perform calculations, compare values, combine conditions and assign results. Understanding operators is essential for Edexcel Paper 1 and for writing clear, efficient code.
在 A-Level 编程中,运算符是表达式的基本构建块。它们让程序能够执行计算、比较值、组合条件并赋值结果。理解运算符对 Edexcel 试卷一和编写清晰、高效的代码至关重要。
1. Introduction to Operators and Expressions | 运算符和表达式简介
An operator is a symbol that tells the compiler or interpreter to carry out a specific operation on one or more operands. For example, in the expression a + b, + is the operator and a, b are operands.
运算符是一个符号,它告诉编译器或解释器对一个或多个操作数执行特定操作。例如,在表达式 a + b 中,+ 是运算符,a 和 b 是操作数。
Expressions combine variables, literals, function calls and operators to produce a value. Edexcel questions often ask you to evaluate an expression step by step using the correct precedence.
表达式将变量、字面量、函数调用和运算符组合起来以产生一个值。Edexcel 题目经常要求你按照正确的优先级一步一步求值表达式。
In pseudocode, an expression can be as simple as a single variable or as complex as ((x + 2) * 3) MOD 5. Every part of the expression must produce a value of a suitable data type.
在伪代码中,表达式可以像单个变量一样简单,也可以像 ((x + 2) * 3) MOD 5 一样复杂。表达式的每个部分都必须产生一个合适数据类型的值。
2. Arithmetic Operators | 算术运算符
Arithmetic operators perform mathematical calculations. The main ones are +, -, *, /, MOD and DIV depending on the pseudocode style used in the exam.
算术运算符执行数学计算。主要的符号是 +、-、*、/、MOD 和 DIV,具体取决于考试中使用的伪代码风格。
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 3 + 4 = 7 |
| – | Subtraction | 9 – 2 = 7 |
| * | Multiplication | 6 * 3 = 18 |
| / | Division | 8 / 2 = 4 |
| MOD | Remainder | 17 MOD 5 = 2 |
| DIV | Integer division | 17 DIV 5 = 3 |
Integer division and modulo are particularly important in A-Level programming because they help process digits, cycles and repeated patterns.
整除和取模在 A-Level 编程中特别重要,因为它们有助于处理数字位、循环和重复模式。
In Edexcel pseudocode, MOD returns the remainder and DIV returns the whole-number quotient. For example, 17 DIV 5 = 3 and 17 MOD 5 = 2.
在 Edexcel 伪代码中,MOD 返回余数,DIV 返回整数商。例如,17 DIV 5 = 3,17 MOD 5 = 2。
17 DIV 5 = 3 and 17 MOD 5 = 2 because 17 = 3 × 5 + 2
When you use arithmetic operators, the result data type depends on the operands. Integer and integer usually gives an integer in DIV, but real division / may give a real number.
使用算术运算符时,结果的数据类型取决于操作数。整数与整数进行 DIV 通常得到整数,但实数除法 / 可能得到实数。
3. Comparison / Relational Operators | 比较/关系运算符
Comparison operators compare two values and return a Boolean result: TRUE or FALSE. These are used in selection statements and loops.
比较运算符比较两个值并返回布尔结果:TRUE 或 FALSE。它们用于选择语句和循环中。
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | 5 = 5 is TRUE |
| <> | Not equal to | 5 <> 3 is TRUE |
| > | Greater than | 7 > 2 is TRUE |
| < | Less than | 3 < 9 is TRUE |
| ≥ | Greater than or equal to | 6 ≥ 6 is TRUE |
| ≤ | Less than or equal to | 4 ≤ 5 is TRUE |
In Edexcel pseudocode, equality is tested with = and inequality with <>. Many real languages use == and !=, so you must be familiar with both conventions.
在 Edexcel 伪代码中,相等性用 = 测试,不等性用 <>。许多真实语言使用 == 和 !=,因此你必须熟悉这两种约定。
Relational operators always produce a Boolean. This matters when you are tracing a condition such as IF score ≥ 60 THEN.
关系运算符总是产生布尔值。这在追踪诸如 IF score ≥ 60 THEN 这样的条件时很重要。
You can compare numbers, characters and strings. String comparison is usually based on alphabetical or ASCII order, so ‘A’ < ‘B’ is TRUE, but ‘a’ < ‘B’ depends on the character set.
你可以比较数字、字符和字符串。字符串比较通常基于字母顺序或 ASCII 顺序,因此 ‘A’ < ‘B’ 为 TRUE,但 ‘a’ < ‘B’ 取决于字符集。
4. Logical Operators | 逻辑运算符
Logical operators combine Boolean expressions. The three core operators are AND, OR and NOT.
逻辑运算符组合布尔表达式。三个核心运算符是 AND、OR 和 NOT。
AND returns TRUE only when both operands are TRUE. OR returns TRUE when at least one operand is TRUE. NOT reverses the truth value.
AND 仅当两个操作数都为 TRUE 时返回 TRUE。OR 至少一个操作数为 TRUE 时返回 TRUE。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 common in programming: if the first operand determines the result, the second may not be evaluated. In the exam, you should trace logical expressions carefully.
短路求值在编程中很常见:如果第一个操作数已经决定结果,第二个操作数可能不会被求值。在考试中,你应该仔细追踪逻辑表达式。
For example, IF (x > 0) AND (y DIV x > 2) THEN will not divide by zero if x is not greater than 0, because the AND fails immediately.
Published by TutorHao | A-Level 编程 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply