Variables and Assignments · Variables y Asignaciones
| English | Español |
|---|---|
| variables/ˈveərɪəblz/ | variables |
| named reference/neɪmd ˈrefrəns/ | referencia nombrada |
| value/ˈvæljuː/ | valor |
| memory/ˈmeməri/ | memoria |
| assignment/əˈsaɪnmənt/ | asignación |
| update/ˈʌpdeɪt/ | actualización |
| data types/ˈdeɪtə taɪps/ | tipos de datos |
| number/ˈnʌmbə/ | número |
| string/strɪŋ/ | cadena |
| Boolean/ˈbuːlɪən/ | Boolean |
| executes/ˈeksɪkjuːts/ | ejecuta |
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.
Una variable es una caja etiquetada
- Un programa en ejecución necesita recordar información; utiliza variables 变量.
- Una variable es una referencia con nombre 命名引用 que almacena un valor 值 en la memoria 内存 del ordenador.
- Imagina una caja con etiqueta: la etiqueta es el nombre, y la caja contiene un valor a la vez.
- Dale un nombre claro como
totalScore, no uno vago comox.
A variable is best described as: · Una variable se describe mejor como:
The name is the label; the box holds the current value. · El nombre es la etiqueta; la caja contiene el valor actual.
In score ← 10, the arrow means score ______ the value 10. · En score ← 10, la flecha significa que score ______ el valor 10.
Assignment stores the right-hand value into the left-hand name. · La asignación almacena el valor del lado derecho en el nombre del lado izquierdo.
Assigning a new value to a variable throws away its old value. · Asignar un nuevo valor a una variable elimina su valor antiguo.
A variable holds one value at a time; the new one overwrites the old. · Una variable almacena un solo valor a la vez; el nuevo sobrescribe al antiguo.
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.
Asignación
- Una asignación 赋值 introduce un valor en una variable. En pseudocódigo AP: $\text{score} \leftarrow 10$.
- La flecha $\leftarrow$ significa "obtiene el valor", por lo que
scoreahora contiene $10$. - Una asignación también puede actualizar 更新 una variable con un nuevo valor; el valor anterior se descarta.
- El lado derecho se calcula primero, luego se almacena en el nombre del lado izquierdo.
A variable is a labelled box · Una variable es una caja etiquetada
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 asignación calcula el lado derecho y lo almacena en una caja con nombre; reasignar el mismo nombre lo sobrescribe. Avanza paso a paso y observa cómo cada caja toma su valor.
Match each value to its data type. · Empareja cada valor con su tipo de dato.
Numbers, strings, and Booleans are the basic data types here. · Números, cadenas y Booleanos son los tipos de datos básicos aquí.
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 datos
- Una variable puede contener diferentes clases de valor, llamadas tipos de datos 数据类型:
- un número 数字, como $7$ o $3.5$;
- una cadena 字符串, un fragmento de texto como "cat";
- un Booleano 布尔值, que es verdadero o falso.
total ← 5, then total ← total + 3, then total ← total × 2. What is total? · total ← 5, luego total ← total + 3, luego total ← total × 2. ¿Cuál es total?
5 → 8 → 16, each line using the previous value. · 5 → 8 → 16, cada línea usa el valor anterior.
Why prefer the name totalScore over x? · ¿Por qué preferir el nombre totalScore sobre x?
Readable names help you and your teammates understand the code. · Los nombres legibles ayudan a ti y a tus compañeros a entender el 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.
Rastreo de cambios
- A medida que un programa se ejecuta 执行, el valor de una variable puede cambiar.
- Rastrear estos cambios a mano ayuda a entender el programa.
Rastrea total. $\text{total} \leftarrow 5$ almacena 5. $\text{total} \leftarrow \text{total} + 3$ lee 5, suma 3, almacena 8. $\text{total} \leftarrow \text{total} \times 2$ lee 8, almacena 16. Por lo tanto, total termina siendo 16 — cada línea usa el valor de la línea 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.
Una variable es una caja con nombre que almacena un solo valor en la memoria. Una asignación ($\leftarrow$) calcula el lado derecho y luego lo almacena en el nombre del lado izquierdo, sobrescribiendo el valor anterior. Las variables contienen tipos de datos — número, cadena, Booleano — y sus valores cambian mientras el programa se ejecuta.