Forgot your password?  

Not What You Meant?  There are 8 definitions for Left.  Also try: Assignment.

Assignment Statement | Research & Encyclopedia Articles

Print-Friendly   Order the PDF version   Order the RTF version
About 2 pages (457 words)
Assignment (computer science) Summary

 


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:

  • result = expression

A very simple example of a real assignment statement is:

  • int i = 3;

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

  • float f = 3.2;
  • int i = f

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

More Information
  • View Assignment Statement Study Pack
  • 8 Alternative Definitions
  • Search Results for "Assignment Statement"
  • More Products on This Subject
    Assignment Operator
    The assignment operator in a computer language takes the value of an expression and copies it to a ... more

    Compound Assignment
    The compound assignment is a form of assignment statement used to perform multiple operations on va... more


    Ask any question on Assignment (computer science) and get it answered FAST!
    Answer questions in BookRags Q&A and earn points toward
    discounted or even FREE Study Guides and other BookRags products!
    Learn more about BookRags Q&A
    Copyrights
    Assignment Statement from World of Computer Science. ©2005-2006 Thomson Gale, a part of the Thomson Corporation. All rights reserved.

    Join BookRagslearn moreJoin BookRags

    Join BookRagslearn moreJoin BookRags