Assignment Statement
The assignment statement is the simplest expression in computer programming. Most programming languages use the conventional mathematical symbol = (the "equals" sign) to indicate assignment, but there are exceptions such as Pascal, which uses := (a colon and an "equals" sign).
In an assignment statement an expression is evaluated and the result stored in a variable in the program. The general form of the assignment statement is:
A very simple example of a real assignment statement is:
Although the number 3 is a constant, it is also an expression that evaluates to the number 3. At the end of the statement, the variable "i" contains this number. It is important to note that the data type that the expression evaluates to must be the same type as the result. Most languages have implicit conversion rules built into them to cope with converting between the primitive data types like int and float (integers and floating-point numbers, respectively), but sometimes this implicit conversion can mean errors creeping into the calculation.
For example, in C++ the statement
will mean the variable "i" takes on the value 3, because the float is implicitly converted to an int before the assignment statement can be executed.
In C++ it is possible to overload the assignment operator so it has different semantics depending on the context. For example it would be possible to create a class to represent a Mongoose and then overload the assignment operator to allow one Mongoose to "become" another, like this:
- Mongoose myMongoose("Fred");
- Mongoose yourMongoose(); // has no name
- yourMongoose = myMongoose; // we now share a mongoose
In the code above, myMongoose and yourMongoose are unique mongooses that happen to be identical. If one changes or dies, the other remains untouched.
In C and C++ the use of pointers can make the incautious use of the assignment operator a dangerous thing. For example in the following code the result is not perhaps what the programmer expected:
- Mongoose myMongoose("Fred");
- Mongoose* myMongoosePtr = &myMongoose;
- Mongoose* yourMongoosePtr;
- yourMongoosePtr = myMongoosePtr; // we now share a mongoose
What has happened here is that yourMongoosePtr and myMongoosePtr both point to the same mongoose; all the code has done is copy the value of the pointer and not the data in the Mongoose object. There is still only one Mongoose.
Java, C, Pascal, Perl, and most other languages do not allow operator overloading so it is not possible to do anything like the code above. Instead the programmer has to write functions that encapsulate "assignment" semantics. For example, in C it might look like this:
- void assignMongoose(Mongoose* a, Mongoose* b) {
- /* code to copy a to b */
- }
and be called like this:
- Mongoose myMongoose;
- Mongoose yourMongoose;
- assignMongoose(&myMongoose, &yourMongoose);
The above code assumes that Mongoose has been defined elsewhere as a C structure type.
This is the complete article, containing 457 words
(approx. 2 pages at 300 words per page).