Variables
An assignment statement tells the computer to store a value in a variable.
Why Programs Need Named Storage
A program works with data: numbers it reads, results it calculates, and text it displays. To use that data, the program needs a way to refer to it. A variable provides that way. Once you understand variables, you can read almost any program, because nearly every program depends on storing values and reusing them later.
What do you think happens?
Given the statement interest = principal * 0.07; what does the variable interest hold after the statement runs?
Reveal answer
The result of multiplying principal by 0.07
The right side is computed first, using the data currently held in principal. The assignment then stores that resulting value in interest, which is the destination on the left side.
A Variable as a Named Memory Location
A variable is a named memory location that can hold data. The name identifies the location, while the location holds the data.
Think of a variable as a labeled storage place. The label is the name, such as score. The place is a memory location set aside for that name. The contents are the data currently stored there, such as the number 90. These three pieces are related but distinct: the name points to the location, and the location holds the data. The diagram below shows this relationship for a variable named score.
Reading an Assignment Statement
An assignment statement tells the computer to store a value in a variable. The general form is variable = expression. The left side is the destination, the variable that will receive the value. The right side is the expression, which describes the value to be produced. The equals sign performs the storing: it takes whatever the right side produces and places it into the variable on the left.
- interest: destination, the variable that receives the result
- principal: existing data used in the computation
- * 0.07: the computation applied to principal
- =: stores the computed value into interest
Tracing a Computation into Storage
Assignment statements connect computation with storage. A program can use data already held in a variable, perform a computation with that data, and store the resulting value in a variable. Variable manipulation means using variables in operations to produce new values. Notice that the statement combines two actions: it computes with data, and it stores the computation's result.
A useful habit when reading or debugging an assignment is to separate the computation from the destination. First ask what the right side computes, using the values currently stored. Then ask which variable on the left receives that result. The same variable can appear on both sides, as in the example below, where x is read on the right and then overwritten on the left.
x = 5;x holds 5
x = x + 1;x holds 6
Computing interest from a principal
principal holds 200. The statement interest = principal * 0.07; runs. What does interest hold afterward?
- 1Read the current data
The right side uses principal, which holds 200.
- 2Perform the computation
Multiply 200 by 0.07, which gives 14.
- 3Identify the destination
The left side names interest as the variable that receives the result.
- 4Store the result
The equals sign places 14 into interest. principal still holds 200.
interest holds 14, and principal is unchanged at 200.
Mistakes Beginners Make with Assignments
Common mistake Reading the left side as a value to compare rather than a destination
What it looks liketotal = price * quantity;Why it failsIn an assignment, the left side receives the stored result. The statement does not ask whether the two sides are equal.
Do this insteadRead the statement as: compute the right side, then store the result in the variable on the left.
Common mistake Assuming the variable on the right is changed by the statement
What it looks likeinterest = principal * 0.07; expecting principal to changeWhy it failsprincipal is only used in the computation. Only the destination on the left receives the new value.
Do this insteadCheck the left side to find which variable is updated. Here, only interest changes.
Common mistake Thinking the right side is stored before it is computed
Why it failsThe expression must produce its value first. The storing happens after the computation.
Names Versus Memory Locations
A program could, in principle, refer to storage by its memory location directly. Variables make this unnecessary for the programmer. The programmer refers to a variable by its name, not by its memory location. This keeps attention on meaningful names such as score rather than on the physical location of the storage.
| Feature | Referring to a memory location directly | Referring to a variable name |
|---|---|---|
| What the programmer writes | The location itself | A meaningful name such as score |
| What the program is focused on | The physical storage | The meaning of the data |
| Who tracks the location | The programmer | The compiler keeps track of the memory location associated with the name |
Using a name lets the programmer work with data without managing the location of its storage.
The Compiler's Role in Tracking Locations
When a program uses the name score, something must connect that name to the storage it refers to. The compiler is responsible for keeping track of the memory location associated with each name. Every later use of score in the program therefore reaches the same storage, and the programmer does not have to record or look up that location.
Tracing a Price Calculation
Suppose price holds 4 and quantity holds 3. The statement total = price * quantity; runs. Before you check the answer, decide: which variable receives a new value, what value does it hold afterward, and do price and quantity change?
Hints
- Find the destination on the left side of the equals sign.
- Compute the right side first, using the values currently stored in price and quantity.
- Only the destination receives a new value.
Key Takeaways
- An assignment statement, of the form variable = expression, stores the value produced by the expression in the variable on the left.
- A variable is a named memory location that can hold data. The name identifies the location, and the location holds the data.
- A computation can use data already stored in variables, and its result is stored in a destination variable, which may be the same variable used in the computation.
- Programmers refer to variables by name rather than memory location, and the compiler tracks the memory location associated with each name.