📚 A-Level OCR Computer Science: Programming Fundamentals Revision Guide | A-Level OCR 计算机科学:编程基础考点精讲
In A-Level OCR Computer Science, programming fundamentals form the backbone of Component 02: Algorithms and Programming. This section covers essential constructs such as variables, data types, control structures, subroutines, parameter passing, scope, recursion, and the use of an IDE. Mastering these concepts is crucial not only for the written examination but also for the practical programming project (Component 03). This revision guide breaks down each key topic with clear explanations, pseudocode examples and exam-focused tips.
在 A-Level OCR 计算机科学课程中,编程基础是 Component 02 "算法与编程" 部分的核心。本节涵盖变量、数据类型、控制结构、子程序、参数传递、作用域、递归以及集成开发环境的使用等重要内容。掌握这些概念不仅对笔试至关重要,对实践编程项目(Component 03)也同样关键。本考点精讲将逐一解析每个重点主题,并辅以清晰的解释、伪代码示例与应试技巧。
1. Variables, Constants and Data Types | 变量、常量与数据类型
Variables are named storage locations whose values can change during program execution. A variable must be declared with a specific data type such as Integer, Real, String, Boolean, Char, String or Date. The data type determines the kind of data that can be stored, the amount of memory reserved, and the operations that can legally be performed on the value.
变量是命名的存储位置,其值在程序执行期间可以改变。变量必须声明为特定的数据类型,如整型(Integer)、实型(Real)、布尔型(Boolean)、字符型(Char)、字符串(String)或日期型(Date)。数据类型决定了所能存储的数据种类、预留的内存大小,以及可以合法对该值执行的操作。
Constants are similar to variables but their values remain fixed throughout the program. They are typically declared with a special keyword (CONST in OCR pseudocode) and are used to avoid magic numbers, making code more readable, easier to maintain and less error-prone when a value needs updating in several places.
常量与变量类似,但其值在整个程序运行过程中保持不变。常量通常使用特殊关键字声明(OCR 伪代码中为 CONST),用于避免使用 "魔数",从而提高代码的可读性与可维护性,并在需要多处修改同一数值时减少出错风险。
| Data type | 数据类型 | Typical use | 典型用途 |
| Integer 整型 | Whole numbers, counters, array indexes 整数、计数器、数组下标 |
| Real / Float 实型 | Decimals, averages, measurements 小数、平均值、测量值 |
| Boolean 布尔型 | TRUE / FALSE flags, conditions 真/假标志、条件判断 |
| Char 字符型 | A single character such as 'A' or '7' 单个字符 |
| String 字符串 | Names, messages, text input 姓名、消息、文本输入 |
| Date 日期型 | Dates for validation or sorting 用于校验或排序的日期 |
OCR pseudocode examples of declaration and assignment:
- DECLARE score : INTEGER
- DECLARE average : REAL
- DECLARE is_valid : BOOLEAN
- CONST PI = 3.14159
- score ← 0
OCR 伪代码中声明与赋值的写法示例:
- DECLARE score : INTEGER(声明一个整型变量)
- DECLARE average : REAL(声明一个实型变量)
- DECLARE is_valid : BOOLEAN(声明一个布尔变量)
- CONST PI = 3.14159(声明常量 PI)
- score ← 0(把 0 赋给 score,注意赋值箭头是 ←)
Many high-level languages are strongly typed, meaning a variable's type cannot be changed after declaration, and operations must be type-compatible. Explicit casting functions such as INT(), REAL(), STR() and ASC()/CHR() allow values to be converted between types, and OCR frequently tests whether candidates know that dividing two integers in pseudocode can still produce a real result, and that STR() is needed before concatenating a number with text.
许多高级语言是强类型的,即变量的类型在声明后不能更改,操作必须类型兼容。显式转换函数如 INT()、REAL()、STR() 以及 ASC()/CHR() 可以在不同类型之间转换值。OCR 常考查的细节包括:伪代码中两个整数相除仍可能得到实型结果;在把数字与文本拼接前必须先用 STR() 转换。
2. Sequence, Selection and Iteration | 顺序、选择与迭代
All programs are built from three fundamental constructs. Sequence means statements executed one after another in order. Selection means choosing a path based on a condition. Iteration means repeating a block of statements. Examiners expect you to combine these three correctly when writing pseudocode answers.
所有程序都由三种基本结构构成。顺序(Sequence)指语句按先后次序依次执行;选择(Selection)指根据条件选择执行路径;迭代(Iteration)指重复执行一段语句。考官期望你在写伪代码答案时能正确组合这三种结构。
<, >, <=, >= and Boolean operators AND, OR, NOT. Remember that <> means "not equal to", and that nested IF statements must each have their own ENDIF.
OCR 伪代码中的选择结构使用 IF、THEN、ELSE 与 ELSEIF,并以 ENDIF 结束。条件使用比较运算符 =、<>、<、>、<=、>= 以及布尔运算符 AND、OR、NOT。请记住 <> 表示 "不等于",并且每一层嵌套的 IF 都必须有各自对应的 ENDIF。
Iteration has three forms worth memorising. A FOR loop is a count-controlled loop used when the number of repetitions is known in advance. A WHILE loop is a condition-controlled loop that is checked before the body runs, so it may execute zero times. A REPEAT...UNTIL loop is also condition-controlled, but the condition is tested after the body, so it always runs at least once.
迭代有三种形式值得牢记。FOR 循环是计数控制循环,适用于循环次数预先已知的情况;WHILE 循环是条件控制循环,条件在循环体之前判断,因此循环体可能一次都不执行;REPEAT...UNTIL 同样是条件控制循环,但条件在循环体之后判断,因此循环体至少执行一次。
FOR i ← 1 TO 10 ... NEXT i | WHILE x < 100 ... ENDWHILE | REPEAT ... UNTIL x >= 100
Choosing the wrong loop is a common error. If the question says "while there are still records to read", a FOR loop cannot be used because the count is unknown. If the question says "the menu must be displayed at least once", REPEAT...UNTIL is usually the safest answer.
选错循环类型是常见错误。如果题目说 "只要还有记录要读取",就不能使用 FOR 循环,因为次数未知;如果题目说 "菜单必须至少显示一次",REPEAT...UNTIL 通常是更稳妥的答案。
3. Arithmetic, Boolean and String Operations | 算术、布尔与字符串运算
iation). MOD and DIV are heavily examined because they appear in problems about converting seconds to minutes, testing whether a number is even, or extracting individual digits from an integer.
OCR 伪代码中的算术运算符包括 +、−、*、/、DIV(整除)、MOD(取余)和 ^(乘方)。MOD 与 DIV 是高频考点,常见于把秒转换为分钟、判断一个数是否为偶数,或从整数中提取各位数字等题目中。
17 DIV 5 = 3 17 MOD 5 = 2 a MOD 2 = 0 表示 a 为偶数
Boolean operations combine or invert conditions. AND requires both operands to be TRUE, OR requires at least one to be TRUE, and NOT reverses the truth value. Truth tables are a standard exam item, and De Morgan's style simplifications occasionally appear in higher-mark questions.
布尔运算用于组合或取反条件。AND 要求两个操作数都为 TRUE,OR 要求至少一个为 TRUE,NOT 则反转真值。真值表是标准考点,部分高分题中还会出现类似德摩根律的简化。
String handling functions include LENGTH(), SUBSTRING(), LEFT(), RIGHT(), UCASE(), LCASE(), ASC() and CHR(). String concatenation is done with the + operator or by simple juxtaposition in OCR pseudocode. A typical exam task asks you to validate a postcode, extract a file extension, or count the vowels in a word.
字符串处理函数包括 LENGTH()、SUBSTRING()、LEFT()、RIGHT()、UCASE()、LCASE()、ASC() 和 CHR()。字符串拼接可使用 + 运算符(OCR 伪代码中也可直接并列书写)。典型考题包括校验邮政编码、提取文件扩展名或统计单词中元音字母的个数。
- LENGTH("Hello") returns 5 返回 5
- SUBSTRING(2, 3, "Computer") returns "omp" 返回 "omp"
- UCASE("abc") returns "ABC" 返回 "ABC"
- ASC("A") returns 65 返回 65;CHR(65) returns "A" 返回 "A"
Remember that string indexes in OCR pseudocode start at 1, not 0, although many real languages such as Python start at 0. Read the question carefully before writing your answer.
请注意:OCR 伪代码中字符串下标从 1 开始,而不是从 0 开始,尽管 Python 等真实语言是从 0 开始的。作答前务必仔细阅读题目说明。
4. Subroutines: Procedures and Functions | 子程序:过程与函数
A subroutine is a named block of code that performs a specific task and can be called from anywhere in the program. Decomposition into subroutines improves readability, avoids duplicated code, makes testing easier and allows a team to work on separate modules.
子程序是一段具有名称、执行特定任务的代码块,可以从程序中的任何位置调用。把程序分解为子程序可以提高可读性、避免重复代码、便于测试,并允许多名开发者分工处理不同模块。
The key distinction is that a procedure performs an action and does not return a value, while a function performs a calculation and returns exactly one value. In OCR pseudocode, procedures are declared with PROCEDURE and functions with FUNCTION...RETURNS.
关键区别在于:过程(procedure)执行一个动作且不返回值;函数(function)完成一次计算并返回恰好一个值。在 OCR 伪代码中,过程用 PROCEDURE 声明,函数用 FUNCTION...RETURNS 声明。
- PROCEDURE printMenu() ... ENDPROCEDURE — 不返回值
- FUNCTION square(n : INTEGER) RETURNS INTEGER ... ENDFUNCTION — 返回一个整型值
- CALL printMenu() / result ← square(5) — 调用方式不同
A function call can be used inside an expression, for example total ← total + square(3), whereas a procedure call must stand as a statement on its own. Mixing these up is a frequent cause of lost marks in pseudocode questions.
函数调用可以出现在表达式内部,例如 total ← total + square(3);而过程调用必须独立成为一条语句。混淆二者是伪代码题失分的常见原因。
5. Parameter Passing: By Value and By Reference | 参数传递:按值与按引用
Parameters are the values passed into a subroutine. There are two passing methods in the OCR specification: by value and by reference. Understanding the difference is essential because exam questions often ask you to trace the value of a variable after a subroutine call.
参数是传入子程序的值。OCR 考纲中规定了两种传递方式:按值传递与按引用传递。理解二者的差异非常关键,因为考题经常要求你在子程序调用之后追踪某个变量的值。
When a parameter is passed by value, a copy of the data is made and given to the subroutine. Any change made inside the subroutine affects only the local copy, so the original variable in the calling code is unchanged. This is the default in OCR pseudocode unless stated otherwise.
当参数按值传递时,会复制一份数据交给子程序。子程序内部所做的任何修改只影响这份局部副本,因此调用代码中的原始变量不受影响。在 OCR 伪代码中,除非另有说明,默认即为按值传递。
When a parameter is passed by reference, the subroutine receives the memory address of the original variable. Any change made inside the subroutine directly alters the original variable. This is useful when a subroutine must return more than one value, or when passing a large array to avoid copying overhead.
当参数按引用传递时,子程序接收的是原始变量的内存地址。子程序内部的修改会直接改变原始变量。当子程序需要返回多个值时,或为了在传递大型数组时避免复制开销,按引用传递非常有用。
| Feature | 对比项 | By value | 按值 | By reference | 按引用 |
| Data passed 传递内容 | A copy 副本 | The address 地址 |
| Original changed? 原值是否改变 | No 否 | Yes 是 |
| Typical use 典型用途 | Protecting data 保护数据不被修改 | Returning several results, large arrays 返回多个结果、传递大数组 |
A worked trace: if x ← 10 and a procedure takes x by value and sets it to 99, x is still 10 afterwards. If the same procedure takes x by reference, x becomes 99. Always state the method explicitly in your answer to gain the mark.
一个典型的追踪例子:若 x ← 10,某过程按值接收 x 并将其设为 99,调用后 x 仍为 10;若同一过程按引用接收 x,则调用后 x 变为 99。作答时务必明确写出所采用的传递方式,才能拿到该得分点。
6. Scope and Lifetime of Variables | 变量的作用域与生存期
Scope describes where in a program a variable can be accessed. A local variable is declared inside a subroutine and can only be used within that subroutine. A global variable is declared outside all subroutines and can be accessed from anywhere in the program.
作用域描述的是变量在程序中可以被访问的范围。局部变量(local variable)在子程序内部声明,只能在该子程序内使用;全局变量(global variable)在所有子程序之外声明,可在程序任意位置访问。
Local variables are preferred in good programming practice because they reduce the risk of unintended interference between different parts of a program, free memory when the subroutine ends, and make debugging easier since the variable can only be modified in one place.
良好的编程实践更倾向于使用局部变量,因为它们降低了程序不同部分相互意外干扰的风险,会在子程序结束时释放内存,并且由于变量只能在一处被修改,调试也更加容易。
Global variables are convenient for values that genuinely need to be shared, such as a login status or a configuration constant, but overuse leads to code that is hard to test and maintain. The lifetime of a local variable is the duration of the subroutine call; the lifetime of a global variable is the whole run of the program.
对于确实需要共享的值,例如登录状态或配置常量,全局变量较为方便,但过度使用会导致代码难以测试和维护。局部变量的生存期是子程序调用期间;全局变量的生存期则是整个程序运行期间。
7. Recursion | 递归
Recursion occurs when a subroutine calls itself. Every recursive subroutine must have at least one base case, which stops the recursion, and a general case, which breaks the problem into a smaller version of itself. Without a base case the recursion never ends and eventually causes a stack overflow.
递归是指子程序调用自身。每个递归子程序都必须至少有一个基线情形(base case),用于终止递归;以及一个一般情形(general case),把问题拆解为规模更小的同类问题。若没有基线情形,递归永不终止,最终会导致栈溢出。
factorial(n) = n × factorial(n − 1),base case: factorial(0) = 1
Each recursive call is pushed onto the call stack, with its own copy of the parameters and local variables. When the base case is reached, the calls are popped off the stack in reverse order and the results are combined. This is why recursion uses more memory than an equivalent loop.
每次递归调用都会被压入调用栈,并拥有各自独立的参数与局部变量副本。当到达基线情形时,各次调用按相反顺序出栈,结果再逐步合并。这也是递归比等效循环占用更多内存的原因。
Advantages of recursion include shorter, more elegant code for naturally recursive problems such as tree traversal, directory searching, and the Towers of Hanoi. Disadvantages include higher memory use, the risk of stack overflow, and generally slower execution than iteration.
递归的优点在于:对于树遍历、目录搜索、汉诺塔等本身具有递归结构的问题,代码更短、更优雅。缺点包括内存占用更高、存在栈溢出风险,并且通常比迭代执行更慢。
8. Data Structures: Arrays, Records and Lists | 数据结构:数组、记录与列表
An array is a collection of data items of the same data type stored under one identifier and accessed by an index. A one-dimensional array is a simple list; a two-dimensional array is a table with rows and columns. Arrays have a fixed size that must be declared in advance.
数组是同一数据类型的若干数据项以同一个标识符存储的集合,通过下标访问。一维数组相当于一个列表;二维数组则相当于带行与列的表格。数组的大小是固定的,必须在声明时确定。
- DECLARE names : ARRAY[1:10] OF STRING — 一维数组
- DECLARE grid : ARRAY[1:3, 1:3] OF INTEGER — 二维数组
- names[3] ← "Hao" — 赋值;LENGTH(names) 返回元素个数
A record groups related but different data types under one identifier, for example a student record containing a name (String), an age (Integer) and a grade (Char). Records are ideal for representing real-world entities and are accessed using dot notation such as student.name.
记录(record)把相关但类型不同的数据组合在同一标识符之下,例如一条学生记录包含姓名(String)、年龄(Integer)和成绩等级(Char)。记录非常适合表示现实世界中的对象,并使用点号访问,如 student.name。
A list is a more flexible structure than a fixed array because it can grow and shrink at runtime, and items can be inserted or removed without leaving empty gaps. Traversing an array with a FOR loop is a standard technique because the bounds are known.
列表(list)比固定数组更灵活,因为它可以在运行时增长或收缩,插入或删除元素时不会留下空位。由于数组边界已知,用 FOR 循环遍历数组是标准技法。
FOR i ← 1 TO LENGTH(names)
OUTPUT names[i]
NEXT i
9. File Handling and Data Persistence | 文件处理与数据持久化
Data held in variables is lost when a program ends. To persist data, programs write to and read from secondary storage files. OCR pseudocode supports text files opened with OPEN, OPENREAD or OPENWRITE, processed with READLINE and WRITELINE, and finally closed with CLOSE.
保存在变量中的数据在程序结束时就会丢失。为了持久保存数据,程序需要向二级存储中的文件写入和读取数据。OCR 伪代码支持使用 OPEN、OPENREAD 或 OPENWRITE 打开文本文件,用 READLINE 与 WRITELINE 进行读写,最后用 CLOSE 关闭文件。
- myFile ← OPENREAD("scores.txt") — 以只读方式打开
- line ← READLINE(myFile) — 读取一行
- WRITELINE(myFile, "Done") — 写入一行
- CLOSE(myFile) — 关闭文件,释放资源
A common exam pattern is reading a file of records into an array until EOF, processing the data, then writing results back to a new file. Always check for end of file before reading, and always close a file after use.
常见考法是把记录文件读取到数组中直到 EOF(文件结束),处理数据后再把结果写入新文件。读取前必须先判断是否到达文件末尾,使用后必须关闭文件。
10. Using an IDE, Debugging and Testing | 集成开发环境、调试与测试
An Integrated Development Environment (IDE) provides tools that speed up development and reduce errors. Typical IDE features include a source code editor with syntax highlighting and auto-completion, a translator (compiler or interpreter), a debugger, a runtime environment and sometimes a version control integration.
集成开发环境(IDE)提供多种工具,可加快开发速度并减少错误。典型的 IDE 功能包括:带语法高亮与自动补全的源代码编辑器、翻译器(编译器或解释器)、调试器、运行环境,有时还集成版本控制。
| Tool | 工具 | Purpose | 作用 |
| Breakpoint 断点 | Pause execution at a chosen line 在指定行暂停执行 |
| Stepping 单步执行 | Run one line at a time 逐行运行代码 |
| Watch window 变量监视 | Observe live variable values 实时观察变量值 |
| Trace table 追踪表 | Record values manually during dry runs 手动记录运行中的值 |
Errors fall into three categories. A syntax error breaks the rules of the language and stops translation. A logic error produces the wrong output although the program runs. A runtime error occurs while the program is running, for example dividing by zero or reading a file that does not exist.
错误分为三类:语法错误(syntax error)违反语言规则,会导致翻译失败;逻辑错误(logic error)程序可以运行但输出结果不正确;运行时错误(runtime error)发生在程序运行过程中,例如除以零或读取不存在的文件。
Testing should include normal, boundary and erroneous data. Boundary data such as 0, 1, the maximum array index and a value exactly on a limit is frequently worth a mark, and erroneous data checks that validation and exception handling work properly.
测试数据应包含正常数据、边界数据与错误数据。边界数据(如 0、1、数组最大下标以及恰好等于某个限值的输入)往往是得分点;错误数据则用于检验校验与异常处理是否正常工作。
11. Object-Oriented Programming Basics | 面向对象编程基础
OCR A-Level introduces object-oriented concepts. A class is a template or blueprint that defines attributes and methods; an object is an instance of a class created at runtime. Attributes are the data an object holds, and methods are the subroutines it can perform.
OCR A-Level 会介绍面向对象概念。类(class)是定义属性与方法的模板或蓝图;对象(object)是在运行时创建的类的实例。属性(attributes)是对象持有的数据,方法(methods)是对象可以执行的子程序。
Encapsulation means keeping attributes private and exposing them only through public methods such as getters and setters. Inheritance allows a subclass to reuse the attributes and methods of a parent class and extend them. Polymorphism allows the same method call to behave differently depending on the object's class.
封装(
更多咨询请联系16621398022(同微信)

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