HOME

TheInfoList



OR:

Augmented assignment (or compound assignment) is the name given to certain assignment operators in certain
programming languages A programming language is a system of notation for writing computer program, computer programs. Most programming languages are text-based formal languages, but they may also be visual programming language, graphical. They are a kind of computer ...
(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. A simple example is x += 1 which is expanded to x = x + 1. Similar constructions are often available for various binary operators. 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 In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operat ...
.


Discussion

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 , add 1 to it, and store the result of the addition in the variable ." As simple as this seems, it may have an inefficiency, in that the location of variable has to be looked up twice if the
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs th ...
does not recognize that two parts of the expression are identical: 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 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 In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a ve ...
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 an optimizing 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 is a complicated entity the compiler will be encouraged to avoid duplication in accessing , and of course, if is a lengthy name, there will be less typing required. This last was the basis of the similar feature in the
ALGOL ALGOL (; short for "Algorithmic Language") is a family of imperative computer programming languages originally developed in 1958. ALGOL heavily influenced many other languages and was the standard method for algorithm description used by the ...
compilers offered via the Burroughs B6700 systems, using the tilde 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.


Semantics

In expression-oriented programming languages such as C, assignment and augmented assignment are expressions, which have a value. This allows their use in complex expressions. However, this can produce 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. In other languages, such as Python, assignment and augmented assignment are statements, not expressions, and thus cannot be used in complex expressions. For example, the following is valid C, but not valid Python: a += b += c As with assignment, in these languages augmented assignment is a form of right-associative assignment. Unlike in C, the compound assignment expressions of C++ evaluate to an lvalue. Being an lvalue allows it to be written on the left-hand-side of some other assignment statement: int x = 11; (x *= 2) += 3; // Sets x to 25


Computed assignment locations

In languages such as C, C++ and Python, an augmented assignment where the assignment location includes function calls, is mandated to only call the functions once. I.e in the statement: my_array 1()+= 1 The function f1 is ''mandated'' to only be called once. If a language implements augmented assignment by macro expansion to: my_array 1()= my_array 1()+ 1 Then f1 is called twice.


By language


C descendants

In C,
C++ C, or c, is the third letter in the Latin alphabet, used in the modern English alphabet, the alphabets of other western European languages and others worldwide. Its name in English is ''cee'' (pronounced ), plural ''cees''. History "C" ...
, and C#, the assignment operator is =, which is augmented as follows: Each of these is called a ''compound assignment'' operator in said languages.


Supporting languages

The following list, though not complete or all-inclusive, lists some of the major programming languages that support augmented assignment operators. * APL *
AWK AWK (''awk'') is a domain-specific language designed for text processing and typically used as a data extraction and reporting tool. Like sed and grep, it is a filter, and is a standard feature of most Unix-like operating systems. The AWK lan ...
* C *
C++ C, or c, is the third letter in the Latin alphabet, used in the modern English alphabet, the alphabets of other western European languages and others worldwide. Its name in English is ''cee'' (pronounced ), plural ''cees''. History "C" ...
* C# *
CFML ColdFusion Markup Language, more commonly known as CFML, is a scripting language for web development that runs on the JVM, the .NET framework, and Google App Engine. Multiple commercial and open source implementations of CFML engines are avail ...
* D * DWScript *
Free Pascal Free Pascal Compiler (FPC) is a compiler for the closely related programming-language dialects Pascal and Object Pascal. It is free software released under the GNU General Public License, witexception clausesthat allow static linking against it ...
(Needs -Sc command line switch) * Go *
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's most ...
*
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, often ...
* Kotlin *
Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTS ...
*
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
*
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
*
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pr ...
*
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sap ...
*
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
* Scala *
SystemVerilog SystemVerilog, standardized as IEEE 1800, is a hardware description and hardware verification language used to model, design, simulate, test and implement electronic systems. SystemVerilog is based on Verilog and some extensions, and since 20 ...
*
Swift Swift or SWIFT most commonly refers to: * SWIFT, an international organization facilitating transactions between banks ** SWIFT code * Swift (programming language) * Swift (bird), a family of birds It may also refer to: Organizations * SWIFT ...
*
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic (cl ...


See also

*
Increment and decrement operators Increment and decrement operators are unary operators that ''add'' or ''subtract'' one, to or from their operand, respectively. They are commonly implemented in imperative programming languages. C-like languages feature two versions (pre- a ...
—special case of augmented assignment, by 1 * IEEE 754 augmented arithmetic operation


References

{{DEFAULTSORT:Augmented Assignment Operators (programming) Computer arithmetic Assignment operations