Assignment (computer science)
From Wikipedia, the free encyclopedia
Categories: Articles lacking sources from May 2007 | All articles lacking sources | Programming constructs
In computer science the assignment statement sets or re-sets the value stored in the storage location(s) denoted by a variable name. In most imperative computer programming languages the assignment statement is one of the basic statements. The assignment statement often allows that the same variable name to contain different values at different times during program execution.
NotationCommon textual representations of the assignment include an equals sign (“=”) and “:=”. These two forms are typical of programming languages, such as C), that classify assignment as an infix operator.
Other possibilities include a left arrow or a keyword.
Some expression-oriented languages, such as Lisp and Tcl, uniformly use functional syntax for all statements, including assignment. OperationSemantically, an assignment operation modifies the current state of the executing program. Consequently, assignment is dependent on the concept of variables. In an assignment:
Example: Assuming that An example segment of C code: <source lang="c"> int x = 10; float y; x = 23; y = 32.4; </source> In this sample, the variable For an assignment operation, it is necessary that the value of the Parallel assignmentSome programming languages, such as Python, Perl, Ruby, Windows PowerShell, OCaml and JavaScript (since 1.7), allow several variables to be assigned in parallel. In pseudocode: a,b := 0,1 Simultaneously assigns 0 to var list := 0,1 a,b := list The list will be unpacked so that 0 is assigned to a,b := b,a Swaps the values of var t := a a := b b := t since Value of an assignmentIn most expression-oriented programming languages, the assignment statement returns the assigned value, allowing such idioms as In other programming languages, the return value of an assignment is undefined and such idioms are invalid. Examples are Scheme and Haskell. Assignment versus single assignmentIn functional programming, assignment is discouraged in favor of single assignment, also called name binding. Single assignment differs from assignment as described in this article in that it can only be made once; no subsequent re-assignment is allowed. Once created by single assignment, named values are not variables but immutable objects. Single assignment is the only form of assignment available in purely functional languages, such as Haskell, which do not have variables in the sense of imperative programming languages. Impure functional languages provide both single assignment as well as true assignment (though true assignment is used with less frequency than in imperative programming languages). For example, Objective Caml provides single assignment by a Assignment versus equalityBeginning programmers sometimes confuse assignment with the relational operator for equality, as "=" means equality in mathematics, and is used for assignment in many languages. But assignment alters the value of a variable, while equality testing tests whether two expressions have the same value. In many languages, the assignment operator is a single equals sign ("=") while the equivalence operator is a pair of equals signs ("=="); in some languages, such as BASIC, a single equals sign is used for both, with context determining which is meant. This can lead to errors if the programmer forgets which form (=, ==, :=) is appropriate (or mistypes = when == was intended). This is a common programming problem with 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 See alsode:Zuweisung hr:Dodjela (računarstvo) pl:Przypisanie ru:Присваивание |


