Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlexandru Georgian Chiurtu
7,008 Pointsdifference between ++i and i++
What is the difference between ++i and i++ ?
2 Answers
bothxp
16,510 PointsHi
i++ is a postfix, it will first use (or return) the original value of i and then increment the value of i afterwards. In this example j will be set to the starting value of i, and then i will increment by 1.
> i = 10;
10
> j = i++;
10
> i
11
> j
10
++i is a prefix, it will increment the value of i first , and then use (or return) the incremented value. In this example i will first increment by 1 and then j will be set equal to this new value of i.
> i = 10;
10
> j = ++i;
11
> i
11
> j
11
SZE SZE XU
9,759 PointsI am a bit confused by the first example, wont i++ assign the value to j ? so the idea is j = i++, (j = i at this point), and then increment i, so i + 1 while nothing changes to j.