Variables and Assignments
Introduced| English | Meaning |
|---|---|
| variables/ˈveərɪəblz/ | A quantity or named value that can change or take different values. |
| named reference/neɪmd ˈrefrəns/ | named reference |
| value/ˈvæljuː/ | An amount, number or item assigned to a variable or expression, or held by a data object. |
| memory/ˈmeməri/ | memory |
| assignment/əˈsaɪnmənt/ | Storing a value in a variable or other named location. |
| update/ˈʌpdeɪt/ | update |
| data types/ˈdeɪtə taɪps/ | A classification of values that determines their representation and permitted operations. |
| number/ˈnʌmbə/ | number |
| string/strɪŋ/ | A sequence of characters treated as a single data value. |
| Boolean/ˈbuːlɪən/ | A logical value that is either true or false. |
| executes/ˈeksɪkjuːts/ | executes |
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.
A variable is best described as:
Difficulty 1/5 · 100 coins + a small bonus on first completion today
The name is the label; the box holds the current value.
In score ← 10, the arrow means score ______ the value 10.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Assignment stores the right-hand value into the left-hand name.
Assigning a new value to a variable keeps its old value as well.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
False. A variable holds one value at a time; the new value overwrites the old one.
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.

A variable is a named store whose value can change
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.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
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.
total ← 5, then total ← total + 3, then total ← total × 2. What is total?
Difficulty 2/5 · 140 coins + a small bonus on first completion today
5 → 8 → 16, each line using the previous value.
Why prefer the name totalScore over x?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
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.
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.