GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||
GeezerVison is showing en.wikipedia.org — Augmented assignment - Wikipedia
For augmented arithmetic operations, see IEEE_754 § Augmented_arithmetic_operation.
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. DiscussionFor 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 += 1; With this version, there is no excuse for a compiler failing to generate code that looks up the location of variable 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 LongName:=x + sqrt(LongName)*7; would become LongName:=x + sqrt(~)*7; and so forth. This is more general than just SemanticsIn expression-oriented programming languages such as C, assignment and augmented assignment are expressions, which have a value. This allows their use in complex expressions. In other languages, such as Python, assignment and augmented assignment are statements, not expressions, and thus cannot be used in horizontally complex expression. For example, the following is valid C, but not valid Python: a += b += c; Instead, Python trades it off for increased "vertical code complexity". In this 2x loop unrolled calculator of the Fibonacci sequence, what takes expression-oriented languages just 9 lines of code would require 16 lines to accomplish. Splitting the core looping expression def fib(n: int) -> int: if n <= 2: return int(n > 0) a: int = 1 b: int = 0 if n & a: n += a else: b = a n -= 2 while n: n -= 2 b += a a += b return a 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.[1] Being an lvalue allows it to be written on the left-hand-side of some other assignment statement:[2] int x = 11; (x *= 2) += 3; // Sets x to 25 Computed assignment locationsIn 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 If a language implements augmented assignment by macro expansion to By languageC descendantsIn C, C++, and C#, the assignment operator is
Each of these is called a compound assignment operator in said languages.[1][3][4][5] Supporting languagesThe following list, though not complete or all-inclusive, lists some of the major programming languages that support augmented assignment operators.
See also
References
| |||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |