Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Dear Al,
Very Good morning.
I am facing some conceptual doubts in post increament and pre increament operators in for loop.
Kindly follow the code below:

C++
for(int i=0;i<5;i++)
{
cout<<i<<endl;
}


output:
0
1
2
3
4


Again,

C++
for(int j=0;j<5;++j)
{
cout<<j<<endl;
}

Output will be same.

My question is how the processing of the code takes place?
++j & i++ both are possessing the same output, how?
Posted
Updated 1-Aug-10 8:50am
v3
Comments
Sauro Viti 30-Jul-10 3:19am    
I saw the same question from you in the C/C++/MFC forum. Please, don't post the same question to multiple forums.

The side-effect of ++i and i++ is the same - the value of i is incremented. However the value of the expression is different.
int x = 2;
int j = ++x; // now x == 3 and j == 3
int q = x++; // now x == 4 and q == 3

So pre-increment is increment before use, and post-increment is increment after use.
 
Share this answer
 
To understand how pre and post increment work, we need an example different from the one you wrote; look at this:

C++
int i = 0;
int j = i++;
int k = ++i;
printf("i = %d\nj = %d\nk = %d\n", i, j, k);


This will produce an output like:

i = 2
j = 0
k = 2


The statement int j = i++; means: first assign to j the value of i and after increment i (it is the same than int j = i; i = i + 1;)
The statement int k = ++i; means: first increment i and after assign to k the value of i (it is the same than i = i + 1; int k = i;)
 
Share this answer
 
Comments
SnowHow 30-Jul-10 8:34am    
Reason for my vote of 5
Illustrates the asm operation difference between the two.
The part after the second semicolon in a for line -
will be processed after the first loop primary :)

So you can feel the difference of ++x & x++ (--x & x--)
for example by the following lines:

C++
{
  // one result
  int i = 5;
  do {
    // after-call decrement
    printf("%d\n", i--);
  } while(i);
  // another result
  printf("\n");
  i = 5;
  do {
    // pre-call decrement
    printf("%d\n", --i); 
  } while(i);
}
 
Share this answer
 
v2
To add a bit of drama to the answer, look at these lines of code.

int ID = 1000;
int IX = 1000;
int GetNextID() {return ID++; }
int GetNextIX() {return ++IX; }


The first one returns 1000 and the second 1001. But if you check, both ID and IX will be 1001 ! Thats because, for ID, increment is done after the return while it is done before for IX.

You may ask how the heck can a code execute after return but know that return translates into a couple or more lines of assembly code. When debugging, watch these variables, step into each line in dis-assembly window and observe.

I hope now you'll never forget how these work. Ever.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900