📚 The Structure of Procedural Programs | 过程式程序的结构
Procedural programming is one of the most widely used programming paradigms in Cambridge IGCSE and A Level Computer Science. It organises code into a clear sequence of instructions, decisions, loops, and reusable blocks called procedures or functions. Understanding its structure is essential for writing correct, maintainable, and exam-ready pseudocode and real programs.
过程式编程是剑桥 IGCSE 和 A Level 计算机科学中使用最广泛的编程范式之一。它将代码组织成清晰的指令顺序、判断、循环以及称为过程或函数的可重用代码块。理解其结构对于编写正确、易维护且符合考试要求的伪代码和真实程序至关重要。
1. Procedural Programming: The Core Idea | 过程式编程的核心思想
A procedural program is built from a series of instructions that are executed in a controlled order. The three fundamental control structures are sequence, selection, and iteration. These structures allow a programmer to solve problems step by step, with procedures and functions adding modularity and reuse. Procedural languages include Python, Pascal, C, and Visual Basic.
过程式程序由一系列按受控顺序执行的指令构成。三种基本控制结构是顺序、选择和迭代。这些结构使程序员能够逐步解决问题,而过程和函数则增加了模块化与可重用性。过程式语言包括 Python、Pascal、C 和 Visual Basic 等。
- Sequence: statements run one after another.
- Selection: statements run only if a condition is true.
- Iteration: statements repeat while or until a condition changes.
顺序:语句逐条执行;选择:仅当条件为真时执行语句;迭代:在条件改变前重复执行语句。
2. Sequence: Step-by-Step Execution | 顺序结构:逐步执行
a rectangle uses three sequential steps: input length, input width, output length × width.
在顺序结构中,每条语句严格按照其出现的先后顺序执行。除非使用其他结构,否则不会跳过或重复。例如,一个计算矩形面积的程序使用三个顺序步骤:输入长、输入宽、输出 长 × 宽。
area = length × width
This simple formula shows that the order of operations matters: the two input statements must come before the output statement. In pseudocode, sequence is represented by writing one statement below another without any indentation change.
这个简单的公式表明操作顺序很重要:两条输入语句必须位于输出语句之前。在伪代码中,顺序结构用一条语句写在另一条下面且不改变缩进来表示。
3. Selection: Making Decisions | 选择结构:做出决策
Selection allows a program to choose between two or more paths. The most common forms are IF...THEN...ELSE...ENDIF and CASE...OF...OTHERWISE...ENDCASE. Selection depends on a Boolean condition that evaluates to TRUE or FALSE.
选择结构允许程序在两条或多条路径之间做出选择。最常见的形式是 IF...THEN...ELSE...ENDIF 和 CASE...OF...OTHERWISE...ENDCASE。选择依赖于一个布尔条件,该条件的结果为 TRUE 或 FALSE。
IF score >= 60 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
If the variable score is 75, the condition score >= 60 is TRUE, so the program outputs "Pass". If score is 45, the condition is FALSE, so it outputs "Fail" ut a CASE statement is often cleaner when one variable is compared against several values.
如果变量 score 为 75,条件 score >= 60 为 TRUE,程序输出 "Pass"。如果 score 为 45,条件为 FALSE,则输出 "Fail"。嵌套 IF 语句可以处理两个以上的情况,但当将一个变量与多个值进行比较时,CASE 语句通常更清晰。
4. Iteration: Repeating Code | 迭代结构:重复代码
Iteration repeats a block of code. The three main loop types are count-controlled (FOR), pre-condition (WHILE), and post-condition (REPEAT...UNTIL). Choosing the correct loop is a common exam requirement.
迭代重复执行一段代码。三种主要循环类型是计数控制循环(FOR)、前置条件循环(WHILE)和后置条件循环(REPEAT...UNTIL)。选择正确的循环类型是考试中的常见要求。
| Loop | When to use |
| FOR | Known number of repetitions |
| WHILE | Zero or more repetitions, condition checked first |
| REPEAT...UNTIL | At least one repetition, condition checked last |
For example, use a FOR loop to output the first ten square numbers because the count is known. Use a WHILE loop to read records until the end of a file because there may be zero records. Use a REPEAT...UNTIL loop to display a menu at least once before checking the user's choice.
例如,使用 FOR 循环输出前十个平方数,因为次数已知。使用 WHILE 循环读取记录直到文件末尾,因为可能没有记录。使用 REPEAT...UNTIL 循环至少显示一次菜单,然后再检查用户的选择。
5. Procedures and Functions: Named Blocks | 过程与函数:命名代码块
A procedure is a named block of code that performs a task but does not return a value. A function is similar but always returns a single value. Both can be called from the main program, reducing duplication and improving readability.
过程是执行某个任务但不返回值的命名代码块。函数与之类似,但总是返回一个值。两者都可以从主程序中调用,从而减少重复并提高可读性。
PROCEDURE DisplayTotal(Total)
OUTPUT "Total is ", Total
ENDPROCEDURE
FUNCTION Max(a, b) RETURNS INTEGER
IF a > b THEN
RETURN a
ELSE
RETURN b
ENDIF
ENDFUNCTION
The procedure DisplayTotal outputs a message but does not give a result back to the caller. The function Max compares two integers and returns the larger one, so it can be used in an expression such as x ← Max(num1, num2).
过程 DisplayTotal 输出一条消息,但不向调用者返回结果。函数 Max 比较两个整数并返回较大的一个,因此它可以用在表达式 x ← Max(num1, num2) 中。
6. Parameters: Passing Data In and Out | 参数:数据的传入与传出
Parameters allow data to be passed into a procedure or function. Cambridge pseudocode often distinguishes between passing by value and passing by reference. With by value, the called routine gets a copy; with by reference, it can modify the original variable.
参数允许将数据传递给过程或函数。剑桥伪代码通常区分按值传递和按引用传递。按值传递时,被调用的例程获得副本;按引用传递时,它可以修改原始变量。
| Passing method | Effect on original variable |
| By value | No change |
| By reference | Can be changed |
In the Cambridge pseudocode, parameters are passed by value unless the keyword BYREF is used. This distinction matters when a procedure needs to update a variable in the main program, such as incrementing a counter.
在剑桥伪代码中,除非使用关键字 BYREF,否则参数按值传递。当过程需要更新主程序中的变量(例如递增计数器)时,这一区别非常重要。
7. Local and Global Variables: Scope Rules | 局部变量与全局变量:作用域规则
Scope determines where a variable can be accessed. A local variable is declared inside a procedure or function and exists only while that routine runs. A global variable is declared outside all routines and can be used anywhere in the program.
作用域决定变量可以在哪里被访问。局部变量在过程或函数内部声明,并且仅在该例程运行时存在。全局变量在所有例程之外声明,可以在程序的任何位置使用。
Local variables are usually preferred because they limit side effects and make modules more independent. Overusing global variables can lead to hard-to-find bugs, because any routine may change the value unexpectedly.
局部变量通常更受青睐,因为它们限制了副作用并使模块更加独立。过度使用全局变量可能导致难以发现的错误,因为任何例程都可能意外地改变其值。
GLOBAL counter counter ← 0 PROCEDURE Increment() counter ← counter + 1 ENDPROCEDURE
In this example, counter is global, so the procedure Increment can access and modify it directly. If counter were local to the main program, the procedure would need a parameter or a return value to update it.
在此示例中,counter 是全局变量,因此过程 Increment 可以直接访问并修改它。如果 counter 是主程序的局部变量,过程就需要参数或返回值来更新它。
8. Modular Programming and Top-Down Design | 模块化编程与自顶向下设计
Modular programming breaks a large problem into smaller, manageable sub-tasks. Top-down design starts with the overall goal and repeatedly divides it into smaller modules until each module is simple enough to code. This approach supports teamwork, testing, and maintenance.
模块化编程将大型问题分解为更小、更易于管理的子任务。自顶向下设计从整体目标开始,反复将其划分为更小的模块,直到每个模块都简单到可以直接编码。这种方法支持团队合作、测试和维护。
- Easier debugging: isolated modules can be tested independently.
- Code reuse: one module can be called many times.
- Readability: the main program becomes a short summary of calls.
更易于调试:独立模块可以单独测试;代码重用:一个模块可被多次调用;可读性:主程序变成模块调用的简短摘要。
A well-structured main program might look like a list of high-level steps: InputData(), ProcessData(), OutputResults(). Each of these modules is then refined separately.
一个结构良好的主程序看起来像一系列高层步骤:InputData()、ProcessData()、OutputResults()。然后分别对每个模块进行细化。
9. Stepwise Refinement and Pseudocode | 逐步求精与伪代码
Stepwise refinement is the process of expanding each high-level module into more detailed pseudocode until every instruction is clear. Pseudocode is a structured, language-independent way of
Published by TutorHao | 体育 Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

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