Adding 1 to a number, or subtracting 1 from a number, are both so common in computer programs that PHP, Java, C++, and C# incorporate a special set of operators to do this. They supports two types of incrementing and decrementing operators:

  • pre-incrementing/decrementing operators
  • post-incrementing/decrementing operators

Pre-incrementing/decrementing operators are placed before the variable name. Post-incrementing/decrementing operators are placed after the variable name. These four types of operators are shown here.

PHP

Operator Description Example Equivalent to
Pre-incrementing Increment a variable by one ++$a; $a = $a + 1
Pre-decrementing Decrement a variable by one --$a; $a = $a - 1
Post-incrementing Increment a variable by one $a++; $a = $a + 1
Post-decrementing Decrement a variable by one $a--; $a = $a - 1

Java

Operator Description Example Equivalent to
Pre-incrementing Increment a variable by one ++a; a = a + 1
Pre-decrementing Decrement a variable by one --a; a = a - 1
Post-incrementing Increment a variable by one a++; a = a + 1
Post-decrementing Decrement a variable by one a--; a = a - 1

C++

Operator Description Example Equivalent to
Pre-incrementing Increment a variable by one ++a; a = a + 1
Pre-decrementing Decrement a variable by one --a; a = a - 1
Post-incrementing Increment a variable by one a++; a = a + 1
Post-decrementing Decrement a variable by one a--; a = a - 1

C#

Operator Description Example Equivalent to
Pre-incrementing Increment a variable by one ++a; a = a + 1
Pre-decrementing Decrement a variable by one --a; a = a - 1
Post-incrementing Increment a variable by one a++; a = a + 1
Post-decrementing Decrement a variable by one a--; a = a - 1


Once again, looking at the “Equivalent to” column, it becomes clear that you can achieve the same result by just using the classic assignment operator ( = ). However, it is much more sophisticated and productive to use these new operators. In the beginning you may hate them, but as the years pass you are definitely gonna love them!

Notice: Please note that Visual Basic and Python do not support incrementing/decrementing operators.

PHP

Let’s see an example with a pre-incrementing operator,

and another one with a post-incrementing operator.

Java

Let’s see an example with a pre-incrementing operator,

and another one with a post-incrementing operator.

C++

Let’s see an example with a pre-incrementing operator,

and another one with a post-incrementing operator.

C#

Let’s see an example with a pre-incrementing operator,

and another one with a post-incrementing operator.


In both examples a value of 6 is assigned to variable b! So, where is the catch? Are these two examples equivalent? The answer is “yes,” but only in these two examples. In other cases the answer will likely be “no.” There is a small difference between them.

Let’s spot that difference! The rule is that a pre-increment/decrement operator performs the increment/decrement operation first and then delivers the new value. A post-increment/decrement operator delivers the old value first and then performs the increment/decrement operation. Look carefully at the next two examples.

PHP

and

Java

and

C++

and

C#

and


In the first example, variable a is incremented by one and then its new value is assigned to variable b. In the end, both variables contain a value of 6.

In the second example, the value 5 of variable a is assigned to variable b, and then variable a is incremented by one. In the end, variable a contains a value of 6 but variable b contains a value of 5!

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!