Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Augmented assignment
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Refimprove|date=September 2014}} '''Augmented assignment''' (or '''compound assignment''') is the name given to certain [[Assignment (computer science)|assignment]] [[operator (programming)|operator]]s in certain [[programming languages]] (especially those derived from [[C (programming language)|C]]). An augmented assignment is generally used to replace a statement where an operator takes a [[Variable (programming)|variable]] as one of its arguments and then assigns the result back to the same variable. A simple example is <code>x += 1</code> which is expanded to <code>x = x + 1</code>. 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 operation|bitwise operators]]. ==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 {{mono|x}}, add 1 to it, and store the result of the addition in the variable {{mono|x}}." As simple as this seems, it may have an inefficiency, in that the location of variable {{mono|x}} has to be looked up twice if the [[compiler]] does not recognize that two parts of the expression are identical: {{mono|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 {{mono|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 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 {{mono|?}} stands for some operator (not always {{mono|+}}), and there may be no special op codes to help. There is still the possibility that if {{mono|x}} is a complicated entity the compiler will be encouraged to avoid duplication in accessing {{mono|x}}, and of course, if {{mono|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 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 <code>x:=~ + 1;</code> Producing optimum code would remain the province of the compiler. ==Semantics== In [[expression-oriented programming language]]s 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: <syntaxhighlight lang=c> a += b += c </syntaxhighlight> As with assignment, in these languages augmented assignment is a form of [[Operator associativity#Right-associativity of assignment operators|right-associative assignment]]. Unlike in C, the compound assignment expressions of C++ evaluate to an [[Value_(computer_science)#lrvalue|lvalue]].<ref name="cppreference" /> Being an lvalue allows it to be written on the left-hand-side of some other assignment statement:<ref name="stroustrup" /> <syntaxhighlight lang=c> int x = 11; (x *= 2) += 3; // Sets x to 25 </syntaxhighlight> ===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: <syntaxhighlight lang=c> my_array[f1()] += 1 </syntaxhighlight> The function <code>f1</code> is ''mandated'' to only be called once. If a language implements augmented assignment by macro expansion to: <syntaxhighlight lang=c> my_array[f1()] = my_array[f1()] + 1 </syntaxhighlight> Then <code>f1</code> is called twice. ==By language== ===C descendants=== In [[C (programming language)|C]], [[C++]], and [[C_Sharp_(programming_language)|C#]], the assignment operator is <code>=</code>, which is augmented as follows: {| class="wikitable" |- ! Operator ! Description |- ! <code>+=</code> | Addition |- ! <code>-=</code> | Subtraction |- ! <code>*=</code> | Multiplication |- ! <code>/=</code> | Division |- ! <code>%=</code> | Modulus |- ! <code><<=</code> | [[Bitwise operations in C#Left shift <<|Left bit shift]] |- ! <code>>>=</code> | [[Bitwise operations in C#Right shift >>|Right bit shift]] |- ! <code>&=</code> | [[Bitwise operations in C#Bitwise AND_&|Bitwise AND]] |- ! <code>^=</code> | [[Bitwise operations in C#Bitwise XOR ^|Bitwise exclusive OR]] |- ! <code>{{pipe}}=</code> | [[Bitwise operations in C#Bitwise OR {{pipe}}|Bitwise inclusive OR]] |} Each of these is called a ''compound assignment'' operator in said languages.<ref name="cppreference" /><ref>{{cite web|title=ISO/IEC 9899:201x Committee Draft April 12, 2011 N1570|url=http://www.iso-9899.info/n1570.html#6.5.16.2}}</ref><ref>{{cite web|title=Assignment and compound assignment operators|url=http://eel.is/c++draft/expr.ass}}</ref><ref>{{cite web|title=C# Language Specification|url=http://msdn.microsoft.com/en-us/library/ms228593.aspx|publisher=Microsoft|accessdate=17 March 2014}}</ref> ==Supporting languages== The following list, though not complete or all-inclusive, lists some of the major programming languages that support augmented assignment operators. {{col-begin}} {{col-break|width=25%}} * [[APL (programming language)|APL]] * [[AWK]] * [[C (programming language)|C]] * [[C++]] * [[C Sharp (programming language)|C#]] * [[ColdFusion Markup Language|CFML]] * [[D (programming language)|D]] * [[Free Pascal]] (Needs -Sc command line switch) {{col-break}} * [[Go (programming language)|Go]] * [[Java (programming language)|Java]] * [[JavaScript]] * [[Julia (programming language)|Julia]] * [[Kotlin (programming language)|Kotlin]] * [[Objective-C]] * [[Perl]] * [[PHP]] {{col-break}} * [[Python (computer language)|Python]] * [[Ruby (programming language)|Ruby]] * [[Rust (programming language)|Rust]] * [[Scala (programming language)|Scala]] * [[SystemVerilog]] * [[Swift (programming language)|Swift]] * [[Visual Basic]] {{col-end}} ==See also== * [[Compound operator]] * [[Fused operation]] * [[Increment and decrement operators]]—special case of augmented assignment, by 1 * [[IEEE 754 augmented arithmetic operation]] ==References== <references> <ref name="cppreference">{{cite web | url=https://en.cppreference.com/w/cpp/language/operator_assignment | publisher=C++ Reference | website=cppreference.com | title=Assignment Operators | accessdate=1 March 2021}}</ref> <ref name="stroustrup">{{cite book | last=Stroustrup | first=Bjarne | author-link=Bjarne Stroustrup | title=[[The C++ Programming Language]] | publisher=Addison-Wesley | year=2013 | edition=Fourth | isbn=978-0-321-56384-2}}</ref> </references> {{DEFAULTSORT:Augmented Assignment}} [[Category:Operators (programming)]] [[Category:Computer arithmetic]] [[Category:Assignment operations]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Col-begin
(
edit
)
Template:Col-break
(
edit
)
Template:Col-end
(
edit
)
Template:Mono
(
edit
)
Template:Pipe
(
edit
)
Template:Refimprove
(
edit
)