5

Possible Duplicate:
Why do most of us use 'i' as a loop counter variable?

Maybe this questions seems to be extreamly stupid but I wonder why we use i as variable in most cases in for loops (and not other letter). This might be a historical cause.

eg.

for(int i = 0; i < 10; i++)
gregory561
  • 151
  • 2

4 Answers4

18

Long ago there was a programming language called FORTRAN, indeed the first so called higher programming language. In FORTRAN variables names beginning with i,j,k,l,m,n were implicitly declared of type INTEGER. So i can stand for:

  • integer
  • index (of an array, like in a[i])

or other things. Short names are typed quickly, so i is a good choice for a loop counter.

The FORTRAN convention goes deeper. Mathematicians love to name integer variables i,j,k,l,m, or n. They often use a,b,c,d,e,f for constant values and x,y,z for variables.

Eric Wilson
  • 12,071
  • 9
  • 50
  • 77
René Richter
  • 391
  • 2
  • 8
4

In FORTRAN IV, the variable names I-N were implicitly assumed to be integers. I think it's part of FORTRAN's legacy as a scientific computing language. Linear algebra uses integer indexes to derefence values in vectors and matricies. The convention diffused into other languages.

duffymo
  • 3,032
  • 16
  • 23
0

I guess the i stands for index.

Long ago, when computers were not that fast and memory and storage space was expensive, it was important to keep your source code small, so that it would fit in memory and the compilation process would run faster. Those things are not true anymore with today's computers, but still the i as the first index variable name stuck.

Jesper
  • 2,559
  • 20
  • 16
0

For what it's worth, I never use i. Perhaps this is a byproduct of using simpler text-editors, rather than IDEs, but I find it makes it very difficult to scan through a routine looking for places where the variable is used. To see what I mean, try searching for all the places i exists on this page, and seeing which ones refer to it as a variable, and which are incidental.

I always use more meaningful names, like

for(int Index = 0; Index < 10; Index++)
{
    // do something
}
Aidan Cully
  • 3,476
  • 1
  • 19
  • 23