Increment and decrement operators

Increment and decrement operators

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 became notorious for featuring two versions (pre- and post-) of each operator with slightly different semantics.

The increment operator increases the value of its operand by 1. The operand must have an arithmetic data type, and must refer to a modifiable data object. Similarly, the decrement operator decreases the value of its modifiable arithmetic operand by 1.

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. In contrast, 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.

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 operators should be performed. Situations like this are made even worse when optimizations are applied by the compiler, which could result in the order of execution of the operations to be different than what the programmer intended.

Contents

Examples

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-increment operator is commonly used with pointers:

// Copy one array to another using pointers
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

The following list, though not complete or all-inclusive, lists some of the major programming languages that support increment/decrement operators.

References

See also



Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Operators in C and C++ — This is a list of operators in the C and C++ programming languages. All the operators listed exist in C++; the fourth column Included in C , dictates whether an operator is also present in C. Note that C does not support operator overloading.… …   Wikipedia

  • Increment — An increment is an increase of some amount, either fixed or variable. For example one s salary may have a fixed annual increment or one based on a percentage of its current value. A decrease is called a decrement. Increment or incremental may… …   Wikipedia

  • GNU Octave — screenshot Developer(s) …   Wikipedia

  • Plus and minus signs — This article is about the mathematical symbols. For other uses of plus , see Plus (disambiguation). For other uses of minus , see Minus (disambiguation). The plus and minus signs (+ and −) are mathematical symbols used to represent the notions of …   Wikipedia

  • Arity — In logic, mathematics, and computer science, the arity i/ˈær …   Wikipedia

  • PDP-11 architecture — The PDP 11 architecture is an instruction set architecture (ISA) developed by Digital Equipment Corporation (DEC). It is implemented by central processing units (CPUs) and microprocessors used in minicomputers of the same name. Additional… …   Wikipedia

  • Counter machine — A counter machine is an abstract machine used in formal logic and theoretical computer science to model computation. It is the most primitive of the four types of register machines. A counter machine comprises a set of one or more unbounded… …   Wikipedia

  • Garbage collection (computer science) — This article is about garbage collection in memory management. For garbage collection in an SSD, see garbage collection (SSD). For other uses, see garbage collection. In computer science, garbage collection (GC) is a form of automatic memory… …   Wikipedia

  • C (programming language) — C The C Programming Language[1] (aka K R ) is the seminal book on C …   Wikipedia

  • C++ classes — For background information, see C++. The C++ programming language allows programmers to separate program specific datatypes through the use of classes. Instances of these datatypes are known as objects and can contain member variables, constants …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”