-2

In some code i've seen a for loop with a --i as third parameters

for(int i=array.length; i<0; --i)

Maybe someone can explain me the difference with i-- ?

i guess it's something like the moment when i is decremented ?

Pigueiras
  • 18,778
  • 10
  • 64
  • 87
jbduzan
  • 1,106
  • 1
  • 14
  • 30
  • 1
    There is no difference in your case. – Pigueiras May 27 '13 at 15:55
  • Let me google that for you: "What's the difference between ++i and i++" – oseiskar May 27 '13 at 15:56
  • 1
    `--i` is pre-decrement and `i--` is post-decrement. Goolge/Bing these keywords and you will see ton of articles explaining the differences, but as @Pigueiras said, your example has no difference. – Bill May 27 '13 at 15:56
  • [java for loop pre-increment vs post-increment](http://stackoverflow.com/questions/2623718/java-for-loop-pre-increment-vs-post-increment) covers this well also – Sean Connolly May 27 '13 at 15:58
  • also duplicate of [what is the difference between i++ & ++i in for loop (Java)?](https://stackoverflow.com/q/2315705/995714) – phuclv Aug 14 '18 at 05:01

1 Answers1

15

If, for example, i = 5:

--i decrements i by 1 then gives you the value of i (4).

i-- gives you the value of i (5) then decrements it by 1.

Both will give you the same result in a for loop.

Francis.Beauchamp
  • 1,323
  • 15
  • 28