📚 Programming Techniques: Operators, Control Flow and Functions | 编程技术:运算符、控制流与函数
In Edexcel A Level Computer Science, programming questions assess more than remembering syntax: they test your ability to read, trace and design algorithms using exam pseudocode, Python or another high-level language. This guide brings together operators, control structures, subprograms, recursion and algorithm efficiency in a focused revision format.
在 Edexcel A Level 计算机科学中,编程题考查的不只是记住语法:它们测试你使用考试伪代码、Python 或其他高级语言阅读、跟踪和设计算法的能力。本指南以集中复习的形式,汇总运算符、控制结构、子程序、递归和算法效率。
1. Operators and Expressions | 运算符与表达式
rs (+ - * / MOD DIV), comparison operators (= ≠ < > ≤ ≥) and logical operators (AND OR NOT).
运算符是一切表达式的基本构件。在 Edexcel 伪代码中,你必须熟练掌握三类运算符:算术运算符(+ - * / MOD DIV)、比较运算符(= ≠ < > ≤ ≥)以及逻辑运算符(AND OR NOT)。
ber quotient, while MOD returns the remainder. For example, 17 DIV 5 = 3 and 17 MOD 5 = 2.
整数除法运算符 DIV 返回整数商,而 MOD 返回余数。例如,17 DIV 5 = 3,17 MOD 5 = 2。
17 DIV 5 = 3 17 MOD 5 = 2 17 / 5 = 3.4
ven; x MOD 10 gives the last digit of x; total seconds MOD 60 gives the leftover seconds after converting to whole minutes.
MOD 在考试中格外有用。判断 x MOD 2 = 0 可知 x 是否为偶数;x MOD 10 给出 x 的最后一位数字;总秒数 MOD 60 给出折算成整分钟后剩余的秒数。
| Category | 类别 | Operators | 运算符 | Example | 示例 |
|---|---|---|
| Arithmetic 算术 | + − * / MOD DIV | 17 MOD 5 = 2 |
| Comparison 比较 | = ≠ < > ≤ ≥ | score ≥ 40 |
| Logical 逻辑 | AND OR NOT | age ≥ 18 AND hasID |
Logical operators follow truth tables, so AND requires both operands to be true, OR requires at least one, and NOT reverses a single Boolean value.
逻辑运算符遵循真值表:AND 要求两个操作数都为真,OR 只要求其中一个为真,NOT 则对单个布尔值取反。
2. Operator Precedence, Data Types and Casting | 运算符优先级、数据类型与类型转换
Precedence decides the order in which operators are evaluated: brackets first, then NOT, then * / MOD DIV, then + −, then comparison, and finally AND / OR. Writing 2 + 3 * 4 gives 14, whereas (2 + 3) * 4 gives 20.
优先级决定运算符的计算顺序:先括号,再 NOT,再 * / MOD DIV,再 + −,然后是比较运算,最后是 AND / OR。2 + 3 * 4 得到 14,而 (2 + 3) * 4 得到 20。
2 + 3 × 4 = 14 (2 + 3) × 4 = 20
Data types matter because mixing them causes errors. Integer division of two integers in Python truncates, so 17 / 5 written with integer variables can surprise you if you expect 3.4. Use casting functions such as INTEGER(x), REAL(x) or STRING(x) to convert between types explicitly.
数据类型很重要,因为混用类型会产生错误。在 Python 中两个整数相除若使用整数除法会被截断,所以如果两个变量都是整数,期望 17 / 5 得到 3.4 就会出错。应使用 INTEGER(x)、REAL(x) 或 STRING(x) 等转换函数显式地转换类型。
String concatenation uses + or & to join text, for example forename + " " + surname.
字符串连接用 + 或 & 拼接文本,例如 forename + " " + surname。
Boolean expressions evaluate to TRUE or FALSE only, so they can be stored in a variable or returned directly.
布尔表达式只求值为 TRUE 或 FALSE,因此可以存入变量或直接返回。
Division by zero causes a run-time error, and it is a common source of marks lost in trace questions.
除以零会导致运行时错误,这也是跟踪题中常见的失分点。
3. Selection: IF, ELSE IF and CASE | 选择结构:IF、ELSE IF 与 CASE
Selection lets a program choose between alternative paths. The single-branch form tests one condition; the two-branch form adds ELSE; and multiple conditions are handled with ELSE IF or with a CASE statement when the same variable is compared against several fixed values.
选择结构让程序在多条路径之间做出选择。单分支形式只测试一个条件;双分支形式增加了 ELSE;当同一个变量需要与多个固定值比较时,可用 ELSE IF 或 CASE 语句处理多重条件。
In Edexcel pseudocode the block always ends with ENDIF, and every branch is indented. Indentation is not decoration: examiners use it to check that you understand which statements belong to which branch.
在 Edexcel 伪代码中,代码块必须以 ENDIF 结束,并且每个分支都要缩进。缩进不是装饰:阅卷人用它来确认你是否理解哪些语句属于哪个分支。
Use ELSE IF for overlapping ranges, for example grade boundaries.
对相互衔接的区间使用 ELSE IF,例如成绩等级划分。
Use CASE when matching a menu option or a single character code.
当匹配菜单选项或单个字符代码时使用 CASE。
Nested selection allows a second condition to be tested only when the first is true.
嵌套选择允许只在第一个条件为真时才测试第二个条件。
ng chain of OR conditions with NOT, and it is far easier to mark correct in a trace table.
布尔表达式应保持简洁易读。score ≥ 0 AND score ≤ 100 比一长串带 NOT 的 OR 条件清晰得多,在跟踪表中也更容易被判为正确。
4. Iteration: FOR, WHILE and REPEAT | 迭代结构:FOR、WHILE 与 REPEAT
Iteration repeats a block of code. Count-controlled loops (FOR) run a known number of times, condition-controlled loops (WHILE) test the condition before each pass, and post-condition loops (REPEAT ... UNTIL) test the condition after each pass so the body always executes at least once.
迭代用于重复执行代码块。计数控制循环(FOR)运行已知的次数,条件控制循环(WHILE)在每一轮开始前测试条件,而后置条件循环(REPEAT ... UNTIL)在每轮结束后测试条件,因此循环体至少执行一次。
Choosing the right loop is worth marks. If the number of repetitions is unknown in advance, a WHILE loop is the correct answer; if the loop must run once before any test makes sense, such as asking for input until it is valid, REPEAT is the correct answer.
选对循环类型本身就能得分。若重复次数事先未知,WHILE 循环是正确答案;若必须先执行一次循环体测试才有意义,例如反复要求输入直到合法,REPEAT 才是正确答案。
FOR i = 1 TO 10 ... NEXT i runs exactly ten times when the step is 1.
FOR i = 1 TO 10 ... NEXT i 在步长为 1 时正好运行十次。
WHILE condition ... ENDWHILE may run zero times if the condition is false at the start.
WHILE condition ... ENDWHILE 若一开始条件为假,可能一次也不执行。
REPEAT ... UNTIL condition always runs at least once, and stops when the condition becomes true.
REPEAT ... UNTIL condition 至少执行一次,并在条件变为真时停止。
A loop must eventually terminate. Forgetting to increment the counter inside a WHILE loop creates an infinite loop, a classic exam answer to the question "state what is wrong with this algorithm".
循环必须最终终止。在 WHILE 循环中忘记递增计数器会造成死循环,这是"指出该算法有何错误"这类题目的经典答案。
5. Subprograms: Procedures and Functions | 子程序:过程与函数
A subprogram is a named block of code that can be called from elsewhere in the program. A procedure performs an action and returns nothing; a function performs a calculation and returns exactly one value, which can be used inside an expression.
子程序是可以被程序其他位置调用的具名代码块。过程执行某种操作但不返回任何值;函数执行计算并返回且仅返回一个值,该值可以直接用于表达式中。
FUNCTION area(radius) → RETURN π × radius²
Because a function returns a value, you can write total ← total + area(5). Because a procedure does not, you call it as a statement on its own line, for example printReport(scores).
由于函数有返回值,你可以写 total ← total + area(5)。由于过程没有返回值,你只能把它当作独立语句调用,例如 printReport(scores)。
Examiners reward clear reasons for using subprograms, so learn a short list you can reproduce under time pressure.
阅卷人会对"使用子程序的理由"给分,因此请背下一份能在时间压力下复现的简明清单。
Decomposition: a large problem is split into smaller, testable tasks.
分解:把大问题拆成更小、可单独测试的任务。
Reusability: the same subprogram can be called many times from different places.
可复用:同一个子程序可在不同位置被多次调用。
Maintainability: fixing a bug in one place fixes it everywhere it is used.
可维护:在一处修复错误即可修复所有调用处。
Readability: named subprograms make the main program read like a summary of the solution.
可读性:具名子程序让主程序读起来像解题概要。
6. Parameter Passing: By Value and By Reference | 参数传递:按值与按引用
Parameters are the values passed into a subprogram when it is called. There are two mechanisms, and the difference is one of the most frequently examined points in the whole programming topic.
参数是在调用子程序时传入的数值。参数传递有两种机制,它们的区别是整个编程专题中最常被考查的知识点之一。
When a parameter is passed by value, the subprogram receives a copy. Changes made inside the subprogram affect only the copy, and the original variable in the calling program is unchanged when the subprogram returns.
当参数按值传递时,子程序收到的是副本。子程序内部所做的修改只影响副本,子程序返回后调用程序中的原变量保持不变。
When a parameter is passed by reference, the subprogram receives the address of the original variable. Any change made inside the subprogram is written back to the caller, so the original value is permanently altered.
当参数按引用传递时,子程序收到的是原变量的地址。子程序内部所做的任何修改都会写回调用方,因此原来的值被永久改变。
| Feature | 特征 | By Value | 按值 | By Reference | 按引用 |
|---|---|---|
| What is passed 传递内容 | A copy 副本 | The address 地址 |
| Effect on caller 对调用方的影响 | None 无影响 | Value changed 值被改变 |
| Typical use 典型用途 | Input data 输入数据 | Returning multiple results 返回多个结果 |
By reference is useful when a subprogram must change a variable or return more than one result, because a function can only ever return a single value.
当子程序必须改变某个变量,或需要返回多个结果时,按引用就很有用,因为函数只能返回一个值。
7. Scope: Global and Local Variables | 作用域:全局变量与局部变量
Scope describes where a variable can be seen and used. A local variable is declared inside a subprogram and exists only while that subprogram runs. A global variable is declared in the main program and can be accessed from anywhere.
作用域描述变量在何处可见、可用。局部变量在子程序内部声明,只在该子程序运行期间存在。全局变量在主程序中声明,可在任何位置访问。
Local variables are generally preferred because they reduce accidental interference between different parts of a program, and they are automatically discarded when the subprogram ends, which saves memory.
通常更推荐使用局部变量,因为它们能减少程序不同部分之间的意外干扰,并且在子程序结束时自动释放,从而节省内存。
Advantage of local: no unintended side effects, easier to debug and reuse.
局部变量的优点:不会产生意外的副作用,更易调试和复用。
Advantage of global: data can be shared across many subprograms without passing parameters.
全局变量的优点:数据可在多个子程序间共享,无需传参。
Disadvantage of global: any subprogram can change it, making faults hard to trace.
全局变量的缺点:任何子程序都能修改它,导致错误难以追踪。
A variable declared inside a loop or a subprogram is invisible outside it, so an exam question that asks why a value printed at the end of the program is zero is usually testing exactly this idea.
在循环或子程序内部声明的变量在其外部不可见,因此一道问"为什么程序最后输出的值是零"的题目,通常正是在考查这个概念。
8. Recursion | 递归
Recursion is the technique of a subprogram calling itself. Every correct recursive solution needs a base case, which stops the recursion, and a general case, which reduces the problem towards that base case.
递归是子程序调用自身的技术。每一个正确的递归解法都需要一个基准情形(base case)来终止递归,以及一个一般情形(general case)来把问题不断缩小、趋向基准情形。
factorial(n) = n × factorial(n − 1), factorial(0) = 1
Tracing recursion means following the call stack. Each call is pushed onto the stack and waits for the one below it to return, so the multiplications happen in reverse order as the stack unwinds.
跟踪递归意味着跟踪调用栈。每次调用都被压入栈中,等待更内层调用返回,因此乘法是在栈逐层弹出时按相反顺序完成的。
d the intermediate values 4, 3, 2, 1 are held on the stack until the base case factorial(0) returns 1.
factorial(4) 展开为 4 × 3 × 2 × 1,中间值 4、3、2、1 一直保存在栈中,直到基准情形 factorial(0) 返回 1。
Advantage: recursive code is often shorter and maps naturally onto tree and divide-and-conquer problems.
优点:递归代码通常更简短,且能自然对应树结构和分治问题。
Disadvantage: it uses more memory because of the call stack, and is slower than iteration for simple loops.
缺点:由于调用栈的存在,它占用更多内存,对简单循环来说比迭代更慢。
Risk: without a base case, or with a faulty one, the recursion never stops and causes a stack overflow.
风险:缺少基准情形或基准情形写错时,递归永不停止,导致栈溢出。
9. Tracing, Dry Runs and Testing | 跟踪、人工执行与测试
Many exam questions give you an algorithm and a set of input values, then ask for the output. The reliable method is a trace table with one column per variable and one row per iteration, updated strictly line by line.
许多考题会给出一个算法和一组输入值,然后要求写出输出。可靠的方法是画一张跟踪表,每个变量占一列,每次迭代占一行,严格逐行更新。
A dry run is performed by hand, without a computer, and is the technique you should use whenever a question contains the words "trace", "complete the table" or "state the output".
人工执行(dry run)是不借助计算机、手工完成的推演,凡是题目中出现"trace""complete the table""state the output"等字样,都应使用这一方法。
Normal data: typical, valid input the program is designed for.
正常数据:程序所针对的典型、合法输入。
Boundary data: values at the edge of validity, such as 0, 100 or the maximum array size.
边界数据:有效范围的边缘值,如 0、100 或数组最大长度。
Erroneous data: input of the wrong type or outside the accepted range, which should be rejected.
错误数据:类型错误或超出可接受范围的输入,应当被拒绝。
Extreme data: very large or very small values that stress the algorithm.
极端数据:极大或极小、会给算法带来压力的数值。
Debugging tools in an IDE complement this. Breakpoints pause execution at a chosen line, single stepping executes one statement at a time, and watch windows display the current value of a variable, which is exactly the information a trace table records on paper.
IDE 中的调试工具可以与之互补。断点让执行在指定行暂停,单步执行一次运行一条语句,监视窗口显示变量的当前值,这正是跟踪表在纸面上记录的信息。
10. Algorithm Efficiency and Big O | 算法效率与大 O 表示法
Efficiency describes how the work an algorithm does grows as the input size n grows. Big O notation expresses the upper bound of this growth and lets you compare two algorithms independently of the machine they run on.
效率描述算法的工作量随输入规模 n 增长的方式。大 O 表示法表达这种增长的上界,让你可以在不依赖具体机器的前提下比较两种算法。
| Complexity | 复杂度 | Name | 名称 | Example | 示例 |
|---|---|---|
| O(1) | Constant 常数 | Array index lookup 数组下标访问 |
| O(log n) | Logarithmic 对数 | Binary search 二分查找 |
| O(n) | Linear 线性 | Linear search 线性查找 |
| O(n log n) | Linearithmic 线性对数 | Merge sort 归并排序 |
| O(n²) | Quadratic 平方 | Bubble sort 冒泡排序 |
Nested loops usually indicate quadratic behaviour: if the outer loop runs n times and the inner loop also runs n times, the body executes approximately n² times.
嵌套循环通常意味着平方级复杂度:如果外层循环运行 n 次,内层循环也运行 n 次,循环体大约执行 n² 次。
Binary search is faster than linear search on a sorted list because it halves the search space each time, requiring about log₂ n comparisons rather than n. The trade-off is that the data must be sorted first.
在有序列表上,二分查找比线性查找更快,因为它每次把查找范围减半,大约需要 log₂ n 次比较而非 n 次。代价是数据必须事先排好序。
11. Structured and Modular Design | 结构化与模块化设计
Structured programming builds a solution from just three constructs: sequence, selection and iteration. Any algorithm, however complex, can be expressed with these three plus subprograms, which is why exam pseudocode stays within them.
结构化程序设计仅用三种构造来搭建解法:顺序、选择和迭代。无论多复杂的算法,都可以用这三种构造加上子程序来表达,这正是考试伪代码始终不超出这一范围的原因。
Top-down design starts with the overall problem and repeatedly decomposes it into smaller subproblems until each one is simple enough to code directly. The result is a hierarchy chart showing which module calls which.
自顶向下设计从整体问题出发,不断把它分解为更小的子问题,直到每个子问题都简单到可以直接编码。其成果是一张层次图,显示哪个模块调用哪个模块。
Abstraction: hide unnecessary detail so the reader sees only what matters at that level.
抽象:隐藏不必要的细节,使读者只看到该层次真正重要的内容。
Information hiding: each module exposes only the interface other modules need.
信息隐藏:每个模块只暴露其他模块所需的接口。
Stepwise refinement: describe the solution in outline first, then add detail gradually.
逐步求精:先给出解法的框架,再逐步补充细节。
更多咨询请联系16621398022(同微信)

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