📚 Three Basic Structures of Program Code | 程序代码的三种基本结构
Every computer program, no matter how complex, is built from only three fundamental control structures: sequence, selection, and repetition (loop). These structures were formally described by Böhm and Jacopini in 1966, who proved that any computable function can be expressed using only these three building blocks.
任何计算机程序,无论多么复杂,都仅由三种基本控制结构构成:顺序结构、选择结构和循环结构。1966 年,Böhm 和 Jacopini 正式阐述了这些结构,并证明了任何可计算函数都可以仅用这三种基本构件来表达。
1. Sequence Structure | 顺序结构
In a sequence structure, statements are executed one after another in the exact order they appear in the code. There is no branching and no skipping; each instruction runs exactly once, from top to bottom.
在顺序结构中,语句按照它们在代码中出现的顺序依次执行。没有分支,也没有跳跃;每条指令从上到下恰好执行一次。
Example in pseudocode:
INPUT a, b
sum ← a + b
OUTPUT sum
Here, the value of a is read first, then b, then the sum is calculated, and finally the result is printed. Each step depends on the previous one, forming a linear flow.
这里,先读入 a 的值,再读入 b,然后计算和,最后输出结果。每一步都依赖于前一步,形成线性流程。
2. Selection Structure | 选择结构
A selection structure allows the program to choose between different paths based on a condition. The most common forms are if-then, if-then-else, and switch/case. The condition is evaluated as either true or false.
选择结构允许程序根据条件在不同路径之间进行选择。最常见的形式有 if-then、if-then-else 和 switch/case。条件会被判断为真或假。
Example:
IF age ≥ 18 THEN
OUTPUT “Adult”
ELSE
OUTPUT “Minor”
END IF
When the condition age ≥ 18 is true, the program prints “Adult”; otherwise it prints “Minor”. Exactly one branch is executed.
当条件 age ≥ 18 为真时,程序输出 “Adult”;否则输出 “Minor”。只有一个分支会被执行。
3. Repetition (Loop) Structure | 循环结构
A loop structure repeats a block of code as long as a given condition holds, or for a fixed number of iterations. The two main types are while loops (condition checked before the body) and for loops (usually with a counter).
循环结构在给定条件成立时重复执行一段代码,或者重复固定次数。两种主要类型是 while 循环(在执行循环体之前检查条件)和 for 循环(通常带有计数器)。
While loop example:
WHILE count < 5 DO
OUTPUT count
count ← count + 1
END WHILE
This prints the numbers 0, 1, 2, 3, 4. The loop stops when count reaches 5.
这段代码会输出 0、1、2、3、4。当 count 达到 5 时循环停止。
4. Flowchart Representation | 流程图表示
Flowcharts visually represent the three structures. A rectangle is used for a process/statement, a diamond for a decision, and arrows show the flow direction. Circles or ovals mark start and end.
流程图直观地表示这三种结构。矩形表示处理/语句,菱形表示判断,箭头表示流程方向。圆角矩形或椭圆标记开始和结束。
| Structure | Flowchart Pattern |
| Sequence | Rectangle → Rectangle → Rectangle |
| Selection | Diamond with two outgoing arrows (Y/N) |
| Loop | Backward arrow from body to condition |
In a loop flowchart, the condition is checked before the body, and if it remains true, the flow loops back. This is why a loop contains an arrow that goes backward.
在循环流程图中,条件在循环体之前检查,如果仍为真,流程就会返回。因此循环中包含一条向后指的箭头。
5. Pseudocode Conventions | 伪代码约定
Pseudocode is a human-readable description of an algorithm that uses the logic of programming languages without strict syntax. Common keywords include IF, THEN, ELSE, WHILE, FOR, REPEAT, UNTIL, INPUT, OUTPUT.
伪代码是一种人类可读的算法描述,它使用编程语言的逻辑而不拘泥于严格的语法。常用关键字包括 IF、THEN、ELSE、WHILE、FOR、REPEAT、UNTIL、INPUT、OUTPUT。
Indentation is often used to show which statements belong inside a loop or selection. For example:
FOR i ← 1 TO 10
OUTPUT i * i
NEXT i
The indented line OUTPUT i * i is inside the loop; the loop repeats ten times.
缩进的一行 OUTPUT i * i 在循环体内;该循环重复十次。
6. Nesting Structures | 结构嵌套
The three basic structures can be combined inside one another. For example, a selection structure can appear inside a loop, and a loop can appear inside another loop (nested loops). This makes it possible to solve very complex problems.
三种基本结构可以互相嵌套。例如,选择结构可以出现在循环内,循环也可以出现在另一个循环内(嵌套循环)。这使得我们能够解决非常复杂的问题。
Example of a selection inside a loop:
FOR n ← 1 TO 5
IF n MOD 2 = 0 THEN
OUTPUT n, ” is even”
ELSE
OUTPUT n, ” is odd”
END IF
NEXT n
The loop runs five times; each time it enters the IF structure to decide whether the current number is even or odd.
循环执行五次;每次进入 IF 结构来判断当前数字是偶数还是奇数。
7. Structured Programming Principles | 结构化编程原则
The three structures support structured programming, which discourages the use of GOTO statements. Restricting code to these structures makes programs easier to read, test, and maintain.
这三种结构支持结构化编程,它不鼓励使用 GOTO 语句。将代码限制在这些结构内,可以使程序更容易阅读、测试和维护。
Key benefits include:
-
Each structure has one entry point and one exit point, making reasoning about code simpler. | 每个结构只有一个入口和一个出口,使代码推理更加简单。
-
Program flow follows a top-down design, so debugging is more systematic. | 程序流程遵循自顶向下设计,因此调试更加系统化。
-
Code can be reused and modified without unintended side effects. | 代码可以复用和修改,而不会产生意外的副作用。
8. Common Errors in Using These Structures | 使用这些结构时的常见错误
Students often make mistakes when translating logic into code. Recognizing these errors is a key exam skill.
学生在将逻辑转化为代码时常犯错误。识别这些错误是一项关键的考试技能。
-
Infinite loop: forgetting to update the loop counter, so the condition never becomes false. | 死循环:忘记更新循环计数器,导致条件永远不会变为假。
-
Off-by-one error: using ≤ instead of <, or setting a counter from 0 instead of 1, causing one extra or missing iteration. | 差一错误:使用 ≤ 而不是 <,或将计数器从 0 而不是 1 开始,导致多一次或少一次迭代。
-
Missing ELSE: assuming a value is changed when the condition is false, but no ELSE branch exists. | 缺少 ELSE:假设条件为假时值会被改变,但实际上没有 ELSE 分支。
-
Wrong nesting: placing END IF or END WHILE in the wrong position, altering the meaning of the code. | 嵌套错误:将 END IF 或 END WHILE 放错位置,改变了代码的含义。
9. Equivalence of the Three Structures | 三种结构的等价性
Böhm and Jacopini’s theorem states that any algorithm that can be written with recursion, GOTO, or other complex control flows can also be rewritten using only sequence, selection, and loop. This means these three structures are sufficient for all programming tasks.
Böhm 和 Jacopini 定理指出,任何可以用递归、GOTO 或其他复杂控制流编写的算法,都可以只用顺序、选择和循环重写。这意味着这三种结构对于所有编程任务都是充分的。
In practice, modern languages add extra conveniences (such as switch or foreach), but they can all be reduced to the three basic forms. Understanding this equivalence helps you design algorithms more flexibly.
在实际中,现代语言添加了额外的便利结构(如 switch 或 foreach),但它们都可以归结为这三种基本形式。理解这种等价性有助于你更灵活地设计算法。
10. Real-World Analogies | 现实世界类比
These structures appear in everyday life. A recipe is a sequence: mix ingredients, bake, serve. A route choice is a selection: if traffic is heavy, take another road. Washing clothes is a loop: repeat the rinse cycle until the water is clear.
这些结构在日常生活中很常见。一份食谱就是顺序:混合食材、烘烤、上桌。路线选择就是选择:如果交通拥堵,就走另一条路。洗衣服就是循环:重复漂洗,直到水变清。
By seeing algorithms in real life, you can better translate them into program code. Whenever you write code, ask yourself: which of the three structures does this action require?
通过在生活中观察算法,你可以更好地将它们转化为程序代码。每当你写代码时,问自己:这个动作需要三种结构中的哪一种?
11. Exam Tips for Identifying Structures | 考试识别技巧
In exam questions, you may be asked to read pseudocode and count how many times a statement executes, or to write the output of a loop. Here are useful tips.
在考试题目中,你可能会被要求阅读伪代码并统计某条语句执行多少次,或者写出循环的输出。以下是一些有用的技巧。
-
Trace the values of all variables in a table. | 用表格追踪所有变量的值。
-
Identify whether the condition is checked before or after the loop body. | 判断条件是在循环体之前还是之后检查。
-
Remember that in a WHILE loop, if the condition is initially false, the body may never run. | 记住,在 WHILE 循环中,如果条件初始为假,循环体可能一次也不执行。
-
For a FOR loop, count exactly how many values the control variable takes. | 对于 FOR 循环,准确计算控制变量取多少个值。
Practice by writing small programs using all three structures, then trace their execution step by step.
通过编写包含三种结构的小程序来练习,然后逐步追踪它们的执行过程。
12. Summary | 总结
Sequence, selection, and repetition are the three building blocks of all programs. Sequence is the default linear flow; selection introduces branches; repetition creates loops. Mastering these structures is the first major step in learning any programming language.
顺序、选择和重复是所有程序的三大构件。顺序是默认的线性流程;选择引入分支;重复产生循环。掌握这些结构是学习任何编程语言的第一步,也是最重要的一步。
Remember the golden rule: any algorithm can be expressed with these three structures alone. Use them deliberately, and your code will be clear, reliable, and easy to maintain.
记住黄金法则:任何算法都可以仅用这三种结构来表达。有意识地使用它们,你的代码将清晰、可靠且易于维护。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply