Edexcel A-Level Programming Fundamentals: Variables, Control Flow and Recursion | Edexcel A-Level 编程基础:变量、控制流与递归

📚 Edexcel A-Level Programming Fundamentals: Variables, Control Flow and Recursion | Edexcel A-Level 编程基础:变量、控制流与递归

Programming is at the heart of the Edexcel A-Level Computer Science specification. To write robust, efficient code, you need to master variables, data types, selection, iteration, subroutines, and recursion. This revision guide explains each concept using Python and Edexcel-style pseudocode, so you can move confidently between theory and practical exam questions.

编程是 Edexcel A-Level 计算机科学课程的核心。要写出健壮、高效的代码,你必须掌握变量、数据类型、选择结构、迭代、子程序和递归。本复习指南使用 Python 和 Edexcel 风格伪代码逐一解释这些概念,帮助你在理论与实操题之间自如切换。


1. Variables, Constants and Data Types | 变量、常量与数据类型

In Edexcel programming, a variable is a named memory location whose value can change while the program runs. A constant is similar, but its value is fixed once declared. Common data types are integer, real/float, Boolean, character, and string. Python assigns types dynamically: age = 17 creates an integer, price = 4.99 creates a float. In Edexcel pseudocode you declare the type explicitly, for example DECLARE age : INTEGER.

在 Edexcel 编程中,变量是一个命名的内存位置,其值可以在程序运行时改变。常量与之类似,但一经声明就固定不变。常见数据类型包括整型、实型/浮点型、布尔型、字符型和字符串型。Python 会动态分配类型:age = 17 创建整型,price = 4.99 创建浮点型。在 Edexcel 伪代码中,你需要显式声明类型,例如 DECLARE age : INTEGER

Type conversions are often tested. In Python, int("17") converts a string to an integer, float(4) converts an integer to a float, and str(17) converts an integer to a string. In pseudocode, the equivalent functions are INT_TO_STRING, STRING_TO_INT, and REAL_TO_INT.

类型转换是常见考点。在 Python 中,int("17") 将字符串转换为整型,float(4) 将整型转换为浮点型,str(17) 将整型转换为字符串。在伪代码中,对应函数为 INT_TO_STRINGSTRING_TO_INTREAL_TO_INT


2. Input, Output and Assignment | 输入、输出与赋值

Input gets data from the user, output displays results, and assignment stores a value in a variable. In Python, name = input("Enter name:") reads a string, and print(name) outputs it. The assignment operator = does not mean equality; it copies the value on the right into the variable on the left.

输入用于获取用户数据,输出用于显示结果,赋值用于把值存入变量。在 Python 中,name = input("Enter name:") 读取一个字符串,print(name) 输出该字符串。赋值运算符 = 不表示相等,它把右侧的值复制到左侧的变量中。

In Edexcel pseudocode, input and output use INPUT and OUTPUT. For example, INPUT age reads a value into age, and OUTPUT "Your age is", age prints a labelled result. A common error is forgetting to convert input: age = int(input("Age:")) is needed if you want to use age in arithmetic.

在 Edexcel 伪代码中,输入和输出使用 INPUTOUTPUT。例如,INPUT age 把值读入 ageOUTPUT "Your age is", age 输出带标签的结果。一个常见错误是忘记转换输入:如果想在算术中使用 age,需要写 age = int(input("Age:"))


3. Arithmetic and Boolean Expressions | 算术与布尔表达式

Arithmetic operators in Edexcel include + - * / MOD DIV. MOD returns the remainder of division, while DIV returns the integer quotient. In Python, % is MOD and // is DIV. For example, 17 MOD 5 = 2 and 17 DIV 5 = 3. Operator precedence follows BIDMAS: brackets, indices, division/multiplication, addition/subtraction.

Edexcel 中的算术运算符包括 + - * / MOD DIVMOD 返回除法余数,DIV 返回整数商。在 Python 中,% 是 MOD,// 是 DIV。例如,17 MOD 5 = 217 DIV 5 = 3。运算符优先级遵循 BIDMAS:括号、指数、除/乘、加/减。

Boolean expressions evaluate to TRUE or FALSE. Comparison operators are = ≠ < > ≤ ≥. In Python, equality is == and not equal is !=. Boolean operators are AND, OR, and NOT. De Morgan’s laws are useful: NOT (A AND B) is equivalent to (NOT A) OR (NOT B).

布尔表达式求值为 TRUE 或 FALSE。比较运算符包括 = ≠ < > ≤ ≥。在 Python 中,相等写作 ==,不等写作 !=。布尔运算符为 ANDORNOT。德摩根定律非常有用:NOT (A AND B) 等价于 (NOT A) OR (NOT B)


4. Selection: IF and SWITCH Statements | 选择结构:IF 与 SWITCH 语句

Selection allows a program to take different paths based on a condition. The simplest form is a single IF statement. Python example:

选择结构允许程序根据条件执行不同路径。最简单的形式是单个 IF 语句。Python 示例:

if score >= 90:
  grade = “A”
elif score >= 80:
  grade = “B”
else:
  grade = “C”

In Edexcel pseudocode, this is written with IF ... THEN ... ELSEIF ... THEN ... ELSE ... ENDIF. The ELSEIF branch prevents unnecessary checks and makes the logic clearer than nested IFs.

在 Edexcel 伪代码中,写作 IF ... THEN ... ELSEIF ... THEN ... ELSE ... ENDIFELSEIF 分支可以避免不必要的判断,并使逻辑比嵌套 IF 更清晰。

Some languages include a SWITCH or CASE statement for comparing one value against several possibilities. Edexcel pseudocode uses CASE OF ... ENDCASE. Python does not have a built-in switch, so you use if-elif-else chains or a dictionary mapping. A common exam task is to rewrite a CASE statement as an IF structure and vice versa.

有些语言提供 SWITCH 或 CASE 语句,用于将一个值与多个可能值进行比较。Edexcel 伪代码使用 CASE OF ... ENDCASE。Python 没有内置的 switch,因此可以使用 if-elif-else 链或字典映射。常见的考试任务是要求把 CASE 语句改写为 IF 结构,或反过来改写。


5. Iteration: FOR and WHILE Loops | 迭代结构:FOR 与 WHILE 循环

Iteration repeats a block of code. A FOR loop is used when the number of iterations is known in advance. Python example: for i in range(5): runs i from 0 to 4. Edexcel pseudocode uses FOR i ← 1 TO 5 ... NEXT i.

迭代用于重复执行代码块。FOR 循环适合在迭代次数已知时使用。Python 示例:for i in range(5): 使 i 从 0 到 4。Edexcel 伪代码使用 FOR i ← 1 TO 5 ... NEXT i

A WHILE loop is used when the number of iterations depends on a condition. It checks the condition before each pass, so it may run zero times. A REPEAT…UNTIL loop checks the condition after each pass, so it always runs at least once. In Python, you can simulate REPEAT…UNTIL with a while True: loop and a break condition.

WHILE 循环适合在迭代次数取决于条件时使用。它在每次执行前检查条件,因此可能一次都不执行。REPEAT…UNTIL 循环在每次执行后检查条件,因此至少执行一次。在 Python 中,可以使用 while True: 循环和 break 条件来模拟 REPEAT…UNTIL。

  • FOR loop: know the exact count, e.g. process 10 items. | FOR 循环:已知确切次数,例如处理 10 个数据项。
  • WHILE loop: repeat while a sensor value is below a threshold. | WHILE 循环:当传感器值低于阈值时重复。
  • REPEAT…UNTIL loop: keep asking for input until valid. | REPEAT…UNTIL 循环:持续要求输入直到有效。

6. Subroutines, Functions and Procedures | 子程序、函数与过程

A subroutine is a named block of code that can be called from elsewhere. A function returns a value; a procedure does not return a value. In Python, a function is defined with def, and it may or may not use return. Subroutines reduce duplication, improve readability, and make testing easier.

子程序是一个命名的代码块,可以从其他位置调用。函数会返回一个值;过程不返回值。在 Python 中,使用 def 定义函数,它可以使用也可以不使用 return。子程序减少重复、提高可读性,并使测试更容易。

Python function example:

Python 函数示例:

def area_of_circle(radius):
  return 3.14 * radius * radius

Edexcel pseudocode uses FUNCTION area_of_circle(radius: REAL) RETURNS REAL and ENDFUNCTION. Procedures use PROCEDURE display_message(name: STRING) and ENDPROCEDURE. You must be able to trace subroutine calls in desk-based questions and identify local versus global variables.

Edexcel 伪代码使用 FUNCTION area_of_circle(radius: REAL) RETURNS REALENDFUNCTION。过程使用 PROCEDURE display_message(name: STRING)ENDPROCEDURE。你必须能够在书面题中跟踪子程序调用,并识别局部变量与全局变量。


7. Parameter Passing: By Value and By Reference | 参数传递:按值与按引用

Parameters allow subroutines to receive data. Passing by value copies the argument into the parameter, so changes inside the subroutine do not affect the original variable. Passing by reference gives the subroutine access to the original memory location, so changes do affect the original variable.

参数允许子程序接收数据。按值传递将实参复制到形参中,因此子程序内部的修改不会影响原变量。按引用传递让子程序访问原始内存位置,因此修改会直接影响原变量。

By value | 按值传递 By reference | 按引用传递
Original variable is safe. | 原变量安全。 Original variable can be changed. | 原变量可被修改。
Used for input parameters. | 用于输入参数。 Used when a subroutine must return multiple values. | 用于子程序返回多个值时。

In Edexcel pseudocode, parameters are often declared with BYVAL or BYREF. Python behaves like pass-by-object-reference: integers and strings cannot be changed inside functions, but lists can be modified. This is a common source of confusion, so practise tracing Python lists passed to functions.

在 Edexcel 伪代码中,参数通常声明为 BYVALBYREF。Python 的行为类似于按对象引用传递:整数和字符串在函数内部不能被修改,但列表可以被修改。这是常见的混淆点,因此要练习跟踪传入函数的 Python 列表。


8. Recursion and the Call Stack | 递归与调用栈

Recursion is when a subroutine calls itself. Every recursive solution must have a base case that stops the recursion and a recursive case that reduces the problem. A classic example is factorial: factorial(n) = n × factorial(n-1) with base case factorial(0) = 1.

递归是指子程序调用自身。每个递归解法必须有一个停止递归的基准情形,以及一个缩小问题的递归情形。经典例子是阶乘:factorial(n) = n × factorial(n-1),基准情形为 factorial(0) = 1

Python factorial function:

Python 阶乘函数:

def factorial(n):
  if n == 0:
    return 1
  else:
    return n * factorial(n – 1)

Each recursive call is placed on the call stack. The stack stores return addresses, local variables, and parameters. If the base case is missing, the stack overflows and the program crashes. Edexcel questions often ask you to trace recursion and show the stack frames at each step.

每次递归调用都会放入调用栈。栈中存储返回地址、局部变量和参数。如果缺少基准情形,栈会溢出,程序崩溃。Edexcel 考题经常要求你跟踪递归并展示每一步的栈帧。


9. Testing and Debugging Strategies | 测试与调试策略

Testing ensures the program meets its specification. Normal data should be valid and expected; boundary data tests the limits of conditions; erroneous data is invalid and should be handled gracefully. For example, if a program accepts scores 0-100, test 0, 100, -1, and 101.

测试确保程序符合规格说明。正常数据应是有效且预期内的;边界数据测试条件的极限;错误数据是无效数据,应被妥善处理。例如,如果一个程序接受 0-100 的分数,应测试 0、100、-1 和 101。

Debugging tools include trace tables, breakpoints, print statements, and walkthroughs. A trace table lists variables and shows how their values change line by line. In the Edexcel exam, you may be given a faulty piece of code and asked to identify the error type: syntax, logic, or runtime.

调试工具包括跟踪表、断点、print 语句和走查。跟踪表列出变量并展示其值如何逐行变化。在 Edexcel 考试中,你可能会看到一段有问题的代码,并被要求识别错误类型:语法错误、逻辑错误或运行时错误。

  • Syntax error: code violates language rules, e.g. missing colon. | 语法错误:代码违反语言规则,例如缺少冒号。
  • Logic error: code runs but gives the wrong result, e.g. using < instead of . | 逻辑错误:代码能运行但结果错误,例如使用 < 而不是
  • Runtime error: an error occurs during execution, e.g. division by zero. | 运行时错误:执行过程中发生错误,例如除以零。

10. Exam Skills for Edexcel Programming | Edexcel 编程考试技巧

When answering Edexcel programming questions, always show your working. If asked to write an algorithm, use the correct pseudocode syntax from the specification. If asked to trace code, draw a clear trace table with one row per iteration. If asked to compare algorithms, discuss time complexity, readability, and memory use.

回答 Edexcel 编程题时,务必写出解题过程。如果要求编写算法,请使用考试规范中正确的伪代码语法。如果要求跟踪代码,请绘制清晰的跟踪表,每次迭代一行。如果要求比较算法,请讨论时间复杂度、可读性和内存使用。

Common command words: “state” means give a fact; “explain” means give reasons; “write an algorithm” means produce structured pseudocode; “trace” means manually step through values. For Python-based practical papers, test your code with the three data types: normal, boundary, and erroneous.

常见指令词:”state” 表示给出事实;”explain” 表示给出理由;”write an algorithm” 表示编写结构化伪代码;”trace” 表示手动跟踪值。对于基于 Python 的实操试卷,请使用三类数据测试代码:正常数据、边界数据和错误数据。

Finally, practise past-paper programming questions under timed conditions. Focus on variable declaration, parameter passing, recursion trace tables, and converting between Python and pseudocode. These skills consistently appear in Edexcel A-Level Computer Science assessments.

最后,请在限时条件下练习历年编程真题。重点练习变量声明、参数传递、递归跟踪表,以及 Python 与伪代码之间的转换。这些技能在 Edexcel A-Level 计算机科学考试中反复出现。


Published by TutorHao | Computer Science 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