Concepts / Variables

Variables

An assignment statement tells the computer to store a value in a variable.

  • Programming

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?

  • The value of principal, unchanged

  • The value 0.07

  • The result of multiplying principal by 0.07

Reveal answer
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

Definition

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.

scorevariable nameMemory locationa memory location90data held thererefers toholds
When the variable score is given the value 90, where does the name sit relative to the memory location and the stored data?

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.

scorevariable receiving theresult=stores the resultpointsdata already in a variable*computation2constant value
In the statement score = points * 2, which token is the variable receiving the result, which is the expression being computed, and which symbol performs the storing?
Notes on the code
  • 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.

Before
x = 5;

x holds 5

After
x = x + 1;

x holds 6

x5x6
In the statement x = x + 1, what is the value of x before the statement runs and after it runs, and where does the computed result go?
Worked example

Computing interest from a principal

principal holds 200. The statement interest = principal * 0.07; runs. What does interest hold afterward?

  1. 1
    Read the current data

    The right side uses principal, which holds 200.

  2. 2
    Perform the computation

    Multiply 200 by 0.07, which gives 14.

  3. 3
    Identify the destination

    The left side names interest as the variable that receives the result.

  4. 4
    Store the result

    The equals sign places 14 into interest. principal still holds 200.

Result

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 like
    total = price * quantity;
    Why it fails

    In an assignment, the left side receives the stored result. The statement does not ask whether the two sides are equal.

    Do this instead

    Read 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 like
    interest = principal * 0.07; expecting principal to change
    Why it fails

    principal is only used in the computation. Only the destination on the left receives the new value.

    Do this instead

    Check 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 fails

    The 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.

FeatureReferring to a memory location directlyReferring to a variable name
What the programmer writesThe location itselfA meaningful name such as score
What the program is focused onThe physical storageThe meaning of the data
Who tracks the locationThe programmerThe 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

EASY

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.