Operators, strings and subroutines · 运算符、字符串与子程序
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| operators/ˈɒpəreɪtəz/ | 运算符 | yùn suàn fú |
| procedure/prəˈsiːdʒə/ | 过程 | guò chéng |
| function/ˈfʌŋkʃn/ | 函数 | hán shù |
| relational/rɪˈleɪʃənl/ | 关系 | guān xì |
| logical/ˈlɒdʒɪkl/ | 逻辑 | luó jí |
| parameter/pəˈræmɪtə/ | 参数 | cān shù |
| local variable/ˈləʊkl ˈveərɪəbl/ | 局部变量 | jú bù biàn liàng |
| global/ˈɡləʊbl/ | 全局 | quán jú |
The tools of a program
- Programs combine values with operators 运算符 and work with strings.
- To avoid repeating code, we use procedures 过程 and functions 函数.
- Good naming and comments make a program maintainable.
一个程序的工具
- 程序用运算符(operators)组合值并处理字符串(strings)。
- 为了避免重复代码,我们使用过程(procedures)和函数(functions)。
- 好的命名和注释使一个程序可维护(maintainable)。
Operators
- Arithmetic:
+ - * /,^(power),MOD(remainder),DIV(whole-number part). e.g.17 MOD 5 = 2,17 DIV 5 = 3. - Relational 关系 (give a Boolean):
= < <= > >= <>(not equal). - Logical 逻辑:
AND(both true),OR(at least one),NOT(reverses).
A pre-condition (WHILE) loop tests before the body, so it may run zero times; a post-condition (REPEAT) loop tests after, so it runs at least once.
运算符
- 算术(arithmetic):
+ - * /、^(幂)、MOD(余数)、DIV(整数部分)。例如17 MOD 5 = 2、17 DIV 5 = 3。 - 关系(relational,给出一个布尔值):
= < <= > >= <>(不等于)。 - 逻辑(logical):
AND(两者都真)、OR(至少一个)、NOT(取反)。

一个前置条件(WHILE)循环在主体之前测试,所以它可能运行零次;一个后置条件(REPEAT)循环在之后测试,所以它至少运行一次。
What is 17 MOD 5? · 17 MOD 5 是多少?
MOD gives the remainder: 17 ÷ 5 = 3 remainder 2. · MOD 给出余数:17 ÷ 5 = 3 余 2。
What is 17 DIV 5? · 17 DIV 5 是多少?
DIV gives the whole-number part: 17 ÷ 5 = 3 (remainder discarded). · DIV 给出整数部分:17 ÷ 5 = 3(余数被丢弃)。
String handling
- A string is made of characters. Useful operations:
- LENGTH (number of characters), SUBSTRING (a part), UCASE (capitals), LCASE (small).
A variable is a named store whose value can change
字符串处理
- 一个字符串由字符构成。有用的操作:
- LENGTH(字符的数量)、SUBSTRING(一个部分)、UCASE(大写)、LCASE(小写)。
name ← "Computer"
OUTPUT LENGTH(name) // 8
OUTPUT SUBSTRING(name, 1, 4) // "Comp"
SUBSTRING: take part of a string · SUBSTRING:取出字符串的一部分
The first character is at position 1. SUBSTRING(Word, Start, Length) takes Length characters beginning at Start — drag the two handles and read the call it builds. · 第一个字符的位置是 1。SUBSTRING(Word, Start, Length) 从 Start 开始取 Length 个字符——拖动两个滑块,读出它组成的调用。
What does LENGTH("Computer") return? · LENGTH("Computer") 返回什么?
"Computer" has 8 characters. · "Computer" 有 8 个字符。
Match each operator or string function to what it gives. · 把每个运算符或字符串函数匹配到它给出什么。
MOD/DIV split a division into remainder and quotient; LENGTH and SUBSTRING work on strings. · MOD/DIV 把一个除法分成余数和商;LENGTH 和 SUBSTRING 处理字符串。
Procedures, functions and good style
- A procedure does a task (run with
CALL); a function returns a value. - A parameter 参数 is a value passed in (up to three).
- A local variable 局部变量 exists only inside its subroutine (safer); a global 全局 is visible everywhere.
- Library routines:
MOD,DIV,ROUND,RANDOM. - A maintainable program uses meaningful names, comments, and subroutines.
The basic data types: integer, real, char, string and boolean
过程、函数和好的风格
- 一个过程(procedure)做一个任务(用
CALL运行);一个函数(function)返回一个值。 - 一个参数(parameter)是传入的一个值(最多三个)。
- 一个局部变量(local variable)只在它的子程序内部存在(更安全);一个全局(global)在到处可见。
- 库程序(library routines):
MOD、DIV、ROUND、RANDOM。 - 一个可维护的程序使用有意义的名称、注释和子程序。
A local variable: · 一个局部变量:
Locals are safer because other parts of the program cannot change them by mistake. · 局部变量更安全,因为程序的其他部分不能错误地改变它们。
A function returns a value (used in an expression) while a procedure just performs a task, and a local variable can only be used inside the subroutine where it is declared. · 一个函数返回一个值(在一个表达式中使用),而一个过程只执行一个任务,且一个局部变量只能在它被声明的子程序内部使用。
Functions return values; locals keep variables safe inside their own subroutine — both make code easier to maintain. · 函数返回值;局部变量在它们自己的子程序内部保持变量安全——两者都使代码更容易维护。
You've got it
MOD= remainder,DIV= whole-number part; logicalAND/OR/NOT- strings: LENGTH, SUBSTRING, UCASE, LCASE
- a procedure does a task; a function returns a value; parameters pass values in
- local variables are safer than global; maintainable = meaningful names + comments + subroutines
你掌握了
MOD= 余数,DIV= 整数部分;逻辑AND/OR/NOT- 字符串:LENGTH、SUBSTRING、UCASE、LCASE
- 一个过程做一个任务;一个函数返回一个值;参数传入值
- 局部变量比全局更安全;可维护 = 有意义的名称 + 注释 + 子程序