Assignment (computer Science) - Semantics

Semantics

An assignment operation is a process in imperative programming in which different values are associated with a particular variable name as time passes. The program, in such model, operates by changing its state using successive assignment statements. Primitives of imperative programming languages rely on assignment to do iteration. At the lowest level, assignment is implemented using machine operations such as MOVE or STORE.

The variables are containers for values. It is possible to put a value into variable and later replace it with a new one. An assignment operation modifies the current state of the executing program. Consequently, assignment is dependent on the concept of variables. 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. The case that the assigned value depends on previous one is so common that many imperative languages, most notably C and the majority of its descendants, augment the notion of assignment by defining special binary operators like *= for convenience so that the example becomes a *= 2.

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 third 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, typically dynamic ones, it is not necessary to declare a variable prior to assigning it a value.

Read more about this topic:  Assignment (computer Science)