跳到主要内容

程序设计

IGCSE 计算机科学 · 第 8 主题

训练
讲义 词汇表
8.1

编程概念

大纲
Candidates should be able to: Notes and guidance
1 Declare and use variables and constants
2 Understand and use basic data types • Including: – integer – real – char – string – Boolean
3 Understand and use input and output
4 (a) Understand and use the concept of sequence
(b) Understand and use the concept of selection • Including: – IF statements – CASE statements
(c) Understand and use the concept of iteration • Including: – count-controlled loops – pre-condition loops – post-condition loops
(d) Understand and use the concepts of totalling and counting
(e) Understand and use the concept of string handling • Including: – length – substring – upper – lower • The first character of the string can be position zero or one
(f) Understand and use arithmetic, relational and logical operators • Arithmetic, limited to: – + – – – / – * – ^ (raised to power of) – MOD – DIV • Relational, limited to: – = – < – <= – > – >= – <> (not equal to) • Logical, limited to: – AND – OR – NOT
5 Understand and use nested statements • Including nested selection and iteration • Candidates will not be required to write more than three levels of nested statements
6 (a) Understand what is meant by procedures, functions and parameters (b) Define and use procedures and functions, with or without parameters (c) Understand and use local and global variables • Procedures and functions may have up to three parameters
7 Understand and use library routines • Including: – MOD – DIV – ROUND – RANDOM
8 Understand how to create a maintainable program • Including appropriate use of: – meaningful identifiers – the commenting feature provided by the programming language – procedures and functions – relevant and appropriate commenting of syntax • Use meaningful identifiers for: – variables – constants – arrays – procedures and functions

来源:剑桥国际大纲

一个变量(variable)是一个有名字的存储,容纳一个在程序运行时能改变的值。一个常量(constant)是一个有名字的存储,它的值是固定的、不改变。

一个名为 age 的变量,容纳值 16
一个变量是一个有名字的存储,它的值能改变
屏幕上一行行程序代码
变量和常量是值的有名字的存储,在程序的代码中设定

你应当在使用前声明(declare)它们(说它们的名称和类型):

DECLARE score : INTEGER
DECLARE name : STRING
CONSTANT Pi = 3.142
两个 RAM 内存模块
当一个程序运行时,它的变量被容纳在计算机的内存(RAM)中

对一个从不改变的值(如 Pi)用一个常量,所以它在一处被设定,而且容易阅读。

探索

Variables and assignment

Step through the lines and watch each variable take its new value.

词汇表 训练
英文 中文 拼音
variable 变量 biàn liàng
constant 常量 cháng liàng
declare 声明 shēng míng
integer 整数 zhěng shù
string 字符串 zì fú chuàn
8.1

数据类型

一个数据类型(data type)说明一个变量容纳什么种类的值。你必须知道五种基本类型。

数据类型:整数(7)、实数(3.5)、字符('A')、字符串("hello")、布尔值(true/false)
基本数据类型:整数、实数、字符、字符串和布尔值
类型 容纳 例子
整数(integer) 一个整数 42-7
实数(real) 一个带小数点的数 3.14-0.5
字符(char) 一个单一字符 'A''?'
字符串(string) 一个字符序列 "Hello"
布尔值(Boolean) 两个值之一 TRUEFALSE
词汇表 训练
英文 中文 拼音
data type 数据类型 shù jù lèi xíng
real 实数 shí shù
char 字符 zì fú
Boolean 布尔值 bù ěr zhí
8.1

输入与输出

输入(input)从用户读一个值。输出(output)在屏幕上显示一个值。

OUTPUT "What is your name?"
INPUT name
OUTPUT "Hello ", name
词汇表 训练
英文 中文 拼音
input 输入 shū rù
output 输出 shū chū
8.1

三种基本结构

每个程序从三个控制结构构建。

三个小流程图:顺序(一行框)、选择(一个测试两路分支)和迭代(一个测试,主体循环回去)
三个控制结构:顺序按顺序运行步骤、选择选择一个分支、迭代重复一个主体

Sequence

顺序(sequence)意味着步骤一个接一个地运行,按顺序,从上到下。

INPUT length
INPUT width
area ← length * width
OUTPUT area

Selection

选择(selection)基于一个条件选择哪些步骤运行。用一个 IF 语句,或当有许多选择时用一个 CASE 语句。

IF score >= 50 THEN
    OUTPUT "Pass"
ELSE
    OUTPUT "Fail"
ENDIF
CASE OF grade
    'A' : OUTPUT "Excellent"
    'B' : OUTPUT "Good"
    OTHERWISE : OUTPUT "Keep trying"
ENDCASE

Iteration

迭代(iteration,一个循环(loop))重复步骤。有三种。

一个计数循环(count-controlled loop)重复一个固定的次数:

FOR i ← 1 TO 5
    OUTPUT "Hello"
NEXT i

一个前测循环(pre-condition loop)在每次重复之前检查条件,所以它可能运行零次:

WHILE answer <> "stop" DO
    INPUT answer
ENDWHILE

一个后测循环(post-condition loop)在每次重复之后检查条件,所以它总是运行至少一次:

REPEAT
    INPUT password
UNTIL password = "secret"
两个循环流程图:WHILE 在主体之前测试条件;REPEAT 先运行主体、之后测试
一个前测(WHILE)循环在主体之前测试,所以它可能运行零次;一个后测(REPEAT)循环之后测试,所以它至少运行一次
探索

How an IF … ELSE IF ladder decides

The conditions are tested top to bottom; the FIRST one that is true runs and the rest are skipped. Slide the score and watch which branch lights up.

探索

Selection

Change the value and watch which branch runs — selection in action.

词汇表 训练
英文 中文 拼音
sequence 顺序 shùn xù
selection 选择 xuǎn zé
iteration 迭代 dié dài
loop 循环 xún huán
count-controlled loop 计数循环 jì shù xún huán
pre-condition loop 前测循环 qián cè xún huán
post-condition loop 后测循环 hòu cè xún huán
length 长度 cháng dù
8.1

求和与计数

  • 求和(totalling)——不断把值加到一个运行中的总数:total ← total + value
  • 计数(counting)——每次给一个计数器加 1:count ← count + 1
count = count + 1 按 1 计数;total = total + value 加到一个运行中的总数
一个计数器按 1 计数;一个总数构建一个运行中的和
一个追踪表,跟随一个在值 4、7 和 5 上的循环:total 走 0、4、11、16,而 count 走 0、1、2、3
一个追踪表一趟接一趟地跟随一个求和和计数的循环
total ← 0
FOR i ← 1 TO 10
    INPUT mark
    total ← total + mark
NEXT i
OUTPUT total

例题。 一个程序要读入 5 个分数,然后输出总分和平均分。

total ← 0
FOR i ← 1 TO 5
    INPUT mark
    total ← total + mark
NEXT i
average ← total / 5
OUTPUT total, average

有两行是得分点。total ← 0 必须放在循环之前:放进循环里,总和每次都会被清零,程序最后只会输出最后一个分数。而 average ← total / 5 必须放在循环之后,因为在所有分数都加完之前,总和还不完整。先初始化,后计算 - 这个先后顺序才是题目真正考的东西。

词汇表 训练
英文 中文 拼音
totalling 求和 qiú hé
counting 计数 jì shù
8.1

运算符

Arithmetic operators

运算符 意义
+ - * / 加、减、乘、除
^ 提升到……次幂
MOD 除法后的余数(remainder)
DIV 一个除法的整数部分

例如,17 MOD 52,而 17 DIV 53

Relational operators

这些比较两个值并给出一个布尔结果:=<<=>>=,以及 <>(不等于)。

Logical operators

这些连接条件:AND(两者都必须为真)、OR(至少一个必须为真)、NOT(反转真/假)。

三族运算符:像加和乘的算术运算符给一个数、像小于的关系运算符比较并给真或假、逻辑运算符 AND OR NOT 连接条件
三族运算符:算术、关系和逻辑
IF age >= 13 AND age <= 19 THEN
    OUTPUT "Teenager"
ENDIF
词汇表 训练
英文 中文 拼音
remainder 余数 yú shù
8.1

字符串处理

一个字符串由字符构成。有用的操作:

  • 长度(length)——字符串中字符的数目;
  • 子串(substring)——从字符串取出的一个更小的部分;
  • 大写(upper case)——把字母改成大写;
  • 小写(lower case)——把字母改成小写。
name ← "Computer"
OUTPUT LENGTH(name)          // 8
OUTPUT SUBSTRING(name, 1, 4) // "Comp"
OUTPUT UCASE(name)           // "COMPUTER"
OUTPUT LCASE(name)           // "computer"

(第一个字符可能被数为位置 0 或位置 1,取决于语言。)

探索

Index and slice a string

Every character has a position (index). A slice takes the characters from the start index up to — but not including — the end index, just like SUBSTRING does.

词汇表 训练
英文 中文 拼音
substring 子串 zi chuàn
upper case 大写 dà xiě
lower case 小写 xiǎo xiě
8.1

嵌套语句

一个嵌套语句(nested statement)是放在另一个里面的一个控制结构。你可以嵌套选择和迭代。

FOR i ← 1 TO 3
    IF i MOD 2 = 0 THEN
        OUTPUT i, " is even"
    ELSE
        OUTPUT i, " is odd"
    ENDIF
NEXT i

你不会必须写多于三层的嵌套。

词汇表 训练
英文 中文 拼音
nested statement 嵌套语句 qiàn tào yǔ jù
8.1

过程与函数

为了避免重复代码,你可以把一个程序分成有名字的块。

  • 一个过程(procedure)是做一个任务的一个有名字的代码块。你用 CALL 运行它。
  • 一个函数(function)像一个过程,但它把一个值返回到它被调用的地方。

一个参数(parameter)是一个传入一个过程或函数的值(最多三个参数)。

PROCEDURE Greet(personName : STRING)
    OUTPUT "Hello ", personName
ENDPROCEDURE

CALL Greet("Sam")
FUNCTION Square(n : INTEGER) RETURNS INTEGER
    RETURN n * n
ENDFUNCTION

answer ← Square(5)   // answer is 25

Local and global variables

  • 一个局部变量(local variable)在一个过程或函数里面声明。它只能在那里被使用。
  • 一个全局变量(global variable)在主程序中声明。它能在任何地方被使用。

局部变量更安全,因为它们不能被程序另一部分错误地改变。

词汇表 训练
英文 中文 拼音
procedure 过程 guò chéng
function 函数 hán shù
parameter 参数 cān shù
local variable 局部变量 jú bù biàn liàng
global variable 全局变量 quán jú biàn liàng
8.1

库函数

一个库程序(library routine)是你能使用的一段现成的代码。你必须知道这些:

程序 它做什么
MOD 给一个除法的余数
DIV 给一个除法的整数部分
ROUND 把一个实数舍入到若干小数位
RANDOM 给一个随机数
词汇表 训练
英文 中文 拼音
library routine 库程序 kù chéng xù
8.1

编写易于维护的程序

一个可维护的(maintainable)程序对其他人来说以后容易阅读和改变。要制作一个:

  • 有意义的标识符(meaningful identifiers)——变量、常量、数组、过程和函数的清晰名称(totalScore,不是 x);
  • 注释(comments)以解释代码的各部分做什么;
  • 用过程和函数把工作分成小块。
词汇表 训练
英文 中文 拼音
maintainable 可维护的 kě wéi hù de
meaningful identifiers 有意义的标识符 yǒu yì yì de biāo shí fú
comments 注释 zhù shì
8.2

数组

大纲
Candidates should be able to: Notes and guidance
1 Declare and use one-dimensional (1D) and two-dimensional (2D) arrays
2 Understand the use of arrays • Including the use of variables as indexes in arrays
3 Write values into, and read values from, an array using iteration • The first index can be zero or one • Including nested iteration

来源:剑桥国际大纲

一个数组(array)是一个单一变量,容纳同一类型的许多值,由一个索引(index,一个位置号)找到。

One-dimensional arrays

一个一维(1D)数组(one-dimensional array)像一个单一的列表。

DECLARE scores : ARRAY[1:5] OF INTEGER
scores[1] ← 90
scores[2] ← 75
OUTPUT scores[1]
一行五个框,标注索引 1 到 5,容纳值,索引 2 处的值被高亮为 75
一个 1D 数组在一个变量中容纳许多值;每个由它的索引找到

你可以用一个循环填充或读取一个数组:

FOR i ← 1 TO 5
    INPUT scores[i]
NEXT i

Two-dimensional arrays

一个二维(2D)数组(two-dimensional array)像一个带行和列的表。它用两个索引。

DECLARE grid : ARRAY[1:3, 1:3] OF INTEGER
grid[1, 1] ← 5
grid[2, 3] ← 8
一个 3 乘 3 的网格,带编号的行和列;第 2 行第 3 列的单元被高亮并标注为 grid 第 2 行第 3 列
一个 2D 数组是一个表;每个值由两个索引 [行, 列] 找到

你用一个循环里面的一个循环(嵌套迭代)读一个 2D 数组。

探索

Arrays

Pick a position to read one element — how a list (array) is indexed.

词汇表 训练
英文 中文 拼音
array 数组 shù zǔ
index 索引 suǒ yǐn
one-dimensional (1D) array 一维数组 yī wéi shù zǔ
two-dimensional (2D) array 二维数组 èr wéi shù zǔ
8.3

文件处理

大纲
Candidates should be able to: Notes and guidance
1 Understand the purpose of storing data in a file to be used by a program
2 Open, close and use a file for reading and writing • Including: – read and write single items of data – read and write a line of text

来源:剑桥国际大纲

一个程序可以把数据存储在一个文件(file)中,所以它在程序停止后被保留。你必须打开文件、使用它,然后关闭它。

OPENFILE "data.txt" FOR WRITE
WRITEFILE "data.txt", "Hello"
CLOSEFILE "data.txt"

OPENFILE "data.txt" FOR READ
READFILE "data.txt", line
CLOSEFILE "data.txt"

你可以读和写单个数据项或一整行文本。当你完成使用一个文件时总是关闭它。

词汇表 训练
英文 中文 拼音
file 文件 wén jiàn
8.3

考试技巧

  • 一个变量能在程序运行时改变;一个常量是固定的。学习五种数据类型:整数、实数、字符、字符串、布尔值。
  • 每个程序从三个结构构建:顺序选择(IF / CASE),和迭代(循环)。
  • 一个 WHILE 循环在主体之前测试,所以它可能运行零次;一个 REPEAT … UNTIL 循环之后测试,所以它总是至少运行一次。
  • MOD 给余数,而 DIV 给一个除法的整数部分:17 MOD 5 是 2,17 DIV 5 是 3。
  • 一个函数返回一个值;一个过程不返回。一个局部变量只在它的块里面工作;一个全局的在任何地方工作。

本主题的互动课程

逐步学习,并即时检测练习。

IGCSE 计算机科学历年真题

IGCSE 计算机科学的更多主题

登录或创建账号

IGCSE, A-Level & AP