IGCSE CIE Computer Science: Programming Basics Key Points | IGCSE CIE 计算机:编程基础考点精讲

📚 IGCSE CIE Computer Science: Programming Basics Key Points | IGCSE CIE 计算机:编程基础考点精讲

In IGCSE CIE Computer Science, programming basics form the foundation for writing efficient, logical solutions. This revision article covers essential concepts such as variables, data types, sequence, selection, iteration, pseudocode conventions, and debugging techniques like trace tables. Mastering these fundamentals is critical for both paper-based algorithm questions and practical problem-solving.

在 IGCSE CIE 计算机科学中,编程基础是编写高效、逻辑清晰的解决方案的根基。这篇复习文章涵盖了变量、数据类型、顺序、选择、迭代、伪代码规范以及跟踪表等调试技巧等核心概念。掌握这些基础知识对解答试卷中的算法题和实际编程问题都至关重要。

1. Variables and Constants | 变量与常量

A variable is a named storage location in memory whose value can change during program execution. A constant is similar but its value remains fixed throughout the program. Both must be declared with a meaningful identifier that follows naming rules (no spaces, must start with a letter, can contain digits and underscores). Using constants improves maintainability, as a single update changes the value everywhere.

变量是内存中命名的存储位置,其值在程序执行过程中可以改变。常量的值则在整个程序中保持不变。两者都必须用一个有意义的标识符声明,标识符需遵循命名规则(不含空格,以字母开头,可以包含数字和下划线)。使用常量可以提高程序的可维护性,因为一次更新即可使所有引用处的值同时改变。

Declare MaxStudents : INTEGER
CONSTANT Pi ← 3.14159

声明 MaxStudents : INTEGER
常量 Pi ← 3.14159

2. Data Types | 数据类型

IGCSE requires recognising four elementary data types: INTEGER (whole numbers), REAL (numbers with decimal parts), CHAR (a single character such as ‘A’ or ‘?’), and STRING (a sequence of characters, e.g. “hello”). Some pseudocode variants also include BOOLEAN for TRUE/FALSE values. Choosing the correct data type affects memory usage and the operations you can perform – you cannot divide a string, for example.

IGCSE 要求识别四种基本数据类型:INTEGER(整数)、REAL(带小数部分的数字)、CHAR(单个字符,如 ‘A’ 或 ‘?’)以及 STRING(一串字符,如 “hello”)。某些伪代码变体还包括用于 TRUE/FALSE 值的 BOOLEAN 类型。选择正确的数据类型会影响内存使用和可执行的操作——例如,你不能对字符串做除法。

DECLARE Name : STRING
DECLARE Age : INTEGER
DECLARE Height : REAL

3. Input and Output | 输入与输出

Programs interact with the user through input and output statements. In CIE pseudocode, INPUT reads a value from the keyboard and stores it in a variable, while OUTPUT displays a message or the value of a variable on the screen. You can combine text and variables using the ampersand (&) operator for concatenation.

程序通过输入和输出语句与用户交互。在 CIE 伪代码中,INPUT 从键盘读取数值并存入一个变量,而 OUTPUT 在屏幕上显示一条消息或某个变量的值。你可以使用连接符(&)将文本和变量组合在一起输出。

INPUT StudentName
OUTPUT "Welcome, " & StudentName

输入 StudentName
输出 “Welcome, ” & StudentName

4. Assignment and Operators | 赋值与运算符

Assignment stores a value into a variable. In CIE pseudocode, the left arrow symbol (←) is used, for example Total ← 0. The value on the right can be a literal, another variable, or an expression involving operators. Arithmetic operators are +, –, *, /, MOD, DIV. MOD returns the remainder of an integer division, while DIV returns the integer quotient. Comparison operators (=, <>, >, <, >=, <=) yield Boolean results. Logical operators AND, OR, NOT combine Boolean values.

赋值操作用于将数值存入一个变量。在 CIE 伪代码中,使用左箭头符号(←),例如 Total ← 0。右边的值可以是字面量、另一个变量或包含运算符的表达式。算术运算符有 +, –, *, /, MOD, DIV。MOD 返回整数除法的余数,DIV 返回整数商。比较运算符(=, <>, >, <, >=, <=)产生布尔结果。逻辑运算符 AND, OR, NOT 用于组合布尔值。

Num ← 7 MOD 2   // Num = 1
Quot ← 7 DIV 2  // Quot = 3
Flag ← (Age > 18) AND (Score >= 50)

Num ← 7 MOD 2 // Num = 1
Quot ← 7 DIV 2 // Quot = 3
Flag ← (Age > 18) AND (Score >= 50)

5. Sequence Structure | 顺序结构

Sequence is the simplest control structure: instructions are executed one after another in the order they are written. Every program begins with a sequence. Understanding sequence helps you plan the correct order of input, processing, and output. For example, you must input numbers before calculating their average, and calculate before displaying the result.

顺序结构是最简单的控制结构:指令按照书写顺序依次执行。每个程序都以顺序结构开始。理解顺序有助于你规划正确的输入、处理和输出顺序。例如,你必须先输入数字再计算平均值,先计算再显示结果。

INPUT X
INPUT Y
Sum ← X + Y
OUTPUT Sum

输入 X
输入 Y
Sum ← X + Y
输出 Sum

6. Selection: IF Statements | 选择结构:IF 语句

Selection allows the program to choose between different paths based on a condition. The IF statement tests a Boolean expression: if TRUE, the THEN block executes; otherwise the ELSE block (if present) executes. CIE pseudocode uses IF … THEN … ELSE … ENDIF. The condition can be a single comparison or a compound expression using AND/OR.

选择结构允许程序根据条件在不同路径之间做出选择。IF 语句测试一个布尔表达式:如果为真,执行 THEN 代码块;否则执行 ELSE 代码块(如果有)。CIE 伪代码使用 IF … THEN … ELSE … ENDIF 结构。条件可以是单一比较,也可以是使用 AND/OR 连接的复合表达式。

INPUT Temperature
IF Temperature > 30 THEN
    OUTPUT "Very hot"
ELSE
    OUTPUT "Comfortable"
ENDIF

输入 Temperature
IF Temperature > 30 THEN
OUTPUT “Very hot”
ELSE
OUTPUT “Comfortable”
ENDIF

7. CASE Statements | CASE 语句

When there are multiple distinct values to test against a single variable, a CASE statement is more readable than nested IFs. CASE OF … OTHERWISE … ENDCASE evaluates the variable and jumps to the matching value block. The OTHERWISE clause handles any value not explicitly listed. This is particularly useful for menu-driven programs or grading systems.

当需要对同一个变量的多个不同取值进行测试时,CASE 语句比嵌套的 IF 更易读。CASE OF … OTHERWISE … ENDCASE 对变量求值,然后跳转到匹配的值对应的代码块。OTHERWISE 子句处理所有未明确列出的值。这在菜单驱动型程序或评分系统中特别有用。

INPUT Grade
CASE Grade OF
    'A': OUTPUT "Excellent"
    'B': OUTPUT "Good"
    OTHERWISE OUTPUT "Try harder"
ENDCASE

输入 Grade
CASE Grade OF
‘A’: 输出 “Excellent”
‘B’: 输出 “Good”
OTHERWISE 输出 “Try harder”
ENDCASE

8. Iteration: Count-controlled Loops (FOR) | 计数控制循环 (FOR)

A FOR loop repeats a block of code a fixed number of times. The CIE syntax is FOR Identifier ← Start TO Stop [STEP StepValue] … NEXT Identifier. The loop counter increments (or decrements with a negative STEP) each time the loop body ends. It is ideal when you know in advance how many iterations are needed, for example processing the four corners of a square or reading ten numbers.

FOR 循环将一段代码重复执行固定的次数。CIE 语法为 FOR Identifier ← Start TO Stop [STEP StepValue] … NEXT Identifier。每次循环体结束时,循环计数器会递增(若 STEP 为负则递减)。当你预先知道需要多少次迭代时,FOR 循环是理想选择,例如处理正方形四个角的数据或读取十个数字。

Sum ← 0
FOR i ← 1 TO 10
    INPUT Num
    Sum ← Sum + Num
NEXT i
OUTPUT Sum

Sum ← 0
FOR i ← 1 TO 10
INPUT Num
Sum ← Sum + Num
NEXT i
输出 Sum

9. Iteration: Condition-controlled Loops (WHILE and REPEAT) | 条件控制循环 (WHILE 和 REPEAT)

Condition-controlled loops repeat while a condition is true, or until it becomes true. The WHILE loop WHILE condition DO … ENDWHILE tests the condition before each iteration; the loop may execute zero times. The REPEAT loop REPEAT … UNTIL condition tests the condition after each iteration, guaranteeing at least one execution. Both are essential when the number of iterations is unknown, such as validating user input.

条件控制循环在条件为真时重复,或重复直到条件为真。WHILE 循环 WHILE condition DO … ENDWHILE 在每次迭代前测试条件;循环可能一次都不执行。REPEAT 循环 REPEAT … UNTIL condition 在每次迭代后测试条件,保证至少执行一次。当迭代次数未知时,例如验证用户输入,这两种循环都是必不可少的。

INPUT Password
WHILE Password <> "secret" DO
    OUTPUT "Wrong! Try again."
    INPUT Password
ENDWHILE
OUTPUT "Access granted."

输入 Password
WHILE Password <> “secret” DO
OUTPUT “Wrong! Try again.”
INPUT Password
ENDWHILE
输出 “Access granted.”

10. Nested Structures | 嵌套结构

Control structures can be placed inside one another: a loop inside a loop, a selection inside a loop, etc. CIE expects you to trace and write nested pseudocode accurately. Nesting is powerful but must be handled carefully with correct indentation and matching keywords (IF…ENDIF, FOR…NEXT, WHILE…ENDWHILE). Common examples include printing a multiplication table (nested FOR loops) or searching a grid pattern.

控制结构可以相互嵌套:循环内的循环、循环内的选择等。CIE 要求你能准确地追踪和编写嵌套的伪代码。嵌套功能强大,但必须小心处理,确保正确的缩进和配对的关键字(IF…ENDIF, FOR…NEXT, WHILE…ENDWHILE)。常见的示例包括打印乘法表(嵌套 FOR 循环)或搜索网格图案。

FOR Row ← 1 TO 5
    FOR Col ← 1 TO 5
        IF Row = Col THEN
            OUTPUT "*"
        ELSE
            OUTPUT "-"
        ENDIF
    NEXT Col
    OUTPUT newline
NEXT Row

FOR Row ← 1 TO 5
FOR Col ← 1 TO 5
IF Row = Col THEN
OUTPUT “*”
ELSE
OUTPUT “-“
ENDIF
NEXT Col
输出新行
NEXT Row

11. Basic String Manipulation | 基本字符串操作

Strings are sequences of characters, and typical operations include concatenation (joining two strings with &), determining length (LENGTH(String)), and extracting substrings (SUBSTRING(String, Start, NumberOfChars)). Upper‑case and lower‑case conversion may also appear. These are useful for data formatting, input validation, and reports.

字符串是字符序列,典型的操作包括拼接(用 & 连接两个字符串)、求长度(LENGTH(String))以及提取子串(SUBSTRING(String, Start, NumberOfChars))。大小写转换也可能出现。这些操作对数据格式化、输入验证和生成报告很有用。

FirstName ← "Ada"
LastName ← "Lovelace"
FullName ← FirstName & " " & LastName
Len ← LENGTH(FullName)   // 11 (including space)
Initials ← SUBSTRING(FirstName,1,1) & "." & SUBSTRING(LastName,1,1)

FirstName ← “Ada”
LastName ← “Lovelace”
FullName ← FirstName & ” ” & LastName
Len ← LENGTH(FullName) // 11(含空格)
Initials ← SUBSTRING(FirstName,1,1) & “.” & SUBSTRING(LastName,1,1)

12. Trace Tables and Debugging | 跟踪表与调试

A trace table is a systematic method for manually stepping through an algorithm to verify its correctness. Each column represents a variable (or condition), and each row shows the variable values after a line of pseudocode executes. Trace tables help identify logic errors such as incorrect conditions, off-by-one errors, or infinite loops. In CIE exams you may be required to complete or construct a trace table for a given algorithm.

跟踪表是一种系统化的人工逐步执行算法并验证其正确性的方法。每一列代表一个变量(或条件),每一行显示执行某行伪代码后变量的值。跟踪表有助于发现逻辑错误,如错误的条件、差一错误(off‑by‑one)或无限循环。在 CIE 考试中,你可能会被要求为一个给定的算法补全或构造一个跟踪表。

Line Count Num Total Condition (Count < 3)
1 0 – 0 TRUE
2 1 5 5 TRUE
2 2 8 13 TRUE
2 3 2 15 FALSE (exit)

Example trace table for a loop that sums three input numbers.

示例跟踪表:一个对三个输入数字求和的循环。

Published by TutorHao | CIE IGCSE 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