# Thread: confusion with increment and decrement operators

1. ## confusion with increment and decrement operators

hi..everyone.i got a little bit confusion about prefix form
for some book i learn that; for example
Code:
`for (i=0; i < 5; ++i)`
and some told me write in this form
Code:
`for (i=0; i<5; i++)`
and i know that the output is same but doesn't it
++i will increment the value first and if i++ the value will increment after the loop.I really doesn't understand that why the for loop come out with the same answer??...

2. hi,
the difference between i++ and ++i is that if the i++ (or ++i) is used in an expression, i++ will cause i to be used in the expression then incremented, wheras ++i will increment i and then use it in the expression. As i isn't being used in an expression in your code, there is no difference between i++ and ++i
If you want to have i going from 1 to 5 instead of 0 to 4, simply use the following code instead:
Code:
`for (i=1;i<=5;i++)`
Hope this helps,
Taran

3. It will be the same because u only use it for loop counter.

Try this:
Code:
```#include <stdio.h>

int main()
{
int i = 0;
int j = 0;
printf("%d\n", i++);//result 0
printf("%d\n", i++);//result 1
printf("%d\n", ++j);//result 1
printf("%d\n", ++j);//result 2
return 0;
}```
Remember, the difference between the two forms is what value (either the old or the new) is passed on to the surrounding expression. If there is no surrounding expression, if the ++i or i++ appears all by itself, to increment i and do nothing else, you can use either form; it makes no difference

4. Originally Posted by cBegginer
and i know that the output is same but doesn't it ++i will increment the value first and if i++ the value will increment after the loop.I really doesn't understand that why the for loop come out with the same answer??...
The first form,
Code:
`for (i=0; i < 5; ++i)`
has an equivalent:
Code:
```   i = 0;
while(i < 5)
{
/* do stuff */
++i;
}```
The second form,
Code:
`for (i=0; i < 5; i++)`
has an equivalent:
Code:
```   i = 0;
while(i < 5)
{
/* do stuff */
i++;
}```
Why would you expect the results to differ?

5. Ic..ok thanks for helping me.I appreciate it alot.

6. ++i and i++ do not really give you the same result everytime.

++i in this case is called the pre-increment operator. This will first add 1 to i and give you the new value of i.

i++ in this case is called the post -decrement operator, because im lacking words to express this i will do it by example.
Code:
` x =i++;`
here x is assigned the original value of i first and then i is increased by 1.

Example from the book that im reading.
Code:
```#include <stdio.h>
main()
{
int w,x,y,z, result;

w = x = y = z = 1; /*initialize x and y*/
printf("Given w =%d, x =%d, y=%d, and z =%d,\n", w, x,y,z);

result = ++w;
printf("++w evaluates to %d and w is now %d\n", result, w);

result = x++;
printf("x++ evaluates to %d and x is now %d\n", result, x);

result = --y;
printf("--y evaluates to %d and y is now %d\n", result, y);

result = z--;
printf("z-- evaluates to %d and z is now %d\n", result, z);
return 0;
}```

7. When used alone, ++foo and foo++ give the exact same results:
Code:
```int foo = 0;

foo++;
printf( "%", foo );```
Is exactly the same as:
Code:
```int foo = 0;

++foo;
printf( "%", foo );```
In a single expression, the results are the same. The difference is when you use them for something other than a single expression. Like so:
Code:
```#include<stdio.h>
void foo( int bar )
{
printf("bar is %d\n", bar );
}

int main( void )
{
int baz;

baz = 0;
foo( baz++ );

baz = 0;
foo( ++baz );

return 0;
}```
Quzah.