Concepts / Variables and Assignment

Variables and Assignment

Assignment is the connection between a variable name and a value. It gives the program a named value that can be used later.

  • Programming

A Name for a Value

A variable gives a program a name through which it can work with a value. In the basic example from this topic, the name is i and the value is 5. The assignment operator, written as =, connects the variable name i to the value 5.

Key point

Assignment is the connection between a variable name and a value. It gives the program a named value that can be used later.

ivariable name5assigned valueconnected to
What does the program's state look like after assigning 5 to i, and how is the name i connected to the value 5?

Following the First Assignment

The program handles the basic example in order. First, it assigns the literal constant value 5 to the variable i. This assignment is written as i = 5. The line is a statement because it states that something should be done: connect the name i to the value 5.

Assign 5 to ii = 5Print idisplay the value5shown on screennextdisplays
What happens first, and how does the program move from assigning 5 to i to printing the result?

Using the Assigned Value

After the assignment connects i to 5, the program uses the variable name when it needs that value. The next step is to print i. The print statement prints the value of the variable to the screen, so the displayed result is 5.

ivariable name5value connected to iScreen5namesprint displays
How does print(i) use the variable name to retrieve and display the value 5?
In the real world

Think of i as a label attached to the value 5. The label gives the program a way to refer to that value. When the program prints i, it uses the label and displays the value connected to it.

Initialization and Updates

Definition

Initialization is an assignment that gives an initial value to a variable that will later be updated.

Assignment is also commonly used to update a variable. In that pattern, an assignment gives the variable a new value that depends on its old value. Before such an update can happen, the variable has to be initialized, usually with a simple assignment.

Key point

Initialization establishes the variable's starting value. Updating then changes the variable using an assignment whose new value depends on the old value.

Mistakes with Assignment

  • Common mistake

    Treating assignment as unrelated to later output.

    What it looks like

    A learner sees the assignment of 5 to i and does not connect it to the later printing of i.

    Why it fails

    The assignment connects i to 5, and the print statement prints the value connected to i.

    Do this instead

    Trace the program in order: first connect i to 5, then print i, which displays 5.

  • Common mistake

    Trying to update a variable before giving it an initial value.

    What it looks like

    A learner attempts to use an update pattern for a variable that has not been initialized.

    Why it fails

    A variable must be initialized before it can be updated.

    Do this instead

    Give the variable an initial value with a simple assignment before applying an update.

  • Common mistake

    Confusing the variable name with the value.

    What it looks like

    Treating i and 5 as if they were the same thing.

    Why it fails

    i is the variable name, while 5 is the value connected to that name.

    Do this instead

    Keep the roles separate: the name provides a way to refer to the value.

Best practice

When tracing an assignment, write down three parts: the variable name, the value connected to it, and the next statement that uses the name. This makes the program's state and execution order explicit.

Check Your Trace

EASY

Trace this sequence: first assign the value 5 to i, then print i. What value is connected to i after the assignment, and what value appears on the screen?

Hints
  • Identify the value assigned to i.
  • Follow the next statement, which prints the value of i.
Worked example

Tracing i = 5

Determine the program state after assigning 5 to i and then printing i.

  1. 1
    Assignment

    The assignment operator connects the variable name i to the literal constant value 5.

  2. 2
    State

    The variable i now refers to the assigned value 5.

  3. 3
    Printing

    The print statement uses i and displays the value connected to that variable name.

Result

The value connected to i is 5, and 5 is printed on the screen.

Key Takeaways

  1. 1
    A variable is a name connected to a value.
  2. 2
    The assignment operator = makes that connection.
  3. 3
    An assignment line is a statement describing an action the program should perform.
  4. 4
    Printing a variable displays the value connected to that variable.
  5. 5
    A variable must be initialized before it can be updated.

At a glance

  • Assignment connects a variable name to a value.
  • In the source example, i is connected to 5.
  • The program assigns first and then uses print to display the value of i.
  • Initialization gives a variable its starting value before an update.

More concepts starting with V