C++

C++: i++ vs ++i

One of many operators in C++ is the increment operator. It came up with two ways to be used, i.e. post-increment and pre-increment. Post-increment implies that the value of a certain variable will not be incremented until its last value is saved to some variable. While in the pre-increment method, the value of a variable will be incremented first and then saved to the variable, i.e. last value will be gone. Therefore, we have decided to explain and show you the working of both pre-increment and post-increment operator implementation in C++, i.e. “++i” and “i++”. Let’s start looking at some examples after the new file generation and launching in some editor, i.e. via the touch and nano keyword on the Ubuntu 20.04 shell.

Example 01:

Let’s get started with the initial illustration of using the increment operator. Firstly, we will have a look at the post-increment operator. So, the code has been initiated with C++ “iostream” header and namespace “std” with the “#include” and “using” words. Within the main() method, an integer “x” is initialized with 1 and printed out in the terminal with the “cout” standard statement. Here is the post-increment operator to raise the value of “x” by 1. It works like “x=x+1”. For this, the original value of “x” has been saved to its variable “x” and after that incremented by 1. The new updated value will be again printed on the console with standard “cout” of C++. The code ends here and is ready to be compiled now.

Ubuntu supports using a g++ compiler for C++ language to compile the code. Therefore, we have already installed it and are using it for the compilation of our new script. It goes smoothly, and then we have executed the new code file “incdic.cc” with the “./a.out” Ubuntu instruction. The original value 1 of variable “x” is displayed first and then the incremented value “2” by the use of the post-increment operator in the code.

Let’s come up with the pre-increment operator usage in the C++ code. The pre-increment operator increments the original value first and then saves it to the variable. The same headers, same namespace, and the same main() function have been utilized. The “x” variable has been assigned a value of 1. We have been using a simple standard “cout” statement of C++ to display it. Now the pre-increment operator is here along with the variable “x” to perform “x = 1 + x”. Therefore, the value of “x” has been incremented by 1 and becomes 2 using the “++” operator. After this, the new value has been saved to the variable “x” again and printed out on the shell with the “cout” statement. This code is complete and ready to get compiled on the terminal.

After this new code compilation, we have got it error-free. After using the “./a.out” query, the original value of “x” has been displayed below, i.e. 1. In last, the pre-incremented value of “x” is also displayed on the shell, i.e. 2.

Example 02:

Let’s take a look at something new here in our illustration. So, we have been starting the second example of C++ code with the same “std” namespace and header, i.e. iostream. At the start of our code’s main() method, we have declared two integer type variables, “y” and “z”. While the variable “y” has been initialized as well, i.e. y = 9. The first two standards “cout” lines for C++ are here to display the original and first values of both variables. i.e. y = 9, and z = 0. Now, it’s the turn for the operator to be used. So, we have been using the post-increment operator here to increment the value of variable “y” by 1 and save it to the variable “z”. But, you need to comprehend that it is not is so simple. The post-increment operator “z=y++” means that the original value “9” of variable “y” will be saved to the variable “z” first. Now, the variable “z” becomes 9. After this, the value of variable “y” will be incremented by 1 and become 10. When we display the values of both variables “x” and “y” now, it will show us the new values for both, i.e. “z = 9”, and “y = 10”. Let’s compile this code now.

After this code compilation and execution, both the original values were displayed on the first two output lines. The last 2 output lines show the new values added by the post-increment operator on variable “y”.

Let’s update the same code for the pre-increment operator now. Within the main() method, both variables have been declared the same as we did before, i.e. the code will not be changed except the increment operator line. The “z=++y” shows the pre-increment operator usage in the code. The statement “++y” means that the value “9” of variable “y” will be incremented by 1 first, i.e. become 10. After that, the new value would be saved to the variable “z” i.e. z also becomes 10. The cout statements are here to display the original and then the incremented values on the shell. This code is ready to be used on the terminal.

This code has been compiled and executed after the update. The output shows the firstly declared values of both variables and the pre-increment value for both variables “x” and “y”.

Example 03:

Let’s have our last example for this article. We have again started our code with the “iostream” package and “std” namespace of C++. The main() function is initialized with the initialization of an integer variable “I” to value 5. The cout clause is here to display this value on the shell. Another variable, “j” has been initialized while taking value from the post-increment of a variable “I”. The last value of “I” will be saved to the variable “j” i.e. “j=i=5”. After this, the value of a variable “I” will be increment by 1, i.e. “i=5+1”. Both the new values for “I” and “j” will be printed with “cout”. A variable “k” is initialized with the pre-increment of variable “j” now. This means the last value of “j” will be incremented first, i.e. “j=5+1=6”, and then saved to the new variable “k”. The values will be displayed with “cout”. Now, it is the turn for double pre-increment operator usage. The last value of variable “k” is incremented twice, i.e. “k=6+1=7+1=8”. This new value would be saved to the new variable “l”. Both the new values for “k” and “l” will be shown on the terminal with the help of the “cout” statement.

After this code execution, the output is the same as expected. Each incrementation step has been demonstrated quite well in the code and on the shell as well.

Conclusion:

This article is all about the difference between post-increment operators and pre-increment operators on some variables while implementing it on Ubuntu 20.04. This article has been assembled sequentially, starting from simple to complicated examples for better understanding. This unique way of explanation used within our article makes it more appealing for C++ students, learners, programmers, and developers.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.