Variables and Assignments · Variáveis e Atribuições
| English | Português |
|---|---|
| variables/ˈveərɪəblz/ | variáveis |
| named reference/neɪmd ˈrefrəns/ | referência nomeada |
| value/ˈvæljuː/ | valor |
| memory/ˈmeməri/ | memória |
| assignment/əˈsaɪnmənt/ | atribuição |
| update/ˈʌpdeɪt/ | atualização |
| data types/ˈdeɪtə taɪps/ | tipos de dados |
| number/ˈnʌmbə/ | number |
| string/strɪŋ/ | string (cadeia de caracteres) |
| Boolean/ˈbuːlɪən/ | Boolean |
| executes/ˈeksɪkjuːts/ | executa |
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.
Uma variável é uma caixa rotulada
- Um programa em execução precisa lembrar informações — ele usa variáveis 变量.
- Uma variável é uma referência nomeada 命名引用 que armazena um valor 值 na memória 内存 do computador.
- Imagine uma caixa rotulada: o rótulo é o nome, a caixa armazena um único valor por vez.
- Dê a ela um nome claro como
totalScore, não um vagox.
A variable is best described as: · Uma variável é melhor descrita como:
The name is the label; the box holds the current value. · O nome é o rótulo; a caixa contém o valor atual.
In score ← 10, the arrow means score ______ the value 10. · Em score ← 10, a seta significa score ______ o valor 10.
Assignment stores the right-hand value into the left-hand name. · Atribuição armazena o valor da direita no nome da esquerda.
Assigning a new value to a variable throws away its old value. · Atribuir um novo valor a uma variável descarta seu valor antigo.
A variable holds one value at a time; the new one overwrites the old. · Uma variável guarda um valor de cada vez; o novo sobrescreve o antigo.
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.
Atribuição
- Uma atribuição 赋值 coloca um valor em uma variável. Em pseudocódigo AP: $\text{score} \leftarrow 10$.
- A seta $\leftarrow$ significa "recebe o valor", então
scoreagora contém $10$. - Uma atribuição também pode atualizar 更新 uma variável com um novo valor — o valor antigo é descartado.
- O lado direito é calculado primeiro, depois armazenado no nome do lado esquerdo.
A variable is a labelled box · Uma variável é uma caixa rotulada
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. · Cada atribuição calcula o lado direito e armazena em uma caixa nomeada; reatribuir o mesmo nome sobrescreve o anterior. Passe linha por linha e observe cada caixa receber seu valor.
Match each value to its data type. · Combine cada valor com seu tipo de dado.
Numbers, strings, and Booleans are the basic data types here. · Números, strings (cadeias) e Booleans são os tipos de dados básicos aqui.
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.
Tipos de dados
- Uma variável pode conter diferentes tipos de valores, chamados tipos de dados 数据类型:
- um número 数字, como $7$ ou $3.5$;
- uma string 字符串, um trecho de texto como "cat";
- um Boolean 布尔值, que pode ser true (verdadeiro) ou false (falso).
total ← 5, then total ← total + 3, then total ← total × 2. What is total? · total ← 5, depois total ← total + 3, depois total ← total × 2. Qual é total?
5 → 8 → 16, each line using the previous value. · 5 → 8 → 16, cada linha usando o valor anterior.
Why prefer the name totalScore over x? · Por que preferir o nome totalScore sobre x?
Readable names help you and your teammates understand the code. · Nomes legíveis ajudam você e seus colegas a entenderem o código.
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.
Rastreamento de alterações
- À medida que um programa executa 执行, o valor de uma variável pode mudar.
- Rastrear essas alterações manualmente ajuda você a entender o programa.
Rastreie total. $\text{total} \leftarrow 5$ armazena 5. $\text{total} \leftarrow \text{total} + 3$ lê 5, adiciona 3, armazena 8. $\text{total} \leftarrow \text{total} \times 2$ lê 8, armazena 16. Então total termina como 16 — cada linha usa o valor da linha anterior.
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.
Uma variável é uma caixa nomeada que armazena um único valor na memória. Uma atribuição ($\leftarrow$) calcula o lado direito e depois armazena-o no nome do lado esquerdo, sobrescrevendo o valor antigo. Variáveis contêm tipos de dados — número, string, Boolean — e seus valores mudam à medida que o programa executa.