Operations in Programming: Arithmetic, Relational and Boolean | 编程中的运算:算术、关系与布尔运算

📚 Operations in Programming: Arithmetic, Relational and Boolean | 编程中的运算:算术、关系与布尔运算

In Edexcel A-Level Computer Science, the ability to construct and evaluate expressions using arithmetic, relational and Boolean operations is fundamental. These operations form the building blocks of decision making, repetition and data manipulation in all programming languages and pseudocode. A deep understanding of how they work, their precedence and common pitfalls is essential for success in both Paper 1 and the programming project.

在 Edexcel A-Level 计算机科学中,使用算术、关系和布尔运算构建并求值表达式是一项基本能力。这些运算构成了所有编程语言和伪代码中决策、循环和数据操作的基石。深入理解它们的工作方式、优先级以及常见陷阱,对于在 Paper 1 和编程项目中取得成功至关重要。

1. What Are Operations? | 什么是运算?

An operation is a process that takes one or more input values (operands) and produces an output. The symbols that represent these processes, such as + and >, are called operators. For example, in the expression 5 + 3, 5 and 3 are operands and + is the arithmetic operator. Operations can be classified into arithmetic, relational, Boolean and string operations, each serving a distinct purpose in an algorithm.

运算是一个过程,它接收一个或多个输入值(操作数)并产生一个输出。表示这些过程的符号,如 +>,称为运算符。例如,在表达式 5 + 3 中,53 是操作数,+ 是算术运算符。运算可分为算术、关系、布尔和字符串运算,每一种在算法中都有其独特的用途。


2. Arithmetic Operators | 算术运算符

Arithmetic operators perform mathematical calculations. In Edexcel pseudocode, the standard operators are addition (+), subtraction (), multiplication (*), division (/), integer division (DIV) and modulus (MOD). These operators allow a program to calculate totals, averages, remainders and much more.

算术运算符执行数学计算。在 Edexcel 伪代码中,标准运算符包括加法(+)、减法()、乘法(*)、除法(/)、整除(DIV)和取模(MOD)。这些运算符使程序能够计算总和、平均值、余数等等。

  • + (Addition): Adds two numeric values, e.g. 7 + 2 gives 9. 加法:将两个数值相加,如 7 + 2 得到 9。
  • – (Subtraction): Subtracts the right operand from the left, e.g. 10 – 4 gives 6. 减法:从左操作数减去右操作数,如 10 – 4 得到 6。
  • * (Multiplication): Multiplies two values, e.g. 3 * 4 gives 12. 乘法:将两个值相乘,如 3 * 4 得到 12。
  • / (Division): Divides the left operand by the right, yielding a real (float) result, e.g. 7 / 2 gives 3.5. 除法:左操作数除以右操作数,得到实数(浮点数)结果,如 7 / 2 得到 3.5。
  • DIV (Integer division): Returns the whole-number part of a division, e.g. 7 DIV 2 gives 3. 整除:返回除法结果的整数部分,如 7 DIV 2 得到 3。
  • MOD (Modulus): Returns the remainder of a division, e.g. 7 MOD 2 gives 1. 取模:返回除法运算的余数,如 7 MOD 2 得到 1。

3. Integer Division and Modulus in Depth | 深入整除与取模

DIV and MOD are essential for algorithms that involve cyclic repetition, parity checks and data extraction. Because integer division discards the fractional part, it is ideal for converting a raw count into whole groups. For instance, if seconds DIV 60 yields minutes and seconds MOD 60 yields the leftover seconds.

DIV 和 MOD 对于涉及循环重复、奇偶校验和数据提取的算法至关重要。由于整除会丢弃小数部分,它非常适合将计数转换为完整组数。例如,seconds DIV 60 得出分钟数,seconds MOD 60 得出剩余的秒数。

A classic use of MOD is checking whether a number is even: IF num MOD 2 = 0 THEN signals an even number. Similarly, a loop counter can cycle through a range by resetting with MOD: index ← (index + 1) MOD length keeps the index within array bounds.

MOD 的一个经典用途是判断一个数是否为偶数:IF num MOD 2 = 0 THEN 表示偶数。类似地,循环计数器可以利用 MOD 实现循环:index ← (index + 1) MOD length 使索引保持在数组范围内。


4. Relational Operators | 关系运算符

Relational operators compare two values and return a Boolean result (TRUE or FALSE). In Edexcel pseudocode, these are: equal to (=), not equal to (<>), greater than (>), less than (<), greater than or equal to (>=) and less than or equal to (<=). Do not confuse = (comparison) with (assignment).

关系运算符比较两个值并返回布尔结果(TRUEFALSE)。在 Edexcel 伪代码中,这些运算符分别是:等于(=)、不等于(<>)、大于(>)、小于(<)、大于等于(>=)和小于等于(<=)。不要将比较用的 = 与赋值用的 混淆。

Relational operators are used in selection statements (IF … THEN … ELSE) and loops (WHILE … DO … ENDWHILE). For example, IF age >= 18 THEN controls access to an adult function. Comparisons are evaluated left to right with associativity rules, but their precedence is lower than arithmetic operators.

关系运算符用于选择语句(IF … THEN … ELSE)和循环(WHILE … DO … ENDWHILE)。例如,IF age >= 18 THEN 控制对成人功能的访问。比较运算按照结合性规则从左到右求值,但其优先级低于算术运算符。


5. Boolean (Logical) Operators | 布尔(逻辑)运算符

Boolean operators combine multiple conditions to form a single logical expression. The three primary Boolean operators are AND, OR and NOT. They operate on Boolean operands and produce a Boolean outcome, allowing complex decision trees to be built from simple comparisons.

布尔运算符将多个条件组合成单个逻辑表达式。三个主要的布尔运算符是 ANDORNOT。它们对布尔操作数进行运算并产生布尔结果,从而能够从简单的比较构建复杂的决策树。

AND returns TRUE only if both operands are TRUE; otherwise it returns FALSE. OR returns TRUE if at least one operand is TRUE. NOT is a unary operator that flips the Boolean value: NOT TRUE becomes FALSE.

AND 仅当两个操作数都为 TRUE 时才返回 TRUE;否则返回 FALSE。OR 只要至少有一个操作数为 TRUE 就返回 TRUE。NOT 是一元运算符,会翻转布尔值:NOT TRUE 变成 FALSE。


6. Operator Precedence | 运算符优先级

When an expression contains multiple operators, the order of evaluation is governed by precedence rules. Without proper understanding, a seemingly correct expression may produce unexpected results. The table below outlines the standard precedence in Edexcel pseudocode, from highest to lowest.

当一个表达式包含多个运算符时,求值顺序由优先级规则决定。如果不理解这些规则,看似正确的表达式可能会产生意料之外的结果。下表概述了 Edexcel 伪代码中的标准优先级,从高到低排列。

Level Operator(s) Description
1 (highest) ( ) Parentheses – evaluate inner expressions first
2 NOT Logical NOT
3 *, /, DIV, MOD Multiplicative operators (left to right)
4 +, – Additive operators (left to right)
5 =, <>, >, <, >=, <= Relational operators
6 AND Logical AND
7 (lowest) OR Logical OR

For example, NOT a > b AND c is evaluated as (NOT (a > b)) AND c. Always use parentheses to clarify intent and avoid ambiguity.

例如,NOT a > b AND c 会被求值为 (NOT (a > b)) AND c。始终使用括号来明确意图并避免歧义。


7. Truth Tables for AND, OR and NOT | AND、OR、NOT 真值表

A truth table summarises the output of a logical operator for every possible input combination. These tables are an essential tool for exam questions that ask you to evaluate or simplify Boolean expressions. The basic truth tables are shown below.

真值表总结了逻辑运算符在每种可能输入组合下的输出。这些表格对于要求求值或化简布尔表达式的考试题目而言是必不可少的工具。以下是基本的真值表。

A B A AND B A OR B
TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE
FALSE TRUE FALSE TRUE
FALSE FALSE FALSE FALSE

NOT truth table: NOT TRUE = FALSE, NOT FALSE = TRUE. When combining operators, construct a truth table column by column, evaluating the highest precedence sub-expressions first.

NOT 真值表:NOT TRUE = FALSENOT FALSE = TRUE。组合运算符时,逐列构建真值表,首先求值具有最高优先级的子表达式。


8. Combining Multiple Operators | 组合多种运算符

Real-world algorithms often mix arithmetic, relational and Boolean operators in a single line. The key to writing and interpreting such expressions is careful use of parentheses and awareness of precedence. For instance, the condition IF (x + y > 10) AND (z MOD 2 = 0) THEN first performs addition x + y, then compares it to 10, evaluates the modulus and combine the two Boolean results with AND.

现实世界的算法常常在单行代码中混合使用算术、关系和布尔运算符。编写和解释这类表达式的关键在于谨慎使用括号并清楚优先级。例如,条件 IF (x + y > 10) AND (z MOD 2 = 0) THEN 首先执行加法 x + y,将其与 10 比较,计算模并求值,然后用 AND 将两个布尔结果结合起来。

When evaluating an expression manually, underline sub-expressions and work step by step. Without parentheses, the expression a + b > c AND d = e could be misinterpreted. Always insert clarifying parentheses in your pseudocode to make the intention obvious.

手动求值表达式时,划出子表达式并逐步计算。如果没有括号,a + b > c AND d = e 这样的表达式可能被误解。在伪代码中始终插入清晰的括号,使意图显而易见。


9. Short-Circuit Evaluation | 短路求值

Short-circuit evaluation is an optimisation technique where the second operand of an AND or OR expression is not evaluated if the first operand already determines the outcome. For AND, if the left-hand side is FALSE, the whole expression is FALSE regardless of the right side. For OR, if the left side is TRUE, the whole expression is TRUE.

短路求值是一种优化技术,如果 AND 或 OR 表达式的第一个操作数已经决定了结果,那么第二个操作数就不会被求值。对于 AND,如果左侧为 FALSE,则无论右侧如何,整个表达式都为 FALSE。对于 OR,如果左侧为 TRUE,则整个表达式为 TRUE。

While short-circuit evaluation can speed up execution, it can lead to subtle bugs if the right-hand expression has side effects, such as modifying a variable or calling a function. In exam pseudocode, you are rarely required to exploit short-circuit behaviour, but you must understand that it can affect trace tables.

虽然短路求值可以加快执行速度,但如果右侧表达式有副作用(例如修改变量或调用函数),则可能导致隐蔽的错误。在考试伪代码中,你很少需要利用短路行为,但必须明白它可能影响跟踪表。


10. Common Programming Errors | 常见编程错误

One frequent mistake is confusing the assignment operator () with the equality comparison (=). Writing IF x ← 5 THEN is syntactically invalid in pseudocode; the condition expects a Boolean, not an assignment. Another error is forgetting the difference between / and DIV, leading to type mismatches or unexpected truncation.

一个常见错误是混淆赋值运算符()与相等比较符(=)。编写 IF x ← 5 THEN 在伪代码中语法无效;条件期望一个布尔值,而不是赋值。另一个错误是忘记 /DIV 之间的区别,从而导致类型不匹配或意外的截断。

Operator precedence errors are also widespread. The expression NOT a = b is evaluated as NOT (a = b), not as (NOT a) = b. Similarly, a > b AND c < d requires no parentheses because relational operators bind before AND, but many programmers add parentheses for readability. Failing to enclose arithmetic within relational tests often yields wrong Boolean results.

优先级错误也很常见。表达式

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课程辅导,国外大学本科硕士研究生博士课程论文辅导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