2

Does i in for loops means iteration or index?

How could I know the original meaning? Some programmers say it's iteration, some say it's index.

It seems to me more of an index we start from --- we go from zero, to the first in length (which is one, of course), and so forth.

But this still doesn't "prove" to me which from both terms is correct, if at all.

  • See Loop Counter under For-Loop, as affirmation of @RobertHarvey's answer; see also Loop Control Variable – Erik Eidt Oct 30 '17 at 23:07
  • Wow. And I thought that it was for integer. – Manoj R Oct 31 '17 at 10:57
  • Depending on the loop it can be iteration, index, both or neither. – Stop harming Monica Oct 31 '17 at 11:15
  • If I'm counting number of connection attempts, I start with 1. If I'm looping an array, I start with 0. The loop variable has different meanings according to its context. It would be incorrect to call it an index I'd think. – Neil Oct 31 '17 at 14:00
  • Usually double- and triple-nested for loops use j and k respectively, so to me, they have always looked from the beginning like the i, j, k components of things like a vector in physics and math. – Panzercrisis Oct 31 '17 at 15:16
15

It doesn't mean either.

It is a variable containing a number that can be used as an index value for an array item. This is evidenced by the fact that i is often used in the context of an array, as in

array[i]

The index value represents the number of the currently executing iteration. More specifically, the body of the loop (the instructions that are executed during each loop repetition) can be said to be iterated as the loop executes. If the item's index value is six, and an array is being iterated, then it's the seventh iteration (since array indices typically start counting at zero).

That said, there's no requirement that i be used as an index in an array; it might be used in some other context. The notion of loop variables named i, j, k, etc. originates in mathematics, where it is an established convention for writing subscripts. It was carried over from mathematics to programming in Fortran, where variables named i through n default to the integer data type.

  • 7
    And it comes from common mathematics notation, specifically, subscripts. – Frank Hileman Oct 30 '17 at 22:17
  • So it is a variable containing the index of the current item (value) in a collection (say, array), and this current item reflects the current iteration. – Arcticooling Oct 30 '17 at 23:03
  • Not quite. It is a variable containing a number that can be used as an index in an array. If its value is six, and an array is being iterated, then it's the seventh iteration (since array indices typically start counting at zero). But there's no requirement that i be used as an index in an array; it might be used in some other context. – Robert Harvey Oct 30 '17 at 23:49
  • 6
    Since j and k don't stand for "index" or whatever, IMO, the truly correct answer is that i stands for i. :-) – user949300 Oct 31 '17 at 4:24
  • 1
    No, I don't mean that. I mean that i, j,k,l,m, and n all default to integer in Fortran. – Robert Harvey Oct 31 '17 at 14:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.