Chained assignment

A statement like w = x = y = z is called a chained assignment in which the value of z is assigned to multiple variables w, x, and y. Chained assignments are often used to initialize multiple variables, as in

a = b = c = d = f = 0

Not all programming languages support chained assignment.

In some programming languages (C for example), chained assignments are supported because assignments return values. In C++ they are also available for values of class types by making appropriate return type for the assignment operator. This is however strongly discouraged because it also allows values of class types to be assigned to temporary objects, which results in a statement with no effect (assignment to temporary objects is detected as error only for non-class types).

In Python, assignment statements are not expressions and thus do not return a value, but chained assignment is recognized and supported as a special case of the assignment statement.

Parallel assignment

Some programming languages, such as JavaScript (since 1.7), occam 2,[9] Perl,[10] Python,[11] REBOL, Ruby,[12], C++ (since C++11 using tuples), and Windows PowerShell allow several variables to be assigned in parallel, with syntax like:

a,b := 0,1

which simultaneously assigns 0 to a and 1 to b. If the right-hand side of the assignment is an array variable, this feature is sometimes called sequence unpacking:

var list := {0, 1}
a,b := list

The list will be unpacked so that 0 is assigned to a and 1 to b. More interestingly,

a,b := b,a

Swaps the values of a and b. In languages without parallel assignment, this would have to be written to use a temporary variable

var t := a
a := b
b := t

since a:=b ; b:=a leaves both a and b with the original value of b.

Parallel assignment was introduced in CPL in 1963, under the name simultaneous assignment.[13]

Assignment versus equality

    A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957 and has blindly been copied by armies of language designers. Why is it a bad idea? Because it overthrows a century old tradition to let “=” denote a comparison for equality, a predicate which is either true or false. But Fortran made it to mean assignment, the enforcing of equality. In this case, the operands are on unequal footing: The left operand (a variable) is to be made equal to the right operand (an expression). x = y does not mean the same thing as y = x.[14]
    —Niklaus Wirth, Good Ideas, Through the Looking Glass

Beginning programmers sometimes confuse assignment with the relational operator for equality, as "=" means equality in mathematics, and is used for assignment in many languages. But assignment alters the value of a variable, while equality testing tests whether two expressions have the same value.

In some languages, such as BASIC, a single equals sign ("=") is used for both the assignment operator and the equality relational operator, with context determining which is meant. Other languages use different symbols for the two operators. For example:

    In Pascal, the assignment operator is a colon and an equals sign (":=") while the equality operator is a single equals ("=").
    In C, the assignment operator is a single equals sign ("=") while the equality operator is a pair of equals signs ("==").

The similarity in the two symbols can lead to errors if the programmer forgets which form ("=", "==", ":=") is appropriate, or mistypes "=" when "==" was intended. This is a common programming problem with languages such as C, where the assignment operator also returns the value assigned, and can be validly nested inside expressions (in the same way that a function returns a value). If the intention was to compare two values in an if statement, for instance, an assignment is quite likely to return a value interpretable as Boolean true, in which case the then clause will be executed, leading the program to behave unexpectedly. Some language processors (such as gcc) can detect such situations, and warn the programmer of the potential error.