Augmented assignment

Augmented assignment

Augmented assignment (or compound assignment) is the name given to certain operators in certain programming languages (especially those derived from C). An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable.

For example, the following statement or some variation of it can be found in many programs:

x = x + 1

This means "find the number stored in the variable x, add 1 to it, and store the result of the addition in the variable x." As simple as this seems, it may have an inefficiency, in that the location of variable x has to be looked up twice if the compiler does not recognize that two parts of the expression are identical: x might be a reference to some array element or other complexity. In comparison, here is the augmented assignment version:

x += 1

With this version, there is no excuse for a compiler failing to generate code that looks up the location of variable x just once, and modifies it in place, if of course the machine code supports such a sequence. For instance, if x is a simple variable, the machine code sequence might be something like

 Load  x
 Add   1
 Store x

and the same code would be generated for both forms. But if there is a special op code, it might be

 MDM   x,1

meaning "Modify Memory" by adding 1 to x, and a decent compiler would generate the same code for both forms. Some machine codes offer INC and DEC operations (to add or subtract one), others might allow constants other than one.

More generally, the form is

x ?= expression

where the ? stands for some operator (not always +), and there may be no special op codes to help. There is still the possibility that if x is a complicated entity the compiler will be encouraged to avoid duplication in accessing x, and of course, if x is a lengthy name, there will be less typing required. This last was the basis of the similar feature in the Algol compilers offered via the Burroughs B6700 systems, using the tilda symbol to stand for the variable being assigned to, so that

LongName:=x + sqrt(LongName)*7;

would become

LongName:=x + sqrt(~)*7;

and so forth. This is more general than just x:=~ + 1; Producing optimum code would remain the province of the compiler.

In general, in languages offering this feature, most operators that can take a variable as one of their arguments and return a result of the same type have an augmented assignment equivalent that assigns the result back to the variable in place, including arithmetic operators, bitshift operators, and bitwise operators.

On the other hand, enthusiastic use of these features (especially with sub-expressions within larger expressions) soon produces sequences of symbols that are difficult to read or understand, and worse, a mistype can easily produce a different sequence of gibberish that although accepted by the compiler does not produce desired results.

The C++ assignment operator is = which is augmented as follows

Operator Description
+= Addition
-= Subtraction
*= Multiplication
/= Division
%= Modulus
<<= Left bit shift
>>= Right shift
&= Bitwise AND
^= Bitwise exclusive OR
|= Bitwise inclusive OR

Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • C (programming language) — C The C Programming Language[1] (aka K R ) is the seminal book on C …   Wikipedia

  • GNU Octave — screenshot Developer(s) …   Wikipedia

  • Increment and decrement operators — are unary operators that add or subtract one from their operand, respectively. They are commonly implemented in imperative programming languages. C like languages became notorious for featuring two versions (pre and post ) of each operator with… …   Wikipedia

  • Linear programming — (LP, or linear optimization) is a mathematical method for determining a way to achieve the best outcome (such as maximum profit or lowest cost) in a given mathematical model for some list of requirements represented as linear relationships.… …   Wikipedia

  • Guided Local Search — is a metaheuristic search method. A meta heuristic method is a method that sits on top of a local search algorithm to change its behaviour. Guided Local Search builds up penalties during a search. It uses penalties to help local search algorithms …   Wikipedia

  • education — /ej oo kay sheuhn/, n. 1. the act or process of imparting or acquiring general knowledge, developing the powers of reasoning and judgment, and generally of preparing oneself or others intellectually for mature life. 2. the act or process of… …   Universalium

  • Extended Backus–Naur Form — In computer science, Extended Backus–Naur Form (EBNF) is a metasyntax notation used to express context free grammars: that is, a formal way to describe computer programming languages and other formal languages. It is an extension of the basic… …   Wikipedia

  • Scale-invariant feature transform — Feature detection Output of a typical corner detection algorithm …   Wikipedia

  • List of mathematics articles (A) — NOTOC A A Beautiful Mind A Beautiful Mind (book) A Beautiful Mind (film) A Brief History of Time (film) A Course of Pure Mathematics A curious identity involving binomial coefficients A derivation of the discrete Fourier transform A equivalence A …   Wikipedia

  • Hungarian algorithm — The Hungarian method is a combinatorial optimization algorithm which solves the assignment problem in polynomial time and which anticipated later primal dual methods. It was developed and published by Harold Kuhn in 1955, who gave the name… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”