You Might Be Using ++i and i++ Wrong

The pre and post-increment operators aren’t interchangeable.

Alyssa Atkinson
Feb 14 · 5 min read
Photo by Christopher Gower on Unsplash

I was a sophomore in college when I started coding for the first time. I had never done any coding on my own time, never taken any special classes in high school, and I didn’t know what a terminal was or what a semi-colon was used for outside of the English language.

Computer programming truly was a foreign concept to me when I began my introduction to C++ programming course, and to say I learned slowly would be an understatement.

Programming never came easily or naturally to me. I failed a handful of quizzes in the first few weeks, and I wondered if I would ever make it through. If it weren’t for my stubborn attitude and unwillingness to give up, I’m not even sure that I would have graduated with the degree I did.

Yet, through patience, persistence, and endless hours of hard work, I learned. I learned to end each statement with a semi-colon. I learned how to compile and run code from my terminal. I learned when to use the pre-increment operator and when to use the post-increment operator, and yes, there is a difference.

To this day, coders of all levels still misuse the pre and post-increment operators. I won’t hold it against you if you don’t know the difference, because I was in your shoes once. However, I do think it’s important enough to share. The following explains the difference between the two operators, and how they are actually executed.

The Post-Increment Operator

The post-increment operator is one way to increment a value by one. It is used:

“to increment the value of variable after executing expression completely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented.”

For example, let’s say you have the following snippet of code:

j = i++;

This is really carried out like:

j = i;
i++;

So, if ‘i’ was initially set to 5, ‘j’ would equal 5 after the line is executed. Essentially, with the post-increment operator, the increment is executed after the other variable is set.

The Pre-Increment Operator

The pre-increment operator is another way to increment a value by one. It is used:

“to increment the value of a variable before using it in a expression. In the Pre-Increment, value is first incremented and then used inside the expression.”

For example, let’s say you have the following snippet of code:

j = ++i;

This is really carried out like:

++i;
j = i;

So, if ‘i’ was initially set to 5, ‘j’ would equal 6 after the line is executed. Essentially, with the pre-increment operator, the increment is executed before the other variable is set.

The Special Case

There is one more case that can be a bit tricky if you aren’t aware of it. When you try to set a variable to the post-increment of itself, you actually don’t change the value at all.

Let’s say you have the following code snippet:

i = i++;

If the value of ‘i’ was initially 5, it will remain 5, just like the variable ‘j’ did in the post-increment example above. This case is a bit counterintuitive, because you would think when incrementing the variable itself you would end up with a value that is one more than the initial value, but that isn’t the case here.

It’s an easy example to get confused about, so I felt it was important to point out.

How to Test the Differences

Now, if you want to visually test out and see the differences between the two operators, I don’t blame you. This was one of the top ways I learned to become a better coder. When I wasn’t sure how a snippet of code worked, I always ran it to see the output and it helped me digest the output in an easy to understand, visual way.

All you have to do is open up a new C++ file and add the necessary header to the file. In this case, all you need is:

#include <iostream>
using namespace std;

Then, you will want to declare and define your main as follows:

int main()
{
return 0;
}

Before your return statement, that is where you will enter your code snippets:

int i = 5;
int j = i++;

Then, write a simple output statement, such as:

cout << i << endl;
cout << j << endl;
j = ++i;
cout << j << endl;

You will be able to compare the post-increment and pre-increment operators respectively, and see the values change with regards to the initial value you set variables ‘i’ and ‘j’ to.

In this case, ‘j’ would first output 5, then ‘i’ would increment to 6, and then ‘j’ would be set to the increment of ‘i’, and output 7 for the second output statement.

How To Run the File

If you are a brand new programmer, make sure to save the file as a .cpp file. You could call it something like test.cpp. After you do so, you can simply open your terminal which you typically compile on, and issue the following command:

g++ -Wall test.cpp

This will create an executable file called a.out, which you can then run using the command:

./a.out

You will see the final results print directly to the screen, and you will have your visual answers along with the explanation I have already provided for both the pre-increment and post-increment operators.

Final Thoughts

While many people believe that the pre and post-increment operators are completely interchangeable, that actually isn’t true. They look similar, but are executed in two completely different ways.

The pre and post-increment operators are essential to have in your tool belt, but only if you know how to distinguish them and use them correctly.

With the quick and simple explanation and examples that were shared, you will be able to understand and utilize each one properly and effectively.

Quick Code

Find the best tutorials and courses for the web, mobile…

Alyssa Atkinson

Written by

Ohio U XC/Track alum. I love to run. I blog about food, health, fitness, lifestyle, + more! My Links (Blog, YouTube, etc.): https://linktr.ee/nomeatfastfeet

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Alyssa Atkinson

Written by

Ohio U XC/Track alum. I love to run. I blog about food, health, fitness, lifestyle, + more! My Links (Blog, YouTube, etc.): https://linktr.ee/nomeatfastfeet

Quick Code

Find the best tutorials and courses for the web, mobile, chatbot, AR/VR development, database management, data science, web design and cryptocurrency. Practice in JavaScript, Java, Python, R, Android, Swift, Objective-C, React, Node Js, Ember, C++, SQL & more.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store