Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Why we use i, j, and k in for loops (ablegray.com)
16 points by nickb on Dec 17, 2007 | hide | past | favorite | 17 comments


Surely Fortran merely copied existing math conventions.


That was exactly what I was thinking. n is used for naturals, i, j and k are used for variable values in sums, for example (which, when programmed, would require the use of "for" loops).


Does i stand for 'integer' or possibly 'index'? I also wonder how far into the past it goes, 19 century?


Like so many other things in mathematics, I suspect it goes back to Euler (18th century). He invented the capital-sigma notation for sums, although I'm not sure what letters he used for his indices.

The use of x, y and z as unknown reals goes far as back as Descartes. I don't know what he used for integers though, or whether he bothered to distinguish integers from reals.


Maybe it stands for "iterator"... who knows.


Clearly it is in deference to D i j k stra. ;-)


Yeah... he had such an awesome name for who he turned out to be.


Errr... i, j, k are often used in mathematical proofs in summations and things like that... are we sure that's not why Fortran used them in the first place?

Which came first, the chicken or the egg? Math or Fortran?


I learned to program on the ZX Spectrum (Awesome home computer). The spectrum had a great(ish) keyboard, where each key also had about 3 or 4 BASIC shortcuts on it, which you could get with various shift keys. Also it would recognize when it was expecting a statement, and just use the statement for that key instead of the letter.

For example, to type "for f", you would press the 'f' key twice, as it had the shortcut 'For' on it, and also was the letter f. Similarly, to type "next n", you'd hit the 'n' key twice.

Perhaps why you sometimes see f and n in for loops.


Wow! I did a global search for the variables "i", "j", and "k" in this 2000 line program I have to enhance, and I got all of them and a whole lot more! What a great idea!


I always thought it was because programmers were too lazy to write a couple of extra characters to make a nice descriptive variable name. At least that's why I do.


> I always thought it was because programmers were too lazy to write a couple of extra characters to make a nice descriptive variable name.

Nah. A variable name's descriptiveness (and length) should be proportional to the size of the scope in which it's used. A simple loop index should almost never be longer than i, j, k, etc.


Here here. There's nothing quite like:

for i in $(exceptionally long pipeline that doesn't work right now)

Because someone didn't tell you what I was supposed to be.

Or being a few lines down the bottom of a very long loop, and trying to remember what 'i' represents this time round.


Sure, we are... But the question is why did we arbitrarily start incrementing from 'i' instead of 'a'?


int i; float f; char c;


 float    f;
 unsigned u;
 char     c;
 key_t    k;
That's how I swing, man.


Could it have something to do with the keyboard layout?

On standard keyboards "i" is just above "j & k". Also in basic typewriter tuition, your right hand apposing finger starts on the keyboard at the letter "j" when you commence typing. "k" has your right index finger. Most people being right handed these letters are pretty much the first & most intuitive letters to use.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: