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.

Mael
  • 2,305
  • 1
  • 13
  • 26
Arcticooling
  • 1
  • 2
  • 11
  • See [Loop Counter under For-Loop](https://en.wikipedia.org/wiki/For_loop#Loop_counters), as affirmation of @RobertHarvey's answer; see also [Loop Control Variable](https://en.wikipedia.org/wiki/Control_variable_(programming)) – 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

1 Answers1

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.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • 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 04:24
  • Robert, in this saying `It was carried over from mathematics to programming in the Fortran language, where variables named `i` through `n` default to the `integer` data type.`, did you mean that in a certain time, `i` replaced `n` in Fortran? – Arcticooling Oct 31 '17 at 13:43
  • 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
  • After 25+ years of programming C and then C++ in the predominantly Electrical Engineering and mathematical/statistical domains.. I have abandoned the notation of i, j, k, as "index" or iterator counter notation... Since in C/C++ indexing starts at ZERO (as it should be) and ID's in database's almost always start at ONE.. i always use ix, jx, kx, etc for "indeXes" that start counting at zero.. and id, jd, kd or "row_ix" or "row_id" for looping variables..because in math, i, j, and k always refer to Current, Imaginary numbers, or some other Vector component of some math construct. – Heston T. Holtmann Nov 03 '17 at 04:28