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