Variables and Assignments · 变量与赋值
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| variables/ˈveərɪəblz/ | 变量 | biàn liàng |
| named reference/neɪmd ˈrefrəns/ | 命名引用 | mìng míng yǐn yòng |
| value/ˈvæljuː/ | 值 | zhí |
| memory/ˈmeməri/ | 内存 | nèi cún |
| assignment/əˈsaɪnmənt/ | 赋值 | fù zhí |
| update/ˈʌpdeɪt/ | 更新 | gēng xīn |
| data types/ˈdeɪtə taɪps/ | 数据类型 | shù jù lèi xíng |
| number/ˈnʌmbə/ | 数字 | shù zì |
| string/strɪŋ/ | 字符串 | zì fú chuàn |
| Boolean/ˈbuːlɪən/ | 布尔值 | bù ěr zhí |
| executes/ˈeksɪkjuːts/ | 执行 | zhí xíng |
A variable is a labelled box
- A running program needs to remember information — it uses variables 变量.
- A variable is a named reference 命名引用 that stores a value 值 in the computer's memory 内存.
- Picture a labelled box: the label is the name, the box holds one value at a time.
- Give it a clear name like
totalScore, not a vaguex.
变量是一个有标签的盒子
- 一个运行的程序需要记住信息——它使用变量(variables)。
- 一个变量是一个命名引用(named reference),把一个值(value)存储在计算机的内存(memory)里。
- 想象一个有标签的盒子:标签是名字,盒子一次装一个值。
- 给它一个清晰的名字如
totalScore,而不是含糊的x。
A variable is best described as: · 变量最好被描述为:
The name is the label; the box holds the current value. · 名字是标签;盒子装当前的值。
In score ← 10, the arrow means score ______ the value 10. · 在 score ← 10 中,箭头意思是 score ______ 值 10。
Assignment stores the right-hand value into the left-hand name. · 赋值把右边的值存进左边的名字。
Assigning a new value to a variable throws away its old value. · 给变量赋一个新值会扔掉它的旧值。
A variable holds one value at a time; the new one overwrites the old. · 变量一次装一个值;新的覆盖旧的。
Assignment
- An assignment 赋值 puts a value into a variable. In AP pseudocode: $\text{score} \leftarrow 10$.
- The arrow $\leftarrow$ means "gets the value", so
scorenow holds $10$. - An assignment can also update 更新 a variable with a new value — the old value is thrown away.
- The right side is worked out first, then stored in the name on the left.
赋值
- 赋值(assignment)把一个值放进变量。在 AP 伪代码中:$\text{score} \leftarrow 10$。
- 箭头 $\leftarrow$ 意思是"取得这个值",所以
score现在装着 $10$。 - 赋值也能用一个新值更新(update)变量——旧值被扔掉。
- 右边先被算出,然后存进左边的名字。
A variable is a labelled box · 变量是一个有标签的盒子
Each assignment works out the right side and stores it in a named box; reassigning the same name overwrites it. Step through and watch each box take its value. · 每个赋值算出右边并存进一个命名的盒子;重新赋值同一个名字会覆盖它。逐步走过,看每个盒子取它的值。
Match each value to its data type. · 把每个值与其数据类型配对。
Numbers, strings, and Booleans are the basic data types here. · 数字、字符串和布尔值是这里的基本数据类型。
Data types
- A variable can hold different kinds of value, called data types 数据类型:
- a number 数字, such as $7$ or $3.5$;
- a string 字符串, a piece of text such as "cat";
- a Boolean 布尔值, which is either true or false.
数据类型
- 一个变量能装不同种类的值,叫数据类型(data types):
- 一个数字(number),如 $7$ 或 $3.5$;
- 一个字符串(string),一段文本如 "cat";
- 一个布尔值(Boolean),它要么真要么假。
total ← 5, then total ← total + 3, then total ← total × 2. What is total? · total ← 5,然后 total ← total + 3,然后 total ← total × 2。total 是多少?
5 → 8 → 16, each line using the previous value. · 5 → 8 → 16,每一行用前一个值。
Why prefer the name totalScore over x? · 为什么名字 totalScore 比 x 更好?
Readable names help you and your teammates understand the code. · 可读的名字帮助你和队友理解代码。
Tracing changes
- As a program executes 执行, the value of a variable can change.
- Tracing these changes by hand helps you understand the program.
Trace total. $\text{total} \leftarrow 5$ stores 5. $\text{total} \leftarrow \text{total} + 3$ reads 5, adds 3, stores 8. $\text{total} \leftarrow \text{total} \times 2$ reads 8, stores 16. So total ends as 16 — each line uses the value from the line before.
追踪变化
- 当程序执行(executes)时,变量的值可以改变。
- 用手追踪这些变化有助于你理解程序。
追踪 total。 $\text{total} \leftarrow 5$ 存 5。$\text{total} \leftarrow \text{total} + 3$ 读 5,加 3,存 8。$\text{total} \leftarrow \text{total} \times 2$ 读 8,存 16。所以 total 最终是 16——每一行都用上一行的值。
A variable is a named box that stores one value in memory. An assignment ($\leftarrow$) works out the right side, then stores it in the name on the left, overwriting the old value. Variables hold data types — number, string, Boolean — and their values change as the program executes.
一个变量是一个命名的盒子,把一个值存在内存里。一个赋值($\leftarrow$)先算出右边,再存进左边的名字,覆盖旧值。变量装数据类型——数字、字符串、布尔值——它们的值随程序执行而改变。