📚 Edexcel A-Level Programming: Data Types, Operators and Control Structures | Edexcel A-Level 编程:数据类型、运算符与控制结构
Programming is at the heart of Edexcel A-Level Computer Science. This article consolidates the key A-Level programming techniques you need to master: variables, data types, operators, selection, iteration, functions and scope.
编程是 Edexcel A-Level 计算机科学的核心。本文整合了你需要掌握的关键 A-Level 编程技术:变量、数据类型、运算符、选择、迭代、函数与作用域。
1. Variables, Constants and Assignment | 变量、常量与赋值
In A-Level pseudocode, a variable is a named store that can hold a value; the value may be changed by an assignment statement using the left arrow or equals sign. Constants are fixed values that cannot be modified after declaration.
在 A-Level 伪代码中,变量是命名的存储位置,可以保存一个值;该值可以通过使用左箭头或等号的赋值语句来改变。常量是固定值,在声明后不能修改。
For example, score ← 0 assigns the integer 0 to the variable score. A constant declaration such as CONST VAT_RATE = 0.20 prevents accidental changes later in the program.
例如,score ← 0 将整数 0 赋给变量 score。像 CONST VAT_RATE = 0.20 这样的常量声明可以防止程序后面意外更改。
2. Primitive Data Types and Type Conversion | 基本数据类型与类型转换
Edexcel A-Level questions expect you to choose appropriate data types: integer for whole numbers, real/float for decimal numbers, Boolean for TRUE/FALSE, character for a single symbol, and string for text.
Edexcel A-Level 题目希望你选择合适的数据类型:整数用于整数,实数/浮点用于小数,布尔型用于 TRUE/FALSE,字符用于单个符号,字符串用于文本。
Explicit type conversion is common in pseudocode: INT("42") returns 42, STR(3.14) returns "3.14", and REAL("2.5") returns 2.5. Implicit conversion happens when a language automatically promotes an integer to a real in mixed arithmetic.
显式类型转换在伪代码中很常见:INT("42") 返回 42,STR(3.14) 返回 "3.14",REAL("2.5") 返回 2.5。当语言在混合算术中自动将整数提升为实数时,就发生隐式转换。
3. Arithmetic Operators and Precedence | 算术运算符与优先级
The arithmetic operators are +, -, *, /, MOD and DIV. The division operator / usually gives a real result in high-level languages, while DIV gives the integer quotient and MOD gives the integer remainder.
算术运算符有 +、-、*、/、MOD 和 DIV。除法运算符 / 在高级语言中通常给出实数结果,而 DIV 给出整数商,MOD 给出整数余数。
Use brackets to control precedence. The expression 3 + 4 * 2 evaluates to 11 because multiplication has higher precedence than addition, but (3 + 4) * 2 evaluates to 14.
使用括号来控制优先级。表达式 3 + 4 * 2 的值为 11,因为乘法的优先级高于加法,但 (3 + 4) * 2 的值为 14。
17 DIV 5 = 3 and 17 MOD 5 = 2
4. Comparison and Logical Operators | 比较运算符与逻辑运算符
Comparison operators are =, ! on is TRUE only when both operands are TRUE; OR is TRUE when at least one operand is TRUE.
比较运算符有 =、!=、<、>、<=、>=。逻辑运算符有 AND、OR 和 NOT。AND 条件只有在两个操作数都为 TRUE 时才为 TRUE;OR 在至少一个操作数为 TRUE 时为 TRUE。
| 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 flips a Boolean value, so NOT TRUE equals FALSE and NOT FALSE equals TRUE. In exam questions, simplify compound conditions carefully using these truth tables.
NOT 会翻转布尔值,因此 NOT TRUE 等于 FALSE,NOT FALSE 等于 TRUE。在考试题中,使用这些真值表仔细化简复合条件。
5. String Operations and Indexing | 字符串操作与索引
String operations include length, substring, concatenation, character indexing and case conversion. Indexing may start at 0 or 1 depending on the pseudocode notation, so read the question carefully.
字符串操作包括长度、子串、连接、字符索引和大小写转换。索引可能从 0 或 1 开始,取决于伪代码符号,因此要仔细阅读题目。
For example, if name ← "Ada", then name[0] returns 'A' in 0-based indexing, while name[1] returns 'A' in 1-based indexing. Concatenation uses the + operator in many languages, such as "Ada" + "Lovelace".
例如,如果 name ← "Ada",在基于 0 的索引中 name[0] 返回 'A',而在基于 1 的索引中 name[1] 返回 'A'。许多语言中使用 + 运算符进行连接,例如 "Ada" + "Lovelace"。
6. Selection: IF and CASE Statements | 选择结构:IF 与 CASE 语句
IF statements allow a program to branch. Use ELSE IF when there are multiple mutually exclusive conditions, and ELSE as a catch-all. A CASE or SELECT statement is cleaner when testing one variable against several constant values.
IF 语句允许程序分支。当有多个互斥条件时使用 ELSE IF,ELSE 作为兜底。当测试一个变量与多个常量值的关系时,CASE 或 SELECT 语句更清晰。
IF score >= 80 THEN
grade ← "A"
ELSE IF score >= 60 THEN
grade ← "B"
ELSE
grade ← "C"
END IF
The conditions are evaluated in order; once a TRUE condition is found, its block runs and the remaining branches are skipped. This sequential evaluation is a common Edexcel trace-table task.
条件按顺序求值;一旦找到 TRUE 条件,就执行其块并跳过其余分支。这种顺序求值是 Edexcel 追踪表的常见题型。
7. Iteration: Count-Controlled and Condition-Controlled Loops | 迭代:计数控制与条件控制循环
FOR loops are count-controlled and repeat a fixed number of times. WHILE loops are condition-controlled and may execute zero times. REPEAT-UNTIL loops always execute at least once.
FOR 循环是计数控制的,重复固定次数。WHILE 循环是条件控制的,可能执行零次。REPEAT-UNTIL 循环至少执行一次。
FOR i ← 1 TO 10
OUTPUT i
END FOR
WHILE x < 100 DO
x ← x * 2
END WHILE
REPEAT
x ← x + 1
UNTIL x = 20
Choose a FOR loop when the number of iterations is known in advance. Choose a WHILE or REPEAT loop when the loop must continue until a condition changes, such as reading input until a sentinel value is entered.
当迭代次数已知时选择 FOR 循环。当循环必须持续到条件改变时选择 WHILE 或 REPEAT 循环,例如读取输入直到输入哨兵值。
8. Nesting and Flow Control | 嵌套与流程控制
Nested structures place one control structure inside another. A common A-Level task is a nested FOR loop to fill a 2D array or print a pattern.
嵌套结构是将一个控制结构放在另一个控制结构内部。A-Level 常见任务是使用嵌套 FOR 循环填充二维数组或打印图案。
FOR row ← 1 TO 3
FOR col ← 1 TO 3
OUTPUT row * col
END FOR
END FOR
The inner loop completes all its iterations for each single iteration of the outer loop. This gives 9 output lines in the example above. In flowcharts, nesting is shown by placing one control block inside another.
内层循环在外层循环的每一次迭代中完成全部迭代。在上面的例子中会输出 9 行。在流程图中,通过将一个控制块放在另一个控制块内部来表示嵌套。
9. Functions, Procedures and Parameter Passing | 函数、过程与参数传递
A function returns a value and can be used in an expression; a procedure performs a task but does not return a value. Parameters may be passed by value or by reference.
函数返回一个值并可用在表达式中;过程执行任务但不返回值。参数可以按值或按引用传递。
Passing by value passes a copy, so changes inside the routine do not affect the original argument. Passing by reference passes the address, so changes inside the routine do affect the original argument.
按值传递传递副本,因此例程内部的更改不会影响原始参数。按引用传递传递地址,因此例程内部的更改会影响原始参数。
FUNCTION square(n: INTEGER) RETURNS INTEGER
RETURN n * n
END FUNCTION
In Edexcel pseudocode, the RETURN statement exits a function immediately and sends the value back to the caller. A procedure does not use RETURN with a value.
在 Edexcel 伪代码中,RETURN 语句立即退出函数,并将值返回给调用者。过程不使用带值的 RETURN。
10. Scope: Local vs Global Variables | 作用域:局部变量与全局变量
Local variables are declared inside a function or procedure and exist only during its execution. Global variables are declared
Published by TutorHao | A-Level 编程 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图