Analysis of Basic Program Control Structures | 程序基本控制结构解析

📚 Analysis of Basic Program Control Structures | 程序基本控制结构解析

In nearly every A-Level Computer Science syllabus, program control structures form the foundation of algorithmic thinking. Whether you are writing pseudocode for Paper 2 or debugging code in a high-level language, mastering sequence, selection, and iteration is essential for earning full marks. This article unpacks the three fundamental control structures, explains their pseudocode forms, and highlights common examination traps.

在几乎所有 A-Level 计算机科学考纲中,程序控制结构都是算法思维的基础。无论你是在卷二上编写伪代码,还是在高级语言中调试程序,掌握顺序、选择和循环都是拿满分的必要条件。本文深度解析三大基本控制结构,说明其伪代码写法,并指出常见考试陷阱。


1. The Three Fundamental Structures | 三种基本结构

The famous theorem by Böhm and Jacopini (1966) proved that any computable problem can be solved using only three control structures: sequence, selection, and iteration. No program, however complex, requires anything beyond these three building blocks. This insight allows examiners to test algorithmic logic in a language-independent way through pseudocode.

Böhm 与 Jacopini 于 1966 年提出的著名定理证明:任何可计算问题都仅需顺序、选择、循环三种控制结构即可求解。无论多么复杂的程序,都不需要超出这三种构件。这一结论使考官能够通过伪代码以不依赖具体语言的方式考查算法逻辑。

The table below summarises the three structures and their characteristics:

下表概述了三种结构及其特征:

Structure | 结构 Key feature | 关键特征 Pseudocode keyword | 伪代码关键字
Sequence | 顺序 Executes in order, top to bottom | 自顶向下依次执行 None (implicit) | 无(隐含)
Selection | 选择 Chooses one path among several | 在多个路径中择一执行 IF, CASE
Iteration | 循环 Repeats a block zero or more times | 重复执行代码块多次 FOR, WHILE, REPEAT

2. Sequence Structure | 顺序结构

Sequence is the simplest structure: statements are executed one after another in the exact order they appear. Each statement completes before the next begins. In pseudocode, a sequence is simply written as consecutive lines, and examiners expect you to trace through them in order, updating variable values line by line.

顺序结构是最简单的结构:语句严格按照出现的先后次序逐条执行。每条语句完成之后,下一条语句才开始。在伪代码中,顺序结构就是连续的几行代码;考官期望你按顺序逐行追踪,逐句更新变量值。

Consider the following example:

看下面的例子:

x ← 5
y ← x + 2
x ← y × 3
OUTPUT x

The output is 21, not 15. Many students incorrectly treat x as ‘5’ throughout the program. In a sequence, x becomes 7 after the second line, then 21 after the third. Tracing tables remain the best tool for this kind of question.

输出结果是 21,而不是 15。许多学生错误地在整个程序中把 x 当作始终等于 5。在顺序结构中,执行第二行后 x 变为 7,第三行后变为 21。对这种题目,追踪表仍然是最好的工具。


3. Selection Structure — IF Statements | 选择结构 — IF 语句

Selection allows a program to make decisions. The simplest form is IF-THEN, where a condition is evaluated as TRUE or FALSE. A condition is a boolean expression using relational operators such as = , ≠ , < , > , ≤ , ≥ , and can be combined with AND, OR, NOT.

选择结构让程序能够做出判断。最简单的形式是 IF-THEN,条件被求值为 TRUE 或 FALSE。条件是用关系运算符(如 =、≠、<、>、≤、≥)构成的布尔表达式,还可以用 AND、OR、NOT 进行组合。

The classic structure in Cambridge-style pseudocode is:

剑桥风格伪代码的经典结构是:

IF <condition> THEN
<statements>
ELSE
<statements>
ENDIF

The ELSE branch is optional; if a condition is FALSE and there is no ELSE, control simply moves to the statement after ENDIF. Every IF must be terminated by ENDIF in formal pseudocode, otherwise marks are lost for missing delimiters.

ELSE 分支是可选的;若条件为 FALSE 且没有 ELSE,控制流将直接跳到 ENDIF 之后的语句。在正式伪代码中,每个 IF 都必须以 ENDIF 结束,否则会因缺少定界符而丢分。

A nested IF occurs when an IF statement appears inside another IF statement. Nesting allows multi-way decisions. However, deeply nested IFs become hard to read; examiners often expect you to replace them with CASE where appropriate.

当一个 IF 语句出现在另一个 IF 语句内部时,就形成了嵌套 IF。嵌套可以实现多路判断。但深层嵌套的 IF 可读性差,考官通常期望你在适当场合改用 CASE 结构。


4. Selection Structure — CASE Statements | 选择结构 — CASE 语句

When a variable must be compared against many distinct values, the CASE statement is cleaner than a long chain of IF-ELSE-IF. In formal pseudocode, CASE evaluates an expression once and matches it against a list of values.

当需要把某个变量与多个不同取值比较时,CASE 语句比一长串 IF-ELSE-IF 更清晰。在正式伪代码中,CASE 只对表达式求值一次,并将其与一组取值逐一匹配。

CASE OF grade
‘A’ : OUTPUT ‘Excellent’
‘B’ : OUTPUT ‘Good’
‘C’ : OUTPUT ‘Average’
OTHERWISE : OUTPUT ‘Try harder’
ENDCASE

OTHERWISE acts as a default branch, similar to ELSE in an IF statement. Note that CASE must be closed by ENDCASE, just as IF requires ENDIF. In some programming languages the equivalent keyword is SWITCH, but in CIE/CAIE and Pearson Edexcel pseudocode, CASE OF … ENDCASE is the accepted standard.

OTHERWISE 充当默认分支,类似 IF 语句中的 ELSE。注意,CASE 必须以 ENDCASE 结束,正如 IF 必须以 ENDIF 结束一样。在有些编程语言中对应关键字是 SWITCH,但在 CIE/CAIE 和 Pearson Edexcel 的伪代码中

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