# Post increment and pre increment operators

Working with Post increment and pre increment operators is one of the wonderful scenarios that still some of the highly professional IT people are confused about. People often use i++ (a post-increment operation), but they never knew how does it actually work? How does it differ from ++i, which is a pre-increment operation? It definitely serves our need to increment the value of a particular variable by 1, but it’s usage without understanding the concept behind, might lead you to sleepless nights in worst cases.

Let’s start with understanding the operation i++, which is a post-increment operation, where ++ is the operator and i is the variable. When we use it somewhere in our application, it actually represents a two-step operation as:

1. return i; // Whatever is the current value of i, just return as it is
2. i = i + 1; // Increment the value of i by 1

It’s just the opposite with the pre-increment operation, where the two-step operation involves:

1. i = i + 1; // Increment the value of i by 1
2. return i; // Whatever is the current value of i, just return as it is

Now let’s check out some of the examples:

Let’s try to explain each of the steps:

1. The first two lines inside main method are initializing the variables i and j.

2. j = ++i;
This step involves the pre-increment operation on i, so we now know that this will involve a two-step operation that we discussed about earlier in this post, where the value of i is incremented by 1 first (so the value of i now becomes 1), and then the value of i is returned, which gets assigned to (so the value of j also becomes 1).

3. j = i++;
This step involves the post-increment operation on i, so this will also involve a similar two-step operation, where the value of i is returned first, which gets assigned to (so the value of j also becomes 1), and later the value of i is incremented by 1 (so the value of i now becomes 2).

I hope you would have created a clear picture on this now. Anyways, let’s see some more examples on this.

The above example is a very similar one to what we have already seen.

1. The first line inside main method is initializing the variable i.
2. The second line involves the post-increment operation on i, so it returns the current value of i first, which is 0 and then increments i by 1.
3. The third line simply prints the current value of i, which is 1.

Note that the post/pre-increment/decrement operations on a variable produces different outputs only when the intermediate value of the concerned variable that is returned as part of the two-step process we already discussed (applicable to each of the post/pre-increment/decrement operations) gets truly consumed/utilized by a same/different variable or by some method call.

Hopefully you got it what I wanted to say, but if not, let’s check out the below example:

Output:

Note that both the loops execute with variable i initialized as 0, and both the loops produce the same output despite using different increment strategy for each of them. I used pre-increment for the first loop and post-increment for the second loop, but still the output remained the same for both. How?

Yes, you guessed it right! As I mentioned in the highlighted note above, single statements such as ++i and i++ won’t create any difference at all in their outputs. But why so? Because, in the pre-increment operation (++i), the value of i gets incremented first and later the current value of i is returned. But where is the current value being returned now? It’s to nowhere, since there is no one to utilize/consume the returned value as part of this operation. Similar is with the post-increment operation (i++), the current value of i is returned first and later it’s value is incremented by 1. But where is the value getting returned? Same, nowhere. Contrary to the earlier examples, there was a very generous variable j or System.out.print() method calls to consume the value i returned every time there was a post/pre-increment operation on it.

The concept of increment and decrement operators is the same irrespective of the programming language involved. So it’s important that you understand the basic essence of such operations.

Actual internal implementation might be different from what has been explained. The approach for explanation on this topic was intentionally decided so as to make you understand the basic concept in a best way possible without giving way to any confusions.

Kindly drop in for any further queries/clarifications.