
Using Variables and
Performing Calculations
Variables hold values in memory so a program can manipulate them. Different kinds of variables
hold different types of data: numbers, text, pictures, Halo scores, even complex groups of data
such as employee records.
In this lesson you learn what variables are and how to use them. You learn how to define
variables, put data in them, and use them to perform simple calculations.
WHAT ARE VARIABLES?
Technically speaking a variable is a named piece of memory that can hold some data of a specific
type. For example, a program might allocate 4 bytes of memory to store an integer. You might
name those bytes “payoffs” so you can easily refer to them in the program’s code.
Less technically, you can think of a variable as a named place to put a piece of data. The
program’s code can use the variables to store values and perform calculations. For example,
a program might store two values in variables, add the values together, and store the result
in a third variable.
DATA TYPES
Every variable has a particular data type that determines the kind of data that it can hold. In
general, you cannot place data of one type in a variable of another. For example, if
price is a
variable that can hold a number in dollars and cents, you cannot put the string “Hello” in it.
If you like, you can think of a variable as an envelope (with a name written on the outside)
that ...