Assignment (computer science)

From Academic Kids

(Redirected from Assignment operation)

In most imperative computer programming languages, the assignment operation is one of the basic operations. It sets or re-sets the value assigned to a variable. This means that the same variable name will possibly stand for different values at different times; the variables are not handled in the same way as the unknowns x, y, z ... of algebra, which stand always for the same value.

It can take on the form, for example,

variable := expression

or often an equals sign is used:

variable = expression

The semantics of this operation is that the current state of the executing program is modified:

  • 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.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 3rd 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 lvalue).

A common error regarding the assignment operation is when programmers confuse it with the equivalence expression. In most languages, the assignment operator is a single equal sign (=) while the equivalence operator is a pair of equal signs (==). Often, the programmer neglects to use two equal signs when he or she wishes to make a comparison, especially when he is proficient in using languages (e.g., BASIC) where the two operators are the same: a single equal sign.

A consequence of this error is that the assignment is made, and the operation then returns the value assigned, which usually is a TRUE value. If the supposed equivalence expression were used as the condition in an if statement, what is often executed is the then clause, which is a program logic error. Most compilers and interpreters are able to flag these types of errors, warning the programmer that a possible assignment operation was unintended in an if statement.

Languages such as Perl do not require variables to be declared before an assignment is made.

Personal tools
Navigation

    Information

    • Home Page (http://academickids.com/encyclopedia/index.php)
    • New Articles (http://www.academickids.com/encyclopedia/index.php/Special:Newpages)
    • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)


    Academic Kids Menu

    • Art and Cultures (http://www.academickids.com/encyclopedia/index.php/Art_and_Cultures)
      • Art (http://www.academickids.com/encyclopedia/index.php/Art)
      • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
      • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
      • Music (http://www.academickids.com/encyclopedia/index.php/Music)
      • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
    • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
    • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
    • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
      • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
      • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
      • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
      • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
    • History (http://www.academickids.com/encyclopedia/index.php/History)
      • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
      • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
      • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
      • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
      • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
      • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
      • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
      • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
      • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
    • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
    • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
    • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
    • Science (http://www.academickids.com/encyclopedia/index.php/Science)
      • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
      • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
      • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
      • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
      • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
      • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
      • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
      • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
    • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
      • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
      • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
      • Government (http://www.academickids.com/encyclopedia/index.php/Government)
      • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
      • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
    • Space and Astronomy (http://www.academickids.com/encyclopedia/index.php/Space_and_Astronomy)
      • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
      • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
    • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
    • US States (http://www.academickids.com/encyclopedia/index.php/US_States)
          Advertisement