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,
$a = 5;
++$a; //This is equivalent to $a = $a + 1
$b = $a;
echo $a;
echo $b;
and another one with a post-incrementing operator.
$a = 5;
$a++; //This is equivalent to $a = $a + 1
$b = $a;
echo $a;
echo $b;
Java
Let’s see an example with a pre-incrementing operator,
a = 5;
++a; //This is equivalent to a = a + 1
b = a;
System.out.println(a);
System.out.println(b);
and another one with a post-incrementing operator.
a = 5;
a++; //This is equivalent to a = a + 1
b = a;
System.out.println(a);
System.out.println(b);
C++
Let’s see an example with a pre-incrementing operator,
a = 5;
++a; //This is equivalent to a = a + 1
b = a;
cout << a << endl;
cout << b << endl;
and another one with a post-incrementing operator.
a = 5;
a++; //This is equivalent to a = a + 1
b = a;
cout << a << endl;
cout << b << endl;
C#
Let’s see an example with a pre-incrementing operator,
a = 5;
++a; //This is equivalent to a = a + 1
b = a;
Console.WriteLine(a);
Console.WriteLine(b);
and another one with a post-incrementing operator.
a = 5;
a++; //This is equivalent to a = a + 1
b = a;
Console.WriteLine(a);
Console.WriteLine(b);
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
$a = 5;
$b = ++$a;
echo $a; //Outputs: 6
echo $b; //Outputs: 6
and
$a = 5;
$b = $a++;
echo $a; //Outputs: 6
echo $b; //Outputs: 5
Java
a = 5;
b = ++a;
System.out.println(a); //Outputs: 6
System.out.println(b); //Outputs: 6
and
a = 5;
b = a++;
System.out.println(a); //Outputs: 6
System.out.println(b); //Outputs: 5
C++
a = 5;
b = ++a;
cout << a << endl; //Outputs: 6
cout << b << endl; //Outputs: 6
and
a = 5;
b = a++;
cout << a << endl; //Outputs: 6
cout << b << endl; //Outputs: 5
C#
a = 5;
b = ++a;
Console.WriteLine(a); //Outputs: 6
Console.WriteLine(b); //Outputs: 6
and
a = 5;
b = a++;
Console.WriteLine(a); //Outputs: 6
Console.WriteLine(b); //Outputs: 5
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!