Forgot your password?  

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

Compound Assignment | Research & Encyclopedia Articles

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

 


Compound Assignment

The compound assignment is a form of assignment statement used to perform multiple operations on variables and expressions. An assignment statement assigns a value to a variable, in the form of variable=expression; the expression can be a constant, another variable, or an arithmetic calculation. With compound assignments the equals sign (=), also called the operator, is interpreted by the computer as "takes the value of" in executing the assignment statement, and compound assignment statements are thereby able to perform more than one operation on a variable.

For example, a multiplication assignment operator (*=) is a compound assignment operator used to direct the multiplication of the value of a variable by the value of an expression. The result of this operation is then assigned to the variable (i.e., becomes the new result). The operational sequence (result*=expression) involves any variable or expression and is identical to specifying that the result equals the result times (multiplied by) the expression (result=result*expression).

An addition assignment operator (+=) usually adds the value of a variable and an expression and then assigns that sum to the variable to form a new result(result += expression). The use of this compound assignment operator is identical to directing that the result plus the expression equals a new result (result =result+expression). The numeric variable expression four (4) would be added to three (3) to form a new result seven (7). There are, however, variations in the type of expression that can alter the meaning of the assignment operator. When the result and the expression are both numbers the expression is added to the result. If however, both the result and the expression are string variables (text) then the operation fuses (concatenates) the strings to form a new result (e.g., the variable expression "Smith" would be added to "Name:" to form a new result "Name: Smith"). If the result and the expression are different data types (e.g. one is a string and the other a number), then the addition assignment operator concatenates the result and the expression to form a new result.

A compound assignment statement differs from the assignment statement in that the operator can have two value commands. The operators of a compound assignment consist of a binary (numerical) operator and an assignment operator. The operators perform the operation specified by the binary operator on both the operands--the objects on both sides of the operator--and give the result of the operation to the left operand. If, a=6, b=2, and c=4, then a*=b+c is equivalent to a=a*(b+c) (a result of 36) or 6*(4+2). However, a*=b+c is not equivalent to a=a*b+c. (6 = 6 *4+2 which should by the rules of algebra operations, of course, equal 26).

The following are compound assignment operators and their expressions. These operators work with bytes or groups of bytes.

  • += Addition--adds the value of an expression to the value of a variable and assigns the result to the variable.
  • -= Subtraction--subtracts the value of an expression from the value of the variable and assigns the result to the variable.
  • *= Multiplication--multiplies the value of an expression by the value of the variable and assigns the result to the variable.
  • /= Division--divides the value of an expression by the value of the variable and assigns the result to the variable.
  • %= Modulus--divides the value of the variable by the value of the expression and assigns the result to the variable.
  • <<= Left shift--left shifts the value of the variable by the number of bits specified in the value and assigns the result to the variable.
  • >>= Right shift--right shifts the value of the variable by the number of bits specified in the value and assigns the result to the variable.

Three other compound assignment operators are known as bitwise operators. As their name indicates, these manipulate individual bits of information within a byte. Bitwise operators are not compatible with all programming languages. Bitwise operators are as follows:

  • &= Bitwise AND--performs an AND comparison on two groups of bits.
  • = Bitwise XOR--performs an XOR comparison on two groups of bits.
  • |= Bitwise OR--performs an OR comparison on two groups of bits.

Compound assignment statements shorten what would otherwise be a longer, and perhaps more cumbersome assignment statement. For example, the statement a=a+4 can be shortened to a+=4 by the use of the compound operator +=. The use of a compound statement should be a considered decision, however, because it may have the undesirable effect of making the code more obscure and less readable.

This is the complete article, containing 730 words (approx. 2 pages at 300 words per page).

More Information
  • View Compound Assignment Study Pack
  • 8 Alternative Definitions
  • Search Results for "Compound Assignment"
  • 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

    Assignment Statement
    The assignment statement is the simplest expression in computer programming. Most programming langu... 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
    Compound Assignment 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