views:

2777

answers:

16

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

+1  A: 

i = iterator.

azollman
I would say, i == index.
Frederik Gheysels
thats ok I would say i === index so, we'll just agree to disagree.
Unkwntech
+108  A: 

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.

tvanfosson
I through N in F77, but close enough. Also I think this is more likely the reason rather than i == iterator or i == index
Kamil Kisiel
Yep, though N. I'll correct this.
tvanfosson
I'm pretty sure it was FORTRAN.
Cade Roux
I'm also voting FORTRAN.
bmb
Ya, fortran..... It rots the mind.We have a programmer that usesiiiand iii as loop variable names.The other symptom is 6 character variable/function names with no vowels.
EvilTeach
@tvan, I'm pretty sure variables starting with I through N **defaulted** to integer, but you could still declare them real hence the joke "God is real, unless declared integer". +1 anyway, since my recollection from so many years ago may possibly NOT be perfect.
paxdiablo
Hey, the FORTRAN guys got it off the mathematicians!
timday
I love historical artifacts like this. It adds depth to programming, and makes me feel connected to the past.
RedFilter
@Pax -- declarations didn't come along until F66 (http://en.wikipedia.org/wiki/Fortran#FORTRAN).
tvanfosson
@Pax - your memory is correct, though as @tvanfosson says only for the later versions of FORTRAN.
ChrisF
Well there you go, I assummed it was an abreivation for index
Harry
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
+50  A: 

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.

Michael Borgwardt
Good answer. This (to me) indeed seems like the most probable explanation.
Noldorin
Yup. The mathematic notation for a sum like `Y = Σ Xi` precedes every programming language.
Treb
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...
awe
+7  A: 

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.

Ed Swangren
+5  A: 

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.

John T
+14  A: 

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

brett
FWIW, I usually skip the letter l because it looks too much like the number 1.
Nosredna
+2  A: 

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.

rjamestaylor
+154  A: 

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.

timday
To me, this is clearly the best answer. (Honorable mention for Michael Borgwardt's answer, which also cites mathematical convention but isn't as specific.) I'm sorry yours isn't the accepted one. All I can do is give it my upvote.
John Y
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).
Lucas McCoy
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 :^)
timday
..OK the 1816 link doesn't go to page with "i" used on it, but they're there.
timday
A: 

Because int starts with an i :)

Geo
+6  A: 

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.

Chris Pietschmann
+1  A: 

Unless you need to know the specific index in an array... Might aswell use a foreach construct;

foreach $item (@list) { do($item); }

lol perl.

Ape-inago
+2  A: 

We're not. At least not if we're using any sort of decent naming convention.

Jon Hopkins
to me, a decent naming convention is something everyone looks at and immediately recognises / is immediately obvious..i would certainly recognise it quicker as an index variable if i saw i over index, as the single letter variable name is more expressive than any name would be.maybe that's just me though.
tim
@tim - it's obvious that it's an index but that's obvious from the fact it's in a for next or while wend loop. what it doesn't tell you is what it refers to - is it the client invoice number? the internal system invoice id? the purchase order id for the PO relating to the invoice? or if you've got a loop within a loop which one is the rows and which one the columns? for the sake of an extra few characters why not have complete clarity?
Jon Hopkins
+2  A: 

When I 'learned' BASIC on the BBC and ZX Spectrum the convention was to use 'n'. When I moved to more curly bracket languages the convention shifted to 'i'.

Adrian
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. http://www.bitsavers.org/pdf/dartmouth/BASIC_Oct64.pdf
Nosredna
+1  A: 

i = index I think, at least that's how I look at it

JonoW
+2  A: 

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.

ilya n.
A: 

Actually, since I had to do a lot of coding in Matlab, where i is predefined as the unit imaginary number, I have switched to k as the default loop counter... =)

Frank Hirsch