text:
Increment and decrement operators
From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (September 2014) (Learn how and when to remove this template message)
Increment and decrement operators are unary operators that add or subtract one from their operand, respectively. They are commonly implemented in imperative programming languages. C-like languages feature two versions (pre- and post-) of each operator with slightly different semantics.
In languages syntactically derived from B (including C and its various derivatives), the increment operator is written as ++ and the decrement operator is written as --. Several other languages use inc(x) and dec(x) functions.
The increment operator increases the value of its operand by 1. The operand must have an arithmetic or pointer data type, and must refer to a modifiable data object. Similarly, the decrement operator decreases the value of its modifiable arithmetic operand by 1. Pointers values are increased (or decreased) by an amount that makes them point to the next (or previous) element adjacent in memory.
In languages that support both versions of the operators：
The pre-increment and pre-decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value.
The post-increment and post-decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's original value prior to the increment (or decrement) operation.
In languages where increment/decrement is not an expression (e.g. Go), only one version is needed (in the case of Go, post operators only).
Since the increment/decrement operator modifies its operand, use of such an operand more than once within the same expression can produce undefined results. For example, in expressions such as x - ++x, it is not clear in what sequence the subtraction and increment operations should be performed. Such expressions generally invoke undefined behavior, and should be avoided.
Contents
1 Examples
2 Supporting languages
3 History
4 See also
5 References
Examples[edit]
The following C code fragment illustrates the difference between the pre and post increment and decrement operators:
int x; int y; // Increment operators x = 1; y = ++x; // x is now 2, y is also 2 y = x++; // x is now 3, y is 2 // Decrement operators x = 3; y = x--; // x is now 2, y is 3 y = --x; // x is now 1, y is also 1
The post-increment operator is commonly used with array subscripts. For example:
// Sum the elements of an array float sum_elements(float arr[], int n) { float sum = 0.0; int i = 0; while (i < n) sum += arr[i++]; // Post-increment of i, which steps // through n elements of the array return sum; }
Likewise, the post-decrement operator is commonly used with pointers:
// Copy one array to another void copy_array(float *src, float *dst, int n) { while (n-- > 0) // Loop that counts down from n to zero *dst++ = *src++; // Copies element *(src) to *(dst), // then increments both pointers }
Note that these examples also work in other C-like languages, such as C++, Java, and C#.
Supporting languages[edit]
The following list, though not complete or all-inclusive, lists some of the major programming languages that support the ++/-- increment/decrement operators.
AWK[1]
Bash[2]
C[3]
C++[4]
C#[5]
CFML
D[6]
Go
Java
JavaScript
Objective-C
GNU Octave
Perl
PHP
Vala
Wolfram Language[7]
(Apple's Swift once supported these operators,[8] but support was removed as of version 3.)
Pascal, Delphi, Modula-2, and Oberon provide the same functions, but they are called inc(x) and dec(x).
History[edit]
The concept was introduced in the B programming language circa 1969 by Ken Thompson.[9]
Thompson went a step further by inventing the ++ and -- operators, which increment or decrement; their prefix or postfix position determines whether the alteration occurs before or after noting the value of the operand. They were not in the earliest versions of B, but appeared along the way. People often guess that they were created to use the auto-increment and auto-decrement address modes provided by the DEC PDP-11 on which C and Unix first became popular. This is historically impossible, since there was no PDP-11 when B was developed. The PDP-7, however, did have a few 'auto-increment' memory cells, with the property that an indirect memory reference through them incremented the cell. This feature probably suggested such operators to Thompson; the generalization to make them both prefix and postfix was his own. Indeed, the auto-increment cells were not used directly in implementation of the operators, and a stronger motivation for the innovation was probably his observation that the translation of ++x was smaller than that of x=x+1.
See also[edit]
Augmented assignment – for += and -= operators
PDP-7
PDP-11
Successor function
References[edit]
Jump up ^ "GNU Awk's User Guide". Free Software Foundation.
Jump up ^ "8.3. The Double-Parentheses Construct". The Linux Documentation Project.
Jump up ^ Ritchie, Brian W. Kernighan; Dennis M.; Ritchie, Dennis (1988). The C programming language (2. ed., [Nachdr.] ed.). Englewood Cliffs, N.J.: Prentice Hall. p. 18. ISBN 0-13-110362-8.
Jump up ^ "Increment/decrement operators". cppreference.com.
Jump up ^ "++ Operator (C# Reference)". Microsoft Developer Network.
Jump up ^ "Operator Overloading". dlang.org.
Jump up ^ "Increment Wolfram Language Symbol". Wolfram Language Documentation Center.
Jump up ^ "Basic Operators". developer.apple.com.
Jump up ^ Ritchie, Dennis M. (March 1993). "The Development of the C Language". ACM SIGPLAN Notices. 28 (3): 5. doi:10.1145/155360.155580.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Increment_and_decrement_operators&oldid=844195181"
Categories:
Operators (programming)
Unary operations
Hidden categories:
Articles needing additional references from September 2014
All articles needing additional references
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Article
Talk
Variants
Views
Read
Edit
View history
More
Search
Navigation
Main page
Contents
Featured content
Current events
Random article
Donate to Wikipedia
Wikipedia store
Interaction
Help
About Wikipedia
Community portal
Recent changes
Contact page
Tools
What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Wikidata item
Cite this page
Print/export
Create a book
Download as PDF
Printable version
Languages
العربية
Azərbaycanca
Беларуская
Dansk
Deutsch
Español
Français
한국어
Nederlands
日本語
Norsk nynorsk
Polski
Русский
Српски / srpski
Svenska
Українська
中文
8 more
Edit links
This page was last edited on 3 June 2018, at 08:28 (UTC).
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.
Privacy policy
About Wikipedia
Disclaimers
Contact Wikipedia
Developers
Cookie statement
Mobile view
Enable previews

key: html

type: local

value: div

attrs: id class

values: mw-page-base noprint

values: mw-head-base noprint

value: a

attrs: id class lang

values: firstHeading firstHeading en

values: siteSub noprint

values: mw-jump-link #mw-head

values: mw-jump-link #p-search

attrs: href class

value: td

attrs: href title

value: b

values: Help:Introduction to referencing with Wiki Markup/1

value: span

value: i

value: small

values: plainlinks metadata ambox ambox-content ambox-Refimprove

values:
/wiki/Unary_operator
mw-redirect
Unary operator

values:
/wiki/Operator_(programming)
mw-redirect
Operator (programming)

values:
/wiki/Data_object
mw-redirect
Data object

values: checkbox button toctogglecheckbox toctogglecheckbox display:none

values: width: 25%;text-align: left; vertical-align: top;

values: border-collapse: collapse; padding: 0; border: 0; background:transparent; width:100%;

values:
/wiki/Delphi_(programming_language)
mw-redirect
Delphi (programming language)

values:
nofollow
external text
//doi.org/10.1145/155360.155580

values:
reflist mw-references-wrap
list-style-type: decimal;

values: Category:Articles needing additional references from September 2014

values: Category:All articles needing additional references

values: Discussion about edits from this IP address [ctrl-alt-n]

values: A list of edits made from this IP address [ctrl-alt-y]

values: You are encouraged to create an account and log in; however, it is not mandatory

values: You're encouraged to log in; however, it's not mandatory. [ctrl-alt-o]

values:
/wiki/Increment_and_decrement_operators
View the content page [ctrl-alt-c]
c

values: Discussion about the content page [ctrl-alt-t]

values: p-namespaces navigation vectorTabs p-namespaces-label

values:
p-variants
navigation
vectorMenu emptyPortlet
p-variants-label

values: Past revisions of this page [ctrl-alt-h]

values: p-views navigation vectorTabs p-views-label

values:
p-cactions
navigation
vectorMenu emptyPortlet
p-cactions-label

values:
mw-wiki-logo
/wiki/Main_Page
Visit the main page

values:
/wiki/Main_Page
Visit the main page [ctrl-alt-z]
z

values: Featured content – the best of Wikipedia

values: Find background information on current events

values:
/wiki/Special:Random
Load a random article [ctrl-alt-x]
x

values: portal navigation p-navigation p-navigation-label

values: Guidance on how to use and edit Wikipedia

values: About the project, what you can do, where to find things

values: A list of recent changes in the wiki [ctrl-alt-r]

values: portal navigation p-interaction p-interaction-label

values: List of all English Wikipedia pages containing links to this page [ctrl-alt-j]

values: Recent changes in pages linked from this page [ctrl-alt-k]

values:
/wiki/Wikipedia:File_Upload_Wizard
Upload files [ctrl-alt-u]
u

values: A list of all special pages [ctrl-alt-q]

values: Permanent link to this revision of the page

values: Link to connected data repository item [ctrl-alt-g]

values: Printable version of this page [ctrl-alt-p]

values: مشغل الزيادة والنقص – Arabic

values:
interlanguage-link interwiki-az
display: none;

values: Інкрэмент – Belarusian

values:
interlanguage-link interwiki-be
display: none;

values:
interlanguage-link interwiki-da
display: none;

values: Operadores de incremento y decremento – Spanish

values: 증감 연산자 – Korean

values:
https://nl.wikipedia.org/wiki/Increment
Increment – Dutch
nl
nl
interlanguage-link-target

values:
interlanguage-link interwiki-nl
display: none;

values: インクリメント – Japanese

values: Inkrement og dekrement – Norwegian Nynorsk

values:
interlanguage-link interwiki-nn
display: none;

values:
interlanguage-link interwiki-pl
display: none;

values: Инкремент – Russian

values: Инкремент – Serbian

values:
interlanguage-link interwiki-sr
display: none;

values:
https://sv.wikipedia.org/wiki/%2B%2B
++ – Swedish
sv
sv
interlanguage-link-target

values: Оператори інкременту та декременту – Ukrainian

values: 增值和减值操作符 – Chinese

values: /static/images/wikimedia-button-1.5x.png 1.5x, /static/images/wikimedia-button-2x.png 2x

values: /static/images/poweredby_mediawiki_132x47.png 1.5x, /static/images/poweredby_mediawiki_176x62.png 2x

values:
suggestions
display: none; font-size: 13px;

values: mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Increment_and_decrement_operators rootpage-Increment_and_decrement_operators skin-vector action-view