1

In all the programming languages I have come across there seems to be the best practice to use variable i in for loop iterations. Usually i is followed by l in the nested loop. This seem to apply both for statically compiled and scripting languages.

What is the history of this practice? Does i mark for integer, index, or something else? Why, for example, we don't use x which would be more common, considering math background.

Mikko Ohtamaa
  • 76,495
  • 46
  • 227
  • 378
  • 1
    I've always thought i is short for index. But I've never really seen i dominate; actually I've seen "pos" or "x" far more commonly than "i". – christopher Feb 25 '13 at 22:59
  • 1
    Even in maths "i" is used as the preferred dummy index. – Kerrek SB Feb 25 '13 at 22:59
  • 1
    In mathematics it is more common to use `i` for an integer variable (and in particular for an index) than it is to use `x`. Loop variables are often (but not always) indexes. `x` is often used as an integer in Diophantine equations, but almost never as an index. – Steve Jessop Feb 25 '13 at 22:59
  • 2
    See this [similar question](http://stackoverflow.com/questions/4137785/why-are-variables-i-and-j-used-for-counters) and [this one](http://stackoverflow.com/questions/454303/why-are-we-using-i-as-a-counter-in-loops) – br3w5 Feb 25 '13 at 22:59
  • FORTRAN started it, IMHO. i,j,k were int variables by default. – wildplasser Feb 25 '13 at 23:01
  • @ssbrewster Thanks... naturally it did not cross my mind that series use i in maths – Mikko Ohtamaa Feb 25 '13 at 23:02
  • and `for` loop first appeared in Algol 60. – ouah Feb 25 '13 at 23:02
  • I didn't know that `for` was introduced in Algol 60. That syntax was quite wicked: http://courses.cs.vt.edu/~cs3304/Spring00/notes/Chapter-7/tsld024.htm – piokuc Feb 25 '13 at 23:12
  • 2
    @wildplasser: Fortran was just copying existing mathematical usage where i,j,k are standard indices. – R.. GitHub STOP HELPING ICE Feb 25 '13 at 23:15

2 Answers2

0

I've got two theories: i can stand for 1) index 2) integer (value of integer type)

piokuc
  • 24,264
  • 10
  • 66
  • 98
0

It makes most sense that i stands for index because the loop is over each element of an array and each element is indexed.

br3w5
  • 4,060
  • 3
  • 32
  • 42