ENCYCLOPEDIA 4U .com



Encyclopedia Home Page

Google
  Web Encyclopedia4u.com

 

Assignment operation

The assignment operation is one of the basic operations in most imperative computer programming languages. It can take on the form, for example,

variable := expression or 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.

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 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.





Content on this web site is provided for informational purposes only. We accept no responsibility for any loss, injury or inconvenience sustained by any person resulting from information published on this site. We encourage you to verify any critical information with the relevant authorities.



Copyright © 2005 Par Web Solutions All Rights reserved.
| Privacy

This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Assignment operation".