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.
Some expression-oriented languages uniformly use functional syntax for all statements, including assignment: (setq variable expression) LISP set variable expression Tcl
expression is evaluated in the current state of the program.
variable is assigned the computed value, replacing the prior value of that variable.
Example: Assuming that 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.
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.
a,b := 0,1
Simultaneously assigns 0 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
as 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.
Přiřazení | Zuweisung | Affectation | Присваивание (программирование)
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Assignment (computer science)".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world