开发者

loop's counter i as I++ vs. i+1 as position in an array

I've made a loop with i as it's counter variable.

Inside that loop I'm comparing cells of an array.

I'm wonderi开发者_开发百科ng what's the difference between array[i++] (or array[++i]) to array[i+1].

Since i++ (or ++i) don't work in the wanted way while i+1 does (a little bag which drove me crazy).

Thanks in advance.


  • array[i++] will evaluate array[i] and increment i.
  • array[++i] will increment i and then evaluate array[i] (so it gives you the next element)
  • array[i+1] will give you the next element without incrementing i

Personally I try to avoid using side-effects like this - it means when I read the code later, I always have to slow down to make sure I get everything in the right order.

If you're already in a loop which is incrementing i, then incrementing i in the array access expression as well would mean that each loop iteration would increment i twice.


i++, ++i, and i+1 are all different expressions (or operations). You should consult your favourite Java reference to learn about the difference.

(In short: i++ increments i but returns the original value, ++i increments i and returns the new value, i+1 does not increment i but returns the value of i+1.)

As to why exactly your loop does not work the way you expect it to: this can not be answered without actually seeing your code—quite logical. My assumption would be that you either used the expressions wrong and/or that you incremented i twice per loop.


array[i++] will give you the same as array[i] and then increment i. array[++i] will give you the same as array[i+1] and increment i. array[i+1] won't actually change the value of i.


  • i++: increase the value stored in i by one, and return the old value.
  • ++i: increase the value stored in i by one, and return the new value.
  • i+1: return the sum of i and 1, without changing the value stored in i

Now consider what happens if, as I suspect, you have code that looks like

for ( i = 0; i < something; i++ )
{
   dosomething(i++);
}

the values of i passed to dosomething() will be 0 2 4 8 . .

since every iteration of the loop, i is incremented once in the for() line and once in the dosomething() line, and the dosomething() call is given the value i had before it was incremented. If the behaviour actually desired is for dosomething() to be called with the sequence 1 2 3 4 . . then you need to avoid updating i with the result of adding 1 to it in the loop body: for ( i = 0; i < something; i++ ) { dosomething(i+1); }

or even

for ( i = 1; i < (something+1); i++ )
{
   dosomething(i);
}


array[i++] means "take the array element with index i, then increment i". array[i+1] means "take the array element with index i+1, do not modify i".


Not working properly because you are incrementing i twice in each loop.

So if your array is {1,2,3,4,5,6,7} and you printed array[++i] starting at i=0, it will print "2,4,6," then throw an ArrayOutOfBoundsExcpetion, if the array is {1,2,3,4,5,6,7,8} it will print "2,4,6,8"

Better be away from ++ on the loop counter, except if you really mean it.


Let's define i=0, then

  • i++ will return 0 and afterwards increment i to 1
    (post increment: returns value, afterwards increase)
  • ++i will increment i to 1 and afterwards return 1
    (pre increment: increases, afterwards return value)
  • ì+1 will not increment i and return 1
    (addition: only return the result, doesn't touch the variable)

I assume, you don't want to increment i by itself, only add 1 to access an index with a different offset.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜