Assignment Operators
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.
From Value to Variable
An assignment statement connects a variable name to a value. In the source example, the literal value 5 is assigned to the variable i with the assignment operator =. A later print statement uses i, so the value associated with i appears on the screen.
Reading an Assignment Statement
Read an assignment from left to right as a connection between a name and a value: the name is on the left of =, and the value being assigned is on the right. The source describes the line as a statement because it states that something should be done. In the example, the statement assigns the literal constant value 5 to i.
Assigning 5 to i
Trace what happens when the value 5 is assigned to the variable i and i is then printed.
Assignment: The assignment operator = connects the variable name i to the literal value 5.
Use of the variable: The print statement uses i, which refers to the value assigned to it.
Output: The value of i is printed to the screen, so the displayed value is 5.
The assignment makes 5 the value associated with i, and printing i displays 5.
Assignment and Comparison
| Symbol | Role | Meaning in the source material |
|---|---|---|
| = | Assignment operator | Connects a variable name to a value |
| == | Comparison operator | Used for comparison rather than assignment |
Chained Assignments
Most operators with the same precedence are associated from left to right. Assignment operators are an important exception described in the source: they have right-to-left associativity. Therefore, a chained assignment such as a = b = c is treated as a = (b = c). The assignment involving b and c is considered first, and the result is then used in the assignment involving a.
Following a chained assignment
Interpret the assignment a = b = c according to assignment associativity.
Group from the right: Because assignment associates from right to left, read the expression as a = (b = c).
Handle the inner assignment: The assignment b = c is considered first.
Handle the outer assignment: The resulting assignment is then used as part of a = (b = c).
The expression is interpreted as a = (b = c), not as (a = b) = c.
Other Uses of the Equal Sign
The assignment operator also appears when defining default argument values for function parameters. A parameter can be followed by = and a default value, allowing that value to be used when the user does not provide one. This is another use of assignment syntax, but it occurs in a function definition rather than in the simple variable assignment example.
When reading an equal sign, first identify its context. In a simple statement, = assigns a value to a variable name. In a function definition, it can specify a default argument value. In contrast, == is the comparison operator identified in the source.
Check Your Understanding
Explain in your own words what happens in the source example when 5 is assigned to i and i is then printed. Then state whether the following expression should be read from left to right or right to left: a = b = c.
Hints
- Identify the operator used to connect i and 5.
- Separate the assignment step from the printing step.
- Recall the special associativity rule for assignment operators.
Using == when the goal is to assign a value.
The source distinguishes == as a comparison operator and = as an assignment operator.
Fix:
Use = for assignment and == for comparison.Reading a chained assignment as if it always associates from left to right.
Assignment operators have right-to-left associativity.
Fix:
Interpret the expression as a = (b = c).Writing =< or => as alternatives to the assignment syntax.
The source explicitly states that there is no such thing as =< or =>.
Fix:
Use the assignment operator = when assigning a value.
Key Takeaways
- The assignment operator = connects a variable name to a value.
- After 5 is assigned to i, printing i displays 5.
- The assignment operator = is different from the comparison operator ==.
- Assignment operators associate from right to left, so a = b = c is treated as a = (b = c).
- The equal sign can also introduce default argument values in a function definition.