Who "invented" i,j,k as integer counter variable names? - Stack Overflow
i = integer
Comes from Fortran where integer variables had to start with the letters I through N and
real variables started with the other letters. Thus I was the first and shortest integer
variable name. Fortran was one of the earliest programming languages in widespread use
and the habits developed by programmers using it carried over to other languages.
(From: Why are we using i as a counter in loops)
Obviously, j and k are just the next ones in your favorite alphabet.
Maybe it was a later addition, but in FORTRAN-G at least, variables starting with I-N
where INTEGER unless declared otherwise. It was perfectly legal to say "REAL
IVAR" or "INTEGER A".
The Mathematicians :)
It's common from school-level and college-level algebra exercises (although x and y
had their part to play, there, too :-)
Also, if I remember correctly, the early programming languages (like early versions of
FORTRAN) used variable naming in a way where initial letters were significant, and
this may have had a part to play. For example, as this page says:
A FORTRAN variable is a way of referring to a cell of the computer. Names for
variables must conform to the following rules:
1. The name may be from one to six characters.
2. The first character must be a letter.
3. Characters other than the first may be letters or numeric digits.
4. If the first character is I, J, K, L, M or N, the variable is integer (i.e. can
hold a whole number value). Otherwise, it is real (i.e. can hold a value according
to the floating point convention).
FORTRAN. If the first character is I, J, K, L, M or N, the variable is integer (i.e.
can hold a whole number value). Otherwise, it is real (i.e. can hold a value according
to the floating point convention).
From the wikipedia for Loop Counter
A common identifier naming convention is for the loop counter to use the variable
names i, j and k (and so on if needed), where i would be the most outer loop, j the
next inner loop, etc. The reverse order is also used by some programmers. This style
is generally agreed to have originated from the early programming of FORTRAN, where
these variable names beginning with these letters were implicitly declared as having
an integer type, and so were obvious choices for loop counters that were only
temporarily required. The practice also dates back further to mathematical notation
where indices for sums and multiplications are often i, j, etc.
I always thought i stands for index as used eg in sum formulas in mathematics.
Why are we using i as a counter in loops - Stack Overflow
why are we using
for (int i = 0 ; i < count ; i++){ }
why the i
why not
for (int a = 0; a < count; a++){ }
I do it, you do it, everyone does it but WHY?
*edit
I found out an old saying about FORTRAN which is more funny than correct which says
"god is real, everything else above is an integer".
"god" would be a variable name stating with a g so it would be in the real
domain, while everything else above (excluding h for the joke's purpose) would be an
integer
It seems that the original saying was in fact : "God is real, unless declared
integer". Apologies to everyone citing me in their phd thesis
i = integer
Comes from Fortran where integer variables had to start with the letters I through N
and real variables started with the other letters. Thus I was the first and shortest
integer variable name. Fortran was one of the earliest programming languages in
widespread use and the habits developed by programmers using it carried over to other
languages.
EDIT: I have no problem with the answer that it derives from mathematics. Undoubtedly
that is where the Fortran designers got their inspiration. The fact is, for me anyway,
when I started to program in Fortran we used I, J, K, ... for loop counters because
they were short and the first legally allowed variable names for integers. As a
sophomore in H.S. I had probably heard of Descartes (and a very few others), but made
very little connection to mathematics when programming. In fact, the first course I
took was called "Fortran for Business" and was taught not by the math
faculty, but the business/econ faculty.
For me, at least, the naming of variables had little to do with mathematics, but
everything due to the habits I picked up writing Fortran code that I carried into
other languages.
I'm pretty sure it was FORTRAN.
Ya, fortran..... It rots the mind. We have a programmer that uses i ii and iii as loop
variable names. The other symptom is 6 character variable/function names with no
vowels.
Hey, the FORTRAN guys got it off the mathematicians! – timday Jan 18 '09 at 0:10
I love historical artifacts like this. It adds depth to programming, and makes me feel
connected to the past. – OrbMan Jan 18 '09 at 1:48
I agree with @timday. Using i as an index of a series has been a practice by
mathematicians for at least 2 centuries. – Scottie T Jun 2 at 13:06
Mathematicians were using i,j,k to designate integers in algebra (subscripts, series,
summations etc) long before (e.g 1836 or 1816) computers were around (this is the
origin of the FORTRAN variable type defaults). The habit of using letters from the end
of the alphabet (...,x,y,z) for unknown variables and from the beginning (a,b,c...)
for constants is generally attributed to Rene Descartes, (see also here) so I assume
i,j,k...n (in the middle of the alphabet) for integers is likely due to him too.
This is exactly the reason that the we need to be able to vote for a community
accepted answer (and yeah I know this belongs on uservoice).
Heh. I occasionally update the link to an old google book in this answer because
google keeps chopping stuff around. By chance, I note the 1816 document now linked
includes Charles Babbage (presumably he of difference engine/analytical engine fame)
as an author. So arguably it's an example of the first use of an integer variable
"i" by a computer programmer :^)
I think it's most likely derived from index (in the mathematical sense) - it's used
commonly as an index in sums or other set-based operations, and most likely has been
used that way since before there were programming languages.
Yup. The mathematic notation for a sum like Y = Σ Xi precedes every programming
language.
I am thinking of index each time I use i. But originally I use it mainly because all
code I look at elsewhere use it, and that may be inherited from Fortran programmers...
Possibly historical ?
FORTRAN, aurguably the first high level language, defined i,j,k,l,m as Integer
datatypes by default, and loops could only be controlled by integer variable, the
convention continues ?
eg:
do 100 i= j,100,5 .... 100 continue ....
I use it for a number of reasons.
* Usually my loops are int based, so you make a complete triangle on the keyboard typing
"int i" with the exception of the space I handle with my thumb. This is a very fast
sequence to type.
* The "i" could stand for iterator, integer, increment, or index, each of which
makes logical sense.
With my personal uses set aside, the theory of it being derived from FORTRAN is correct,
where integer vars used letters I - N.
It's funny how everyone has a different take on this. I always assumed that it stood
for 'index'. The fact that everyone immediately realizes what it means is enough for
me.
i = iterator, i = index, i = integer
Which ever you figure "i" stands for it still "fits the bill".
Also, unless you have only a single line of code within that loop, you should probably
be naming the iterator/index/integer variable to something more meaningful. Like:
employeeIndex
BTW, I usually use "i" in my simple iterator loops; unless of course it
contains multiple lines of code.
Most probably we are using i because we learn to program from examples.
Early examples of for(;;){} loops in C seem to use i as the iteration variable.
Checking around the Internet shows this to remain true (see
http://en.wikipedia.org/wiki/For_loop).
Once we learn a pattern (i, j, k for nested loops) they become habits and moreover
become accepted, even expected, norms for collaboration.
This doesn't make it right or optimal to continue, of course.
The original BASIC manual from 1964 used X. From the late 1970s and early 1980s, I
remember a lot of I, J, X, Y, M, N. Every once in awhile you saw an A, A0, AA, but not
very often. bitsavers.org/pdf/dartmouth/…
I'm quite sure that i comes from index (I'm a mathematician, after all).
Actually when I was high-schooler, my loop variables were usually called idx, jdx and kdx.