In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variablename; in other words, it copies a value into the variable. In most imperativeprogramming languages, the assignment statement (or expression) is a fundamental construct.
Today, the most commonly used notation for this basic operation has come to be x = expr
(originally Superplan1949–51, popularized by Fortran 1957 and C) followed by x := expr
(originally ALGOL 1958, popularised by Pascal).
In an assignment:
- The
expression
is evaluated in the current state of the program. - The
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.4f;
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 third line, x
is reassigned the value of 23. Finally, y
is assigned the value of 32.4.
Value of an assignment
In some programming languages, an assignment statement returns a value, while in others it does not.
In most expression-oriented programming languages (for example, C), the assignment statement returns the assigned value, allowing such idioms as x = y = a
, in which the assignment statement y = a
returns the value of a
, which is then assigned to x
.