pre increment or post increment in c++

Pre increment in c++

The increment operator has very simple meaning increasing in the value but thinks that Is increment is done before reaching the value or increment done after passing the value?
 
This question creates doubt in the mind because during the execution of the code this thing matters a lot.
sometimes logical error happened due to pre or post-increment and we can not resolve because we do not about the difference between pre-increment or post-increment.

In pre-increment increment in the value before reaching the value.

syntax like that:

int num;

++num;

cout<<num;

Post increment in c++

In post-increment increasing value after passing through the value.

syntax like that:

  1. int num;

  2. num++;

  3. cout<<num;
post increment

Run this example:
#include<iostream>
using namespace std;
int main()
{
int num1=0;
int num=0;
cout<<++num1*num++;                    //++num1=1 and num++=0

return 0;

}
output:
0


Challenge for you
Try to write this code for producing output =1;



What is the difference between post-increment and pre-increment?




#include<iostream>
using namespace std;
int main()
{
int value=10;
cout<<(++(value))<<endl;             //pre-increment
cout<<(value++)<<endl;              //post-increment
cout<<(++(++value))<<endl;              //double pre-increment
cout<<(value++)<<endl;                  //post-increment

return 0;

}
Output
11
11
14
14


In this example, simply increment and double increment use to understand the difference of pre-increment and post-increment. The first line of cout pre-increment and increase in the value instantly but in the second line, post-increment do not increment the value instantly.

In the third line, double pre-increment but 14 is displayed.This is happened due to post-increment in the previous statement.

In the fourth line, output 14 is the same because of post-increment.


Another example for more understanding the difference between post and pre-increment

#include<iostream>
using namespace std;
int main()
{
int number=10;
cout<<(++(number))<<endl;   //pre-increment 
cout<<(number++)<<endl;                  //post increment
cout<<(++(++number))<<endl;              //pre-increment and again pre-increment
cout<<((number++)+(++number))<<endl;              //first number post increment and second is pre-increment
cout<<((number++)+(number++))<<endl;              //first number pre-increment and second number also post -increment
return 0;

}

Output

11
11
14
30
33

In the above example, most of the cases are cover which are faced during the pre-increment and post-increment. 

In the first line, pre-increment and an increase in the value of the number. In the second line,post-increment and not increase in the value of the number as shown second output in the output of the code.

 But after the second line increase in the value of the number. As reach in the third line, the value becomes 12 and again two times increment in the value then value becomes 14 as shown in the output.


In the fourth line, first is post-increment and not increment in the 14. After that pre-increment and value become 16 because the previous pre-increment happened. The output of the fifth line is 16+14=30.

In the sixth line, the value is 16, and both times post-increment. In the first time remain value 16 but in the second time value is 17 then sum 16+17=33.In this way, output is shown by the compiler.

If you do not about the cout then learn cout.



From the above examples, you will find the solution of the difference between pre-increment and post-increment.


Please write the comment,if you find anything incorrect,any problem in the above topic or you want to share more information about the above topic.

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

How to format in HTML