from Wikipedia, the free encyclopedia

Under an assignment (English assignment ) is understood in an imperative programming language a type of instruction (English statement ) by which a variable gets a new value. To distinguish it from other instruction types that can also change the value of a variable (for example, calling a procedure with side effects), we only speak of an assignment if the assignment operator of the respective programming language is used. Many programming languages ​​take into account the assignment compatibility of the expressions and variables involved in order to avoid program errors.

Before a value can be assigned to a variable, it is necessary in many programming languages to declare this explicitly . In some programming languages ​​this is done implicitly when an assignment is made to a variable that has not yet been declared (for example in Perl , but not in C ++ ).


Below is va variable and aan expression (English expression ). The examples show some notations in different programming languages.

v = a
Java , C , C ++ , C # , Python , Visual Basic .NET , PHP , JavaScript , Perl , Objective-C , Swift , Go , Ruby
v := a
Ada , ALGOL , Eiffel , Modula-2 , Oberon , Pascal , Seed7 , Smalltalk
set v a
MAKE "v :a

Syntactic peculiarities

The assignment can easily be confused with the mathematical equal sign in cases where it is only symbolized with a simple equal sign . The assignment to a variable with the equal sign (for example x = y ), which has often been found in programming languages since the invention of FORTRAN, can therefore easily lead to confusion or confusion: the two assignments x = y (assignment of the value of the variable y to the variable 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 corresponding Boolean expressions with relational operators (comparison for equality of x and y ), which in both cases lead to an identical Boolean result to lead.

In some programming languages, the problem is that the assignment can be integrated into other instructions if it can itself be interpreted as a result value. The following two examples show two variants of a corresponding program sequence in the C programming language, both of which can lead to programming errors that are very easy to overlook :

int i = 0; 
if (i = 1) { 
    // Dieser Block wird immer ausgeführt, 
    // weil die Zuweisung i = 1 immer das numerische Ergebnis 1 hat,
    // was als der boolesche Wert "wahr" interpretiert wird.
int i = 0;
if (i == 1) { 
    // Dieser Block wird nie ausgeführt, 
    // weil die Vergleichsoperation i == 1 immer das numerische Ergebnis 0 hat, 
    // was als der boolesche Wert "falsch" interpretiert wird. 

Multiple assignments

In some programming languages ​​it is also possible to formulate several assignments within one statement. For example, several variable names can be noted on the left side of the assignment operator and several expressions on the right side separated by commas.

v1,v2 = a1,a2      Ruby
($v1,$v2) = ($a1,$a2) Perl
[v1, v2] = [a1, a2] JavaScript linksseitige Destrukturierung des rechtsseitigen Arrays
$v1,$v2 = $a1,$a2  Windows PowerShell

In programming languages ​​that support multiple assignments, a construct of the form

v1,v2 := v2,v1

exchange the values ​​of the two variables v1and v2. If a language does not allow multiple assignments, you generally need an auxiliary variable to exchange the contents of two variables; one then speaks of a triangle swap .

Individual evidence

  1. Nikolaus Wirth: Good Ideas, Through the Looking Glass , Section 4.1 Notation and Syntax (PDF; 207 kB), Zurich (2005)