Concepts / Variables and Assignment

Variables and Assignment

How It Works Here's how this program works. First, we assign the literal constant value 5 to the variable i using the assignment operator ( = ). This line is called a statement because it states that something should be done and in this case, we connect the variable name i to the value 5 . Next, we print the value of i using the print statement which, unsurprisingly, just prints the value of the variable to the screen.

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

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

connected toivariable name5assigned value
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.

nextdisplaysAssign 5 to ii = 5Print idisplay the value5shown on screen
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.

namesprint displaysivariable name5value connected to iScreen5
How does print(i) use the variable name to retrieve and display the value 5?

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

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.

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

  • Treating assignment as unrelated to later output.

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

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

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

    A variable must be initialized before it can be updated.

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

  • Confusing the variable name with the value.

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

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

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.

Tracing i = 5

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

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

State: The variable i now refers to the assigned value 5.

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

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

Key Takeaways

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

Key Takeaways

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