When the increment or decrement operator is placed before the operand or to the operands left the operator is being used in?

If used postfix, with operator after operand (for example, x--), the decrement operator decrements and returns the value before decrementing.

If used prefix, with operator before operand (for example, --x), the decrement operator decrements and returns the value after decrementing.

The decrement operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets). --x itself evaluates to a value, not a reference, so you cannot chain multiple decrement operators together.

--(--x); // SyntaxError: Invalid left-hand side expression in prefix operation

If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.

If used prefix, with operator before operand (for example, ++x), the increment operator increments and returns the value after incrementing.

The increment operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets). ++x itself evaluates to a value, not a reference, so you cannot chain multiple increment operators together.

++(++x); // SyntaxError: Invalid left-hand side expression in prefix operation

Increment and decrement operators are unary operators that add or subtract one, to or 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, and the decrement operator decreases, 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. 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 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.

In languages with typed pointers like C, the increment operator steps the pointer to the next item of that type -- increasing the value of the pointer by the size of that type. When a pointer (of the right type) points to any item in an array, incrementing (or decrementing) makes the pointer point to the "next" (or "previous") item of that array. When a pointer has a type of pointer-to-integer, incrementing that pointer makes it point to the next integer (typically increasing it by 4 bytes).[1] When a pointer has a type pointer-to-employee, incrementing that pointer makes it point to the next "employee" -- if the size of the employee structure is 106 bytes, incrementing that pointer increases it by 106 bytes.[2]

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
// Pre-increment: x is incremented by 1, then y is assigned the value of x
x = 1;
y = ++x;    // x is now 2, y is also 2

// Post-increment: y is assigned the value of x, then x is incremented by 1
x = 1;
y = x++;    // y is 1, x is now 2

// Decrement operators
// Pre-decrement: x is decremented by 1, then y is assigned the value of x
x = 1;
y = --x;    // x is now 0, y is also 0

// Post-decrement: y is assigned the value of x, then x is decremented by 1
x = 1;
y = x--;    // y is 1, x is now 0

In languages lacking these operators, equivalent results require an extra line of code:

# Pre-increment: y = ++x
x = 1
x = x + 1  # x is now 2  (can be written as "x += 1" in Python)
y = x      # y is also 2

# Post-increment: y = x++
x = 1
y = x      # y is 1
x = x + 1  # x is now 2


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;
}

The post-increment operator is also 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#.

  • Increment operator can be demonstrated by an example:

    #include <stdio.h>
    int main()
    {
        int c = 2;
        printf("%d\n", c++); // this statement displays 2, then c is incremented by 1 to 3.
        printf("%d", ++c);   // this statement increments c by 1, then c is displayed.
        return 0;
    }
    

    • Output:

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.

  • ABAP
  • AWK[3]
  • Bash[4]
  • C[5]
  • C++[6]
  • C#[7]
  • CFML
  • D[8]
  • Go
  • Java
  • JavaScript
  • Objective-C
  • GNU Octave
  • PARI/GP[9]
  • Perl
  • PHP
  • PowerShell[10]
  • Vala
  • Vex, a scripting language in the software Houdini
  • Wolfram Language[11]

(Apple's Swift once supported these operators,[12] 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).

Notably Python and Rust do not support these operators.

History[edit]

The concept was introduced in the B programming language circa 1969 by Ken Thompson.[13]

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]

  1. ^ Richard M Reese. "Understanding and Using C Pointers". "Chapter 4. Pointers and Arrays". O'Reilly Media, Inc. 2013. ISBN: 9781449344184
  2. ^ Richard Petersen. "Introductory C with C++". 2019. Figure 12-12.
  3. ^ "GNU Awk's User Guide". Free Software Foundation.
  4. ^ "8.3. The Double-Parentheses Construct". The Linux Documentation Project.
  5. ^ 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.
  6. ^ "Increment/decrement operators". cppreference.com.
  7. ^ "++ Operator (C# Reference)". Microsoft Developer Network.
  8. ^ "Operator Overloading". dlang.org.
  9. ^ "GP Operators and their Priorities".
  10. ^ "About Assignment Operators".
  11. ^ "Increment Wolfram Language Symbol". Wolfram Language Documentation Center.
  12. ^ "Basic Operators". developer.apple.com.
  13. ^ Ritchie, Dennis M. (March 1993). "The Development of the C Language". ACM SIGPLAN Notices. 28 (3): 5. doi:10.1145/155360.155580.

When the increment or decrement operator is placed before the operand or to the operand's left the operator is being used in?

When the increment or decrement operator is placed before the operand (or to the operands left) the operator is being used in the prefix mode. You just studied 37 terms!

When increment or decrement operator is used before the operand it is known as?

The ++ operator increments its single operand. The operator converts its operand to a number, adds 1 to that number and assigns the incremented value back into the variable. When ++ is used before the operand, where it is known as the pre-increment operator.

What is increment and decrement operator used for?

Increment ++ and Decrement -- Operator as Prefix and Postfix In programming (Java, C, C++, JavaScript etc.), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1.

What means increment or decrement value of a running variable?

Adding or subtracting 1 from a variable is a very common programming practice. Adding 1 to a variable is called incrementing and subtracting 1 from a variable is called decrementing.