Programming basics · 编程基础
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| variable/ˈveərɪəbl/ | 变量 | biàn liàng |
| data type/ˈdeɪtə taɪp/ | 数据类型 | shù jù lèi xíng |
| constant/ˈkɒnstənt/ | 常量 | cháng liàng |
| flowchart/ˈfləʊtʃɑːt/ | 流程图 | liú chéng tú |
| structured English/ˈstrʌktʃəd ˈɪŋɡlɪʃ/ | 结构化英语 | jié gòu huà yīng yǔ |
| pseudocode/ˈsuːdəʊkəʊd/ | 伪代码 | wěi dài mǎ |
| assignment/əˈsaɪnmənt/ | 赋值 | fù zhí |
| identifier/aɪˈdentɪfaɪə/ | 标识符 | biāo shí fú |
| operators/ˈɒpəreɪtəz/ | 运算符 | yùn suàn fú |
| precedence/ˈpresɪdəns/ | 优先级 | yōu xiān jí |
| concatenation/kənˌkætəˈneɪʃn/ | 拼接 | pīn jiē |
| library routines/ˈlaɪbrəri ruːˈtiːnz/ | 库例程 | kù lì chéng |
| program library/ˈprəʊɡræm ˈlaɪbrəri/ | 程序库 | chéng xù kù |
| insert/ˈɪnsɜːt/ | 附页 | fù yè |
The number that did not fit
- On 4 June 1996 the first Ariane 5 rocket veered off course and destroyed itself 37 seconds after launch, with four satellites on board.
- The cause was one conversion: a 64-bit decimal value describing the rocket's sideways speed was copied into a 16-bit integer. Ariane 5 was faster than Ariane 4, the number was too big for the box, and the guidance computer shut down.
- Every variable has a data type 数据类型, and the type decides what fits. Choosing it is the first job in turning a design into code.
- This lesson is the basics: constants, variables, types, expressions, input and output, and the ready-made routines you may call.
放不进去的那个数
- 1996 年 6 月 4 日,第一枚阿丽亚娜 5 火箭在发射 37 秒后偏离航线并自毁,带着四颗卫星。
- 原因是一次转换:描述火箭横向速度的一个 64 位小数被复制进一个 16 位整数。阿丽亚娜 5 比阿丽亚娜 4 快,这个数对那个"盒子"来说太大了,制导计算机随即关机。
- 每个变量都有一个数据类型(data type),类型决定什么放得进去。选择类型是把设计变成代码的第一件事。
- 这一课是基础:常量、变量、类型、表达式、输入输出,以及你可以调用的现成例程。
From design to code
- A design arrives as a flowchart 流程图 or as structured English 结构化英语. You turn it into pseudocode 伪代码, and later into a real language.
- Find the variables 变量 and their types. Input and output boxes become
INPUTandOUTPUT; decision diamonds becomeIF … ELSE … ENDIForCASE; loop arrows becomeWHILE,REPEAT … UNTILorFOR; process boxes become assignments. - Check by tracing a small input through your pseudocode before you call it finished.
Each flowchart symbol becomes one kind of pseudocode statement
从设计到代码
- 设计以流程图(flowchart)或结构化英语(structured English)的形式到来。你把它变成伪代码(pseudocode),之后再变成真正的语言。
- 找出变量(variables)和它们的类型。输入输出框变成
INPUT和OUTPUT;判断菱形变成IF … ELSE … ENDIF或CASE;循环箭头变成WHILE、REPEAT … UNTIL或FOR;处理框变成赋值。 - 在宣布完成之前,用一个小输入在你的伪代码里走一遍来检查。

每种流程图符号变成一种伪代码语句
Constants and variables
- A constant 常量 holds a value that never changes while the program runs:
CONSTANT Pi = 3.14159. A variable holds one that may change, and is declared with a type:DECLARE Radius : REAL. - Use a constant for a fixed value that appears in several places, such as a tax rate or a maximum score.
- The benefits the mark scheme lists: it is set once and cannot be changed accidentally; a change is made in one place and reaches every statement that uses it; the identifier 标识符 gives the value a meaningful name.
A variable can be reassigned; a constant cannot
常量和变量
- 常量(constant)保存一个程序运行期间永不改变的值:
CONSTANT Pi = 3.14159。变量保存可能改变的值,并带类型声明:DECLARE Radius : REAL。 - 对出现在多处的固定值用常量,比如税率或最高分。
- 评分标准列出的好处:它只设置一次,不会被意外改变;修改只需在一处进行,就能到达每条使用它的语句;标识符(identifier)给这个值一个有意义的名字。

变量可以重新赋值;常量不能
A variable is a labelled box · 一个变量是一个有标签的盒子
Each assignment stores one value in a named box; reassigning the same name overwrites it. Step through the program and watch each box take its current value. · 每个赋值在一个有名字的盒子里存储一个值;重新赋值同一个名字会覆盖它。逐步走过程序,看每个盒子取它当前的值。
A constant holds a fixed value that never changes while the program runs, whereas a variable can be reassigned. · 一个常量保存一个在程序运行时从不改变的固定值,而一个变量能被重新赋值。
Constants (like Pi or a maximum score) make code clearer and let you change a recurring value in one place. · 常量(像 Pi 或一个最高分)使代码更清晰,并让你在一个地方改变一个反复出现的值。
Which are benefits of using a constant for a value such as a tax rate? Select all · 所有 that apply. · 对税率这样的值使用常量有哪些好处?选出所有适用的。
The three mark-scheme benefits are safety, a single point of change, and readability. Speed is not one of them. · 评分标准的三个好处是安全、单一修改点和可读性。速度不在其中。
The six data types
INTEGERfor whole numbers (a count, an index, a loop counter).REALfor numbers with a fractional part (a price, an average).CHARfor one character in single quotes,'A'.STRINGfor text in double quotes,"Hello".BOOLEANforTRUEorFALSE(a flag such asFound).DATEfor a calendar date.- Read the type off how the variable is used: a decimal point means
REAL,TRUEmeansBOOLEAN, single quotes meanCHAR, an array index orDIVandMODmeanINTEGER. Write the type in capitals as the guide spells it.
六种数据类型
INTEGER用于整数(计数、索引、循环计数器)。REAL用于带小数部分的数(价格、平均值)。CHAR用于单引号里的一个字符,'A'。STRING用于双引号里的文本,"Hello"。BOOLEAN用于TRUE或FALSE(像Found这样的标志)。DATE用于日历日期。- 从变量怎样被使用读出类型:有小数点是
REAL,TRUE是BOOLEAN,单引号是CHAR,数组索引或DIV、MOD是INTEGER。类型用大写,按指南的拼法写。
Worked example: state the appropriate data type
Found ← FALSEisBOOLEAN: it holds a truth value.Initial ← 'K'isCHAR: one character in single quotes.Name ← "Li Wei"isSTRING: text in double quotes.Price ← 12.99isREAL: it has a fractional part.Count ← Count + 1isINTEGER: a counter that goes up in whole steps.- One mark per correct type, so answer every row even when unsure; a blank cannot score.
例题:说出合适的数据类型
Found ← FALSE是BOOLEAN:它保存一个真值。Initial ← 'K'是CHAR:单引号里的一个字符。Name ← "Li Wei"是STRING:双引号里的文本。Price ← 12.99是REAL:它有小数部分。Count ← Count + 1是INTEGER:按整数步长增加的计数器。- 每个正确的类型一分,所以不确定时也要答完每一行;空白不能得分。
Match each value to the data type its variable should be declared with. · 把每个值与它的变量应声明的数据类型配对。
A decimal point, single quotes, double quotes and a truth value each point to one type. · 小数点、单引号、双引号和真值各指向一种类型。
Assignment and expressions
- Assignment 赋值 uses the arrow:
Total ← Total + 1,Average ← Sum / Count. - Arithmetic operators 运算符 are
+ - * /, plusDIVfor whole-number division andMODfor the remainder:7 DIV 2 = 3and7 MOD 2 = 1. - Comparisons are
=,<>,<,>,<=,>=; logic isAND,OR,NOT. - Precedence 优先级, highest first:
NOT, then* / DIV MOD, then+ -, then comparisons, thenAND, thenOR. Use brackets when unsure.
赋值和表达式
- 赋值(assignment)用箭头:
Total ← Total + 1,Average ← Sum / Count。 - 算术运算符(operators)是
+ - * /,加上整数除法DIV和余数MOD:7 DIV 2 = 3,7 MOD 2 = 1。 - 比较是
=、<>、<、>、<=、>=;逻辑是AND、OR、NOT。 - 优先级(precedence)从高到低:
NOT,然后* / DIV MOD,然后+ -,然后比较,然后AND,然后OR。不确定时加括号。
What is the value of 7 DIV 2? · 7 DIV 2 的值是多少?
DIV is integer division: 7 ÷ 2 = 3 remainder 1, so DIV gives 3. · DIV 是整数除法:7 ÷ 2 = 3 余 1,所以 DIV 给出 3。
What is the value of 7 MOD 2? · 7 MOD 2 的值是多少?
MOD gives the remainder: 7 ÷ 2 leaves remainder 1. · MOD 给出余数:7 ÷ 2 留下余数 1。
Which symbol assigns a value to a variable in this pseudocode? · 在这个伪代码中,哪个符号给一个变量赋一个值?
Assignment uses ←; = is comparison. · 赋值用 ←;= 是比较。
Match each pseudocode operator to what it does. · 把每个伪代码运算符与它做的事配对。
← stores, = compares; DIV gives the quotient and MOD the remainder (7 DIV 2 = 3, 7 MOD 2 = 1). · ← 存储,= 比较;DIV 给出商,MOD 给出余数(7 DIV 2 = 3,7 MOD 2 = 1)。
Input and output
OUTPUT "Enter your name:"prompts;INPUT Namereads the keyboard into the variable;OUTPUT "Hello, ", Nameprints several items separated by commas.- Strings are joined with
&, called concatenation 拼接:"A" & "BC"is"ABC". - Keep the quotes straight:
"7"is a string,7is a number, and'7'is a single character.
输入和输出
OUTPUT "Enter your name:"提示;INPUT Name把键盘输入读进变量;OUTPUT "Hello, ", Name用逗号分隔打印多个项目。- 字符串用
&拼接,叫拼接(concatenation):"A" & "BC"是"ABC"。 - 把引号分清:
"7"是字符串,7是数字,'7'是单个字符。
Built-in functions and library routines
- A program library 程序库 holds routines that are already written, compiled and tested; calling one is faster and safer than writing your own. The Paper 2 insert 附页 lists the library routines 库例程 you may use, with their exact names.
- Strings:
LENGTH(s),LEFT(s, n),RIGHT(s, n),MID(s, start, n)with positions counted from 1,TO_UPPER(s),TO_LOWER(s). Conversion:NUM_TO_STR(x),STR_TO_NUM(s),IS_NUM(s),ASC(c),CHR(n). - Numbers and dates:
INT(x)cuts to the whole part;RAND(n)gives a real from 0 up to but not including n, soINT(RAND(6)) + 1is a dice roll;DAY,MONTH,YEAR,DAYINDEX,SETDATE,TODAY;EOF(f)for files.
The common string routines on s = "COMPUTER", positions 1 to 8
内置函数和库例程
- 程序库(program library)保存已经写好、编译并测试过的例程;调用一个比自己写更快也更安全。Paper 2 的附页(insert)列出你可以使用的库例程(library routines)及其确切名称。
- 字符串:
LENGTH(s)、LEFT(s, n)、RIGHT(s, n)、MID(s, start, n)(位置从 1 开始数)、TO_UPPER(s)、TO_LOWER(s)。转换:NUM_TO_STR(x)、STR_TO_NUM(s)、IS_NUM(s)、ASC(c)、CHR(n)。 - 数字和日期:
INT(x)截取整数部分;RAND(n)给出从 0 到但不包括 n 的实数,所以INT(RAND(6)) + 1是掷一次骰子;DAY、MONTH、YEAR、DAYINDEX、SETDATE、TODAY;文件用EOF(f)。

常用字符串例程作用于 s = "COMPUTER",位置 1 到 8
Which routine converts a string to capitals in 9618 pseudocode? · 在 9618 伪代码中,哪个例程把字符串转成大写?
UCASE is the IGCSE name and is not accepted here. Use the exact names printed in the Paper 2 insert. · UCASE 是 IGCSE 的名字,这里不被接受。用 Paper 2 附页上印的确切名称。
Worked example: evaluate each expression
- Given
Word ← "Program",Code ← 'Q'andN ← 7. Work from the inside out. LENGTH(Word)is7.MID(Word, 4, 2)is"gr": two characters from position 4.LEFT(Word, 3) & "!"is"Pro!".TO_UPPER(RIGHT(Word, 2))is"AM": the inner function runs first.ASC(Code) - ASC('A')is16, because'Q'is 81 and'A'is 65.N DIV 2 + N MOD 2is3 + 1 = 4.NUM_TO_STR(N) & "th"is"7th".INT(N / 2)is3, the whole part of 3.5.
例题:求每个表达式的值
- 已知
Word ← "Program"、Code ← 'Q'和N ← 7。从里往外算。 LENGTH(Word)是7。MID(Word, 4, 2)是"gr":从位置 4 起两个字符。LEFT(Word, 3) & "!"是"Pro!"。TO_UPPER(RIGHT(Word, 2))是"AM":里面的函数先运行。ASC(Code) - ASC('A')是16,因为'Q'是 81,'A'是 65。N DIV 2 + N MOD 2是3 + 1 = 4。NUM_TO_STR(N) & "th"是"7th"。INT(N / 2)是3,3.5 的整数部分。
With Word ← "Program", the value of MID(Word, 4, 2) is "". · 已知 Word ← "Program",MID(Word, 4, 2) 的值是 ""。
Positions count from 1: P-r-o-g-r-a-m, so position 4 is g and two characters from there are gr. · 位置从 1 开始数:P-r-o-g-r-a-m,所以位置 4 是 g,从那里起两个字符是 gr。
Worked example: find the error
- Each statement may misuse a function or an operator. Describe the error, or write NO ERROR.
Result ← 2 & 4:&joins strings, but 2 and 4 are integers, so+is needed.SubString ← MID("pseudocode", 4, 1): NO ERROR, one character from position 4,"u".Answer ← LENGTH(42):LENGTHtakes a string, not an integer.Flag ← "TRUE": a Boolean isTRUEwithout quotes; with quotes it is a string.
例题:找出错误
- 每条语句可能误用了函数或运算符。描述错误,或写 NO ERROR。
Result ← 2 & 4:&拼接字符串,但 2 和 4 是整数,所以需要+。SubString ← MID("pseudocode", 4, 1):NO ERROR,从位置 4 取一个字符,"u"。Answer ← LENGTH(42):LENGTH接受字符串,不是整数。Flag ← "TRUE":布尔值是不带引号的TRUE;带引号就是字符串。
Result ← 2 & 4 correctly stores 6 in Result. · Result ← 2 & 4 正确地把 6 存进 Result。
& joins strings. For two integers the operator is +; this is the error the exam asks you to describe. · & 拼接字符串。两个整数用的运算符是 +;这正是考试要你描述的错误。
Marks that slip away
- The IGCSE names
UCASE,LCASE,VALandSTRare not accepted at A Level. UseTO_UPPER,TO_LOWER,STR_TO_NUM,NUM_TO_STR. - Single quotes make a
CHAR, double quotes make aSTRING.'A'and"A"are different types. - String positions start at 1, not 0.
MID(s, 1, 1)is the first character. RAND(n)never returns n itself, andINTcuts off the fraction rather than rounding.
容易丢掉的分
- IGCSE 的名字
UCASE、LCASE、VAL和STR在 A Level 不被接受。用TO_UPPER、TO_LOWER、STR_TO_NUM、NUM_TO_STR。 - 单引号构成
CHAR,双引号构成STRING。'A'和"A"是不同的类型。 - 字符串位置从 1 开始,不是 0。
MID(s, 1, 1)是第一个字符。 RAND(n)永远不会返回 n 本身,INT是截去小数而不是四舍五入。
You've got it
- design → pseudocode: find the variables and types, then map each flowchart symbol to a statement and trace a small input
- constant set once, changed in one place, named; variable declared with one of six types read off how it is used
←assigns;DIVwhole-number division,MODremainder; precedenceNOT→* / DIV MOD→+ -→ comparisons →AND→OR- use the insert's library routines with their exact 9618 names:
LENGTH,MID,TO_UPPER,NUM_TO_STR,INT,RAND
你掌握了
- 设计 → 伪代码:找出变量和类型,然后把每个流程图符号映射成一条语句,并用小输入跟踪
- 常量设置一次、一处修改、有名字;变量用六种类型之一声明,类型从用法读出
←赋值;DIV整数除法,MOD余数;优先级NOT→* / DIV MOD→+ -→ 比较 →AND→OR- 用附页里的库例程及其确切的 9618 名称:
LENGTH、MID、TO_UPPER、NUM_TO_STR、INT、RAND