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
(section)
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!
==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.
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)