4

I've made a loop with i as it's counter variable.
Inside that loop I'm comparing cells of an array.
I'm wondering 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.

Joe
  • 41,484
  • 20
  • 104
  • 125

7 Answers7

15
  • 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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
7

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.

Bombe
  • 81,643
  • 20
  • 123
  • 127
  • That I know, but why does ++i (a copy of the increment of i by one) doesn't work as i+1 the value of adding one to i? –  Dec 15 '09 at 12:39
  • 2
    That still depends on your code. Show it and we can tell you what’s wrong with it. – Bombe Dec 15 '09 at 12:40
  • 1
    @baruch1mekonen: because when using `++i`, `i` gets incremented by one and it's *probably* also being incremented a second time in the loop. So next time you use it, it's incremented by 2 instead of one. `i+1` results in the same value but does not increment (the content of `i`). – user85421 Dec 15 '09 at 17:22
2

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.

DMI
  • 6,843
  • 2
  • 24
  • 25
1
  • 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);
}
moonshadow
  • 86,889
  • 7
  • 82
  • 122
0

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".

Konamiman
  • 49,681
  • 17
  • 108
  • 138
0

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.

mohdajami
  • 9,604
  • 3
  • 32
  • 53
0

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.

Hardcoded
  • 6,476
  • 2
  • 22
  • 20