Concepts / Assignment Operators

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.

  • Programming

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.

ii5literal valueassignment connects name to value
What changes when the value 5 is assigned to the variable i?

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.

Worked example

Assigning 5 to i

Trace what happens when the value 5 is assigned to the variable i and i is then printed.

  1. 1
    Assignment

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

  2. 2
    Use of the variable

    The print statement uses i, which refers to the value assigned to it.

  3. 3
    Output

    The value of i is printed to the screen, so the displayed value is 5.

Result

The assignment makes 5 the value associated with i, and printing i displays 5.

Assignment and Comparison

SymbolRoleMeaning in the source material
=Assignment operatorConnects a variable name to a value
==Comparison operatorUsed 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.

Worked example

Following a chained assignment

Interpret the assignment a = b = c according to assignment associativity.

  1. 1
    Group from the right

    Because assignment associates from right to left, read the expression as a = (b = c).

  2. 2
    Handle the inner assignment

    The assignment b = c is considered first.

  3. 3
    Handle the outer assignment

    The resulting assignment is then used as part of a = (b = c).

Result

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.

Key point

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

EASY

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 like

    Treating the comparison operator as if it were the assignment operator.

    Why it fails

    The source distinguishes == as a comparison operator and = as an assignment operator.

    Do this instead
    Use = for assignment and == for comparison.
  • Common mistake

    Reading a chained assignment as if it always associates from left to right.

    What it looks like
    Interpreting a = b = c as (a = b) = c.
    Why it fails

    Assignment operators have right-to-left associativity.

    Do this instead
    Interpret the expression as a = (b = c).
  • Common mistake

    Writing =< or => as alternatives to the assignment syntax.

    Why it fails

    The source explicitly states that there is no such thing as =< or =>.

    Do this instead

    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.

Related concepts

More concepts starting with A