FACTOID # 121: Houses in English-speaking countries have the most rooms.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Assignment operator

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.




  Results from FactBites:
 
Chapter 4 (1579 words)
The division operator, when one integer is divided by another, results in the whole number of times the second number divides into the first (the result when the quotient is a whole number and a remainder).
Mathematical operators used in combination with the assignment operator, "=", can be used to reassign a value to a variable by performing a mathematical operation on it, and replacing the original value with the result (e.g., num += 6 is the same as num = num + 6).
When you have a number of operators in the same statement, the order in which the operations are processed is dictated by the rules of precedence, such as division and multiplication being performed before addition and subtraction.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m