Assignment Operators
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.
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.
- 1Assignment
The assignment operator = connects the variable name i to the literal value 5.
- 2Use of the variable
The print statement uses i, which refers to the value assigned to it.
- 3Output
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 |
The number of equal signs changes the role of the operator.
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.
- 1Group from the right
Because assignment associates from right to left, read the expression as a = (b = c).
- 2Handle the inner assignment
The assignment b = c is considered first.
- 3Handle 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.
Common mistake Using == when the goal is to assign a value.
What it looks likeTreating the comparison operator as if it were the assignment operator.
Why it failsThe source distinguishes == as a comparison operator and = as an assignment operator.
Do this insteadUse = for assignment and == for comparison.Common mistake Reading a chained assignment as if it always associates from left to right.
What it looks likeInterpreting a = b = c as (a = b) = c.Why it failsAssignment operators have right-to-left associativity.
Do this insteadInterpret the expression as a = (b = c).Common mistake Writing =< or => as alternatives to the assignment syntax.
Why it failsThe source explicitly states that there is no such thing as =< or =>.
Do this insteadUse 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.