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.
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.
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.
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.
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
Common mistake Treating assignment as unrelated to later output.
What it looks likeA learner sees the assignment of 5 to i and does not connect it to the later printing of i.
Why it failsThe assignment connects i to 5, and the print statement prints the value connected to i.
Do this insteadTrace 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 likeA learner attempts to use an update pattern for a variable that has not been initialized.
Why it failsA variable must be initialized before it can be updated.
Do this insteadGive 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 likeTreating i and 5 as if they were the same thing.
Why it failsi is the variable name, while 5 is the value connected to that name.
Do this insteadKeep 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
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.
- 1Assignment
The assignment operator connects the variable name i to the literal constant value 5.
- 2State
The variable i now refers to the assigned value 5.
- 3Printing
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
- 1A variable is a name connected to a value.
- 2The assignment operator = makes that connection.
- 3An assignment line is a statement describing an action the program should perform.
- 4Printing a variable displays the value connected to that variable.
- 5A 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.