Is there a difference between i++ and ++i?
Hi, i know that i++ is equivalent to i+1. What about ++i? Does it mean the same thing? Can I use both interchangeably in my Java program?Hope somebody can help clarify my doubts, thanks!
[200 byte] By [
apsurfera] at [2007-11-26 12:59:35]

i++: means, you increment the value of i by 1 just after your loop statement execution++i: means you increment the value of i by 1 before your loop statement execution..now, it's up to you to decide on when to use the former and the latter..
Both means i=i+1, but difference is thereif u use k=i++ and i value 10 then first i value(10) assigned to k and then i value incremented to 11if u use k=++i , first i value incremented to 11 and then it is assinged to k(11)it has the effect how u'r using in u'r
> Does it mean the same thing? Can I use both
> interchangeably in my Java program?
No , you cannot use them interchangeably , both are different. I think the following code will help you.
public class Check {
public static void main(String[] args) {
(new Check()).check();
}
private void check() {
int i = 0;
// Here the value of i is assigned to temp, then the value of i is
// incremented by 1
int temp = i++;
System.out.println(temp + " " + i);
i = 0;
// Here the value of i is incremented by 1, then the value of i is
// assigned to temp
temp = ++i;
System.out.println(temp + " " + i);
}
}
Good luck!!