Assignment (computer science)
Encyclopedia : A : AS : ASS : Assignment (computer science)
In most imperative computer programming languages, the assignment statement is one of the basic statements. It sets or re-sets the value assigned to a variable. This means that the same variable name will possibly stand for different values at different times; the variables are not handled in the same way as the unknowns x, y, z ... of algebra, which stand always for the same value.
Notation
Common representations of the assignment operator include an equals sign ("="), ":=", or a left-arrow; this last is less common in modern languages as this character is not available on most standard keyboard layouts. e.g.variable = expression Fortran, C, ... variable := expression Algol, Pascal, Ada, ... variable <- expression S, R variable ← expression APL MOVE expression TO variable COBOLSome expression-oriented languages uniformly use functional syntax for all statements, including assignment:
(setq variable expression) LISP set variable expression Tcl
Operation
Semantically, an assignment operation modifies the current state of the executing program:
- The
expressionis evaluated in the current state of the program. - The
variableis assigned the computed value, replacing the prior value of that variable.
a is a numeric variable, the assignment a := 2*a means that the content of the variable a is doubled after the execution of the statement.An example segment of C code:
int x=10; float y; x=23; y=32.4;In this sample, the variable x is first declared as an int, and is then assigned the value of 10. Notice that the declaration and assignment occur in the same statement. In the second line, y is declared without an assignment. In the 3rd line, x is reassigned the value of 23. Finally, y is assigned the value of 32.4.
For an assignment operation it is necessary that the value of the expression is well-defined (it is a valid rvalue) and that the variable represents a modifiable entity (it is a valid modifiable (non-const) lvalue). In some languages, such as Perl, it is not necessary to declare a variable prior to assigning it a value.
Assignment versus equality
A common error regarding the assignment operation is when programmers confuse it with the equivalence expression, readily associated with the mathematical use of "=" to mean equality. Whereas assignment is an operation which alters the value of a variable, equivalence is an expression which tests whether two variables or expressions evaluate to the same value.In many languages, the assignment operator is a single equals sign ("=") while the equivalence operator is a pair of equals signs ("=="), but this is far from universal; indeed some languages, such as BASIC, use a single equals sign for both, determining which is meant based on context.
This can lead to errors if the programmer forgets which form ("=", "==", ":=") is appropriate, particularly in 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 interprettable as TRUE, in which case the then clause will be executed, leading the program to behave unexpectedly. While not necessarily a syntax error, most compilers and interpretters are able to detect such situations, and warn the programmer that an assignment operation may have been used where a comparison was intended.
Parallel assignment
Some programming languages, such as Python and Ruby, allow several variables to be assigned in parallel. In pseudocode:
a,b := 0,1Simultaneously assigns 0 to
a and 1 to b. More interestingly,
a,b := b,aSwaps 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 := tas the simpler construct
a:=b ; b:=a would first assign the value of b to a, and then assign this same value back to b, thus ending with a and b both having the original value of b.See also
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
