📚 General-Purpose Features of Procedural Languages | A-Level计算机:过程式语言通用功能解析
Procedural programming languages are built around the idea of a sequence of instructions executed in order. They provide a set of general-purpose features that allow programmers to design clear, modular, and maintainable solutions to problems.
过程式编程语言的核心思想是按顺序执行一系列指令。它们提供了一套通用功能,使程序员能够设计清晰、模块化且易于维护的问题解决方案。
1. Variables and Constants | 变量与常量
Variables are named memory locations whose values can change during program execution. In procedural languages, a variable must often be declared before use, specifying its data type and sometimes an initial value.
变量是命名的内存位置,其值在程序执行过程中可以改变。在过程式语言中,变量通常需要在使用前声明,并指定其数据类型,有时还需指定初始值。
Constants are similar but their values cannot be changed after initial assignment. Using constants improves readability and makes it easier to update values in one place.
常量与变量类似,但常量在初始赋值后其值不能被改变。使用常量可以提高程序可读性,并且便于在某一处统一更新数值。
DECLARE score : INTEGER
DECLARE PI : REAL = 3.14159
2. Data Types | 数据类型
Data types define the kind of values a variable can hold and the operations that can be performed on it. Common primitive types include:
数据类型定义了变量可保存的值的种类以及可以对其执行的操作。常见的基本类型包括:
- INTEGER – whole numbers | 整数
- REAL – numbers with fractional parts | 实数(含小数)
- CHAR – a single character | 单个字符
- STRING – a sequence of characters | 字符串
- BOOLEAN – TRUE or FALSE | 布尔值(真或假)
Choosing the correct data type is essential for memory efficiency and avoiding errors such as overflow or type mismatch.
选择正确的数据类型对于内存效率以及避免溢出或类型不匹配等错误至关重要。
3. Assignment and Expressions | 赋值与表达式
Assignment stores a value into a variable. The assignment operator is usually written as = or ← in pseudocode, and it evaluates the right-hand side before storing the result.
赋值是将一个值存入变量。赋值运算符在伪代码中通常写作 = 或 ←,它先计算右侧表达式,再存储结果。
Expressions combine variables, constants, operators, and function calls to produce new values. Arithmetic expressions use operators such as +, -, *, /, and MOD or DIV.
表达式将变量、常量、运算符和函数调用组合起来,产生新的值。算术表达式使用 +、-、*、/、MOD 或 DIV 等运算符。
total ← price × quantity + deliveryCost
4. Arithmetic and Logical Operators | 算术与逻辑运算符
Arithmetic operators perform mathematical calculations. Logical operators connect Boolean expressions and produce Boolean results.
算术运算符执行数学计算。逻辑运算符连接布尔表达式并产生布尔结果。
- AND – TRUE only if both operands are TRUE | 仅当两个操作数均为真时为真
- OR – TRUE if at least one operand is TRUE | 至少一个操作数为真即为真
- NOT – reverses the Boolean value | 反转布尔值
Relational operators such as =, <>, <, >, <=, >= compare values and return a Boolean result.
关系运算符如 =、<>、<、>、<=、>= 用于比较值并返回布尔结果。
5. Input and Output | 输入与输出
Programs interact with users through input and output statements. Input statements read data from a keyboard or file and store it in variables; output statements display results to the screen or write them to a file.
程序通过与用户的交互完成输入和输出。输入语句从键盘或文件读取数据并存入变量;输出语句将结果显示到屏幕或写入文件。
INPUT name
OUTPUT “Hello, ” + name
Formatted output is important for readability, especially when printing numbers with a specific number of decimal places.
格式化输出对于可读性很重要,特别是在按指定位数打印小数时。
6. Selection (IF and CASE) | 选择结构(IF 与 CASE)
Selection allows a program to choose between different paths of execution based on a condition. The IF...THEN...ELSE structure executes one block if the condition is TRUE and another if it is FALSE.
选择结构允许程序根据条件在不同的执行路径之间做出选择。IF...THEN...ELSE 结构在条件为真时执行一个语句块,为假时执行另一个语句块。
The CASE statement is a concise way to select among several discrete values, avoiding deeply nested IF statements.
CASE 语句是在多个离散值之间进行选择的简洁方式,可避免深层嵌套的 IF 语句。
IF mark >= 50 THEN
OUTPUT “Pass”
ELSE
OUTPUT “Fail”
ENDIF
7. Iteration (FOR, WHILE, REPEAT) | 迭代结构(FOR、WHILE、REPEAT)
Iteration, also known as looping, repeats a block of statements. A FOR loop runs a fixed number of times, controlled by a counter.
迭代,也称为循环,用于重复执行一组语句。FOR 循环通过计数器控制,运行固定的次数。
A WHILE loop checks a condition before each iteration; if the condition is initially FALSE, the loop body may never execute.
WHILE 循环在每次迭代之前检查条件;如果条件一开始为假,则循环体可能一次都不会执行。
A REPEAT...UNTIL loop checks the condition after the body, so it always executes at least once.
REPEAT...UNTIL 循环在循环体之后检查条件,因此它至少会执行一次。
FOR i ← 1 TO 10
OUTPUT i
ENDFOR
8. Procedures and Functions | 过程与函数
Procedures and functions are named blocks of code that can be called from elsewhere in the program. They implement modularity, making programs easier to write, test, and maintain.
过程与函数是命名的代码块,可以从程序的其他位置调用。它们实现了模块化,使程序更易于编写、测试和维护。
- Procedure – performs a task but does not return a value | 执行任务但不返回值
- Function – returns a single value and can be used within expressions | 返回一个值,可在表达式中使用
9. Parameter Passing | 参数传递
Parameters allow data to be passed into a procedure or function. Two common mechanisms are by value and by reference.
参数允许将数据传入过程或函数。两种常见的传递机制是按值传递和按引用传递。
| By Value | By Reference |
| A copy of the argument is passed | 传递参数的副本 | The address of the argument is passed | 传递参数的地址 |
| Changes do not affect the original | 修改不影响原始值 | Changes affect the original variable | 修改会影响原始变量 |
| Suitable for small data or read-only data | 适合小数据或只读数据 | Suitable for large data or when modification is needed | 适合大数据或需要修改时 |
Choosing the correct parameter passing mechanism is essential to avoid unintended side effects and to manage memory efficiently.
选择正确的参数传递机制对于避免意外副作用以及高效管理内存至关重要。
10. Local Variables and Scope | 局部变量与作用域
Variables declared inside a procedure or function are local to that block: they cannot be accessed outside it. Variables declared at the top level are global and can be accessed by all parts of the program.
在过程或函数内部声明的变量是该代码块的局部变量:它们无法在块外部访问。在顶层声明的变量是全局变量,可以被程序的所有部分访问。
Using local variables reduces the risk of naming conflicts and makes code more self-contained. However, excessive use of global variables can make debugging difficult and create hidden dependencies.
使用局部变量可以降低命名冲突的风险,并使代码更加独立。然而,过多使用全局变量会使调试变得困难,并产生隐藏的依赖关系。
11. Arrays and Records | 数组与记录
An array is a collection of elements of the same data type, accessed by an index. It is ideal for storing lists of data.
数组是相同数据类型元素组成的集合,通过下标访问。它非常适合存储数据列表。
DECLARE scores : ARRAY[1:5] OF INTEGER
A record is a composite data type that groups together different fields, each of which can have a different type. Records are used to represent real-world entities with multiple attributes.
记录是一种复合数据类型,将不同字段组合在一起,每个字段可以有不同的类型。记录用于表示具有多个属性的现实世界实体。
TYPE Student
DECLARE name : STRING
DECLARE age : INTEGER
DECLARE grade : CHAR
ENDTYPE
12. Structured Programming and Modularity | 结构化编程与模块化
Procedural languages encourage structured programming: programs are built from three basic control structures — sequence, selection, and iteration. This approach eliminates the need for GOTO statements and produces clearer, more reliable code.
过程式语言鼓励结构化编程:程序由三种基本控制结构组成——顺序、选择和迭代。这种方法避免了使用 GOTO 语句,从而生成更清晰、更可靠的代码。
Modularity means breaking a large problem into smaller sub-problems, each solved by a procedure or function. Each module has a single purpose and a clear interface, which improves testing, reuse, and team collaboration.
模块化意味着将一个大问题分解为较小的子问题,每个子问题由一个过程或函数解决。每个模块具有单一职责和清晰的接口,从而提高测试效率、代码复用和团队协作。
By mastering these general-purpose features, students can write efficient, readable, and maintainable programs in any procedural language.
掌握这些通用功能后,学生便能在任何过程式语言中编写高效、可读且易于维护的程序。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导