Assignment (computer science)

Under an assignment ( english assignment ) refers, in an imperative programming language a type of instruction (English statement) by which a variable gets a new value. In contrast of other types of statements, which can also change the value of a variable (for example, calling a procedure with side effects ), one speaks only of an assignment, if the assignment operator of the programming language is used. Many programming languages ​​considered here the assignment compatibility of the involved expressions and variables to avoid bugs.

Before an assignment of a value can occur at a variable, it is required in many programming languages, this explicitly declaring. In some programming languages ​​, this is done implicitly when assigning to a not yet declared variable (for example, in Perl, but not in C ).

Notation

In the following, let v be a variable and a is an expression (english expression). The examples show some notations in different programming languages ​​.

Syntactic peculiarities

The assignment can be easily confused with the mathematical equal sign in the cases where it is symbolized only with a simple equal sign. The often found in programming languages ​​since the invention of FORTRAN assignment to a variable with an equal sign (for example, x = y ) can therefore easily lead to irritation or confusion: the two assignments x = y ( assigning the value of the variable y on the variables x ) and y = x ( assignment of the value of the variable x to the variable y ) each have a completely different meaning than the two equivalent Boolean expressions using relational operators ( equality comparison of x and y), which in both cases to an identical Boolean result lead.

The problem is aggravated in some programming languages ​​that allocation can be integrated into other instructions if it can be interpreted by you as a result value. The following two examples show two variations of a corresponding program sequence in the C programming language, which can lead both very easily overlooked programming errors:

Int i = 0; if ( i = 1) {     This block is always executed,     because the assignment i = 1 is always the numerical result one has     which is interpreted as a Boolean value of "true". } int i = 0; if ( i == 1) {     This block is never executed,     because the comparison operation i == 1 is always the numerical result 0,     which is interpreted as a Boolean value of "false". } Multiple assignments

In some programming languages ​​, it is also possible to formulate multiple assignments in a single statement. So may be listed separated by commas around on the left side of the assignment operator several variable names and on the right side of several expressions.

V1, v2 = a1, a2 Ruby ($ v1, $ v2) = ($ a1, $ a2) Perl $ v1, $ v2 = $ a1, $ a2 Windows PowerShell In programming languages ​​that support multiple assignments can be personalized with a construct of the form

V1, V2 = v2, v1 Replacing the values ​​of the two variables V1 and V2. Allows a language no multiple assignments, then you need in the general case an auxiliary variable to swap the contents of two variables; this is called a triangle exchange.

561952
de