Header
TOPIC DETAILS
AUTHOR

Difference Between I++ And ++i?

I Am Unable To Understand The Difference Between I++ And ++i In For Loops..can U Please Help Me With That With An Example?

Karthikeya R

>> For standard for and while loops, it is the same. Until and unless you are using the return value of the increment they are same. In a for loop e.g. for(i=0; i<5 ; i++){} in the first iteration i=0 is executed then i<5 is checked then the body(refers to the part within the {} braces) is executed(if and only if i<5 returns true and this condition is checked for the final value i), in the 2nd iteration i<5 is checked then body is executed then i++. So, i is incremented after all the statements in the body are executed. Since we are not using the return value of i anywhere, there is no difference between i++ and ++i here. The difference between i++ and ++i is manifested when another expression uses the return value from the increment operation. In other words, if we consider the following code snippet: int i=0, j=0; for(i=0;j<5;j=i++) printf("%d",j); the output is :001234 But if the code is : int i=0, j=0; for(i=0;j<5;j=++i) printf("%d",j); the output is: 01234