Daniel Tian’s Professional Blog


Just another WordPress.com weblog

Programming Tip: i++ vs. ++i

It’s been a while since I made my last post in this blog. School kept me pretty busy, but now that this semester is over, I’m hoping to write more posts in this blog.

Recently I had a classmate ask me why some people used ++i instead of i++ in their for loops. For example (in Java):

for (int i = 0; i <= 100; ++i) for (int i = 0; i <= 100; i++) [/sourcecode] If you know the difference between these two lines of code, you already know what I'm about to say, but if you find yourself pausing for a moment and thinking, this post is for you. So what is the difference? Let's take a look at what they do first. i++ will increment the value of i by 1. But wait, doesn't ++i do the same thing? Yes it does, but the difference lies in how the i is used. Suppose we have these lines of code (in Java): [sourcecode language='java'] i = 0; System.out.println("Testing i++"); System.out.println(i++); System.out.println(i); System.out.println(); i = 0; System.out.println("Testing ++i"); System.out.println(++i); System.out.println(i); [/sourcecode] What will the outputs be? Here they are: [sourcecode language='java'] Testing i++ i = 0 i = 1 Testing ++i i = 1 i = 1 [/sourcecode] From this, you can probably already tell the difference between the two. i++ will use the value of i first and then increment it, whereas ++i will increment it first and then use the value. In the case of the example, i++ will print out the value of i first, and then increment it, whereas ++i will increment i, and then print out its value. This is especially important to keep in mind when incrementing i this way inside a for or while loop. So how is this important when incrementing i for a for loop? After all, i is always incremented before the loop iteration begins. Take for example: [sourcecode language='java'] System.out.println("Testing i++ for a for loop"); for (int i = 0; i <= 1; i++) { System.out.println("i = " + i); } System.out.println(); System.out.println("Testing ++i for a for loop"); for (int i = 0; i <= 1; ++i) { System.out.println("i = " + i); } [/sourcecode] The output will be: [sourcecode language='java'] Testing i++ for a for loop i = 0 i = 1 Testing ++i for a for loop i = 0 i = 1 [/sourcecode] In both cases, the output is the same. So is there an advantage of using one of the other? There is, and it turns out that the answer lies in how i is handled in memory. When either i++ or ++i is used, there are two operations being performed, one to use the value of i, and one to increment it. These two operations, in a sense, work simultaneously and can be considered one operation. When i++ is used, the old value of i must be kept, but it also needs to be incremented. This means that a separate i value must be created in memory and a pointer created to reference it. After it's used, the garbage collector must remove this temporary i reference. On the other hand, ++i does not need to create this new value because it increments i before it's used. There are no additional operations needed. Ultimately, this means that using ++i for a for loop is really just an optimization to avoid having to create a temporary i value + reference, then garbage collecting it afterwards. Nowadays though, with the advent of new compilers and faster computers, this isn't really an issue anymore. Still, it's one optimization that exceedingly easy to use, so as a best-practice it's better to use ++i rather than i++.


Filed under: Programming Tips, , , ,