Increment and decrement
With the gradual increase or reduction in size or variables is the increment (from latin incrementare , enlarge ' ) or decrement (from Latin. Decrementare , reduce') of the predetermined amount of change. An example is a turnstile in a swimming pool where the counter is increased by one for each visitor. However, the verbs increment and decrement are mainly used in computer science and denote the increase or decrease of an integer variable by one.
Computer science
Increment and decrement are elementary operations that are usually performed at the machine level by a single command. The construction of loops is often based on incrementing or decrementing a variable . In the most common case, the amount of the increment and the decrement is 1.
Implementation in various programming languages
Often the increment or decrement operation is only defined for integer variables, sometimes also for enumeration types (example: days of the week ). In the Perl programming language , the operation can also be applied to strings.
Even if an increment operation is implemented with a machine command in many programming languages, this is not the case everywhere. For example, in Java, an increment or decrement of a variable of a primitive data type is not atomic ; this means that this operation is already divided into several (machine) commands at the Java bytecode level .
Code samples
i and j denote variables here.
Programming languages | code | effect |
---|---|---|
Brainfuck |
+
|
The current cell is incremented by 1 |
-
|
Decrement the current cell by 1 | |
C , C ++ , C # , Perl , PHP , Java and JavaScript |
i++;
|
Post increment |
++i; |
Pre-increment | |
i += j; |
i is increased by j increases | |
i--; |
Post decrement | |
--i; |
Pre-decrement | |
i -= j; |
i is increased by j reduced | |
COBOL |
ADD j TO i
|
i is increased by j increases |
ADD -j TO i
|
i is increased by j reduced | |
Pascal |
Inc(i);
|
i is increased by 1 |
Inc(i,j);
|
i is increased by j increases | |
Dec(i); |
i is decreased by 1 | |
Dec(i,j); |
i is increased by j reduced | |
PowerBASIC |
Incr i
|
i is increased by 1 |
Decr i
|
i is decreased by 1 | |
Python , Ruby and Visual Basic |
i += 1
|
i is increased by 1 |
i += j |
i is increased by j increases | |
i -= 1 |
i is decreased by 1 | |
i -= j |
i is increased by j reduced |
Perl, PHP, and the majority of other high-level programming languages increment and decrement in the same way as C, with the difference that scalar variables are identified by the prefix $
.
Time of value assignment in higher-level expressions
If an increment or decrement operation is used in an instruction within another expression, a distinction must be made as to whether the value increase or decrease takes place before or after the evaluation of the higher-level expression. With the pre-decrement, the already reduced value is transferred to the superordinate expression, while with the post-decrement, the original value is transferred.
Explanation | Example in Java code | ||
---|---|---|---|
Increment | Post increment | Subsequent increase in value. |
int i = 5;
int c = i++; // c = 5
|
Pre-increment | Previous increase in value |
int i = 5;
int d = ++i; // d = 6
|
|
Decrement | Post decrement | Subsequent reduction in value. |
int i = 5;
int e = i--; // e = 5
|
Pre-decrement | Previous value reduction |
int i = 5;
int f = --i; // f = 4
|
Economy
In economics , the increment is the specified minimum increase in the bid at auctions .
Digital measurement technology
In digital measurement technology , where the quantity to be measured is divided into discrete values, the resolution corresponds to the increment .
plotter
In plotters and other drawing machines, the increment is the fixed smallest possible step size of the drawing head. A line or curve is divided into equal increments.
time to think
In games with a fixed time limit (e.g. chess ), the time credit that a player receives per move is called an increment.
See also
Individual evidence
- ↑ Rules for using volatile. Angelika Langer, accessed on January 3, 2010