135

Has anyone thought about why so many of us repeat this same pattern using the same variable names?

for (int i = 0; i < foo; i++) {
    // ...
}

It seems most code I've ever looked at uses i, j, k and so on as iteration variables.

I suppose I picked that up from somewhere, but I wonder why this is so prevalent in software development. Is it something we all picked up from C or something like that?

Just an itch I've had for a while in the back of my head.

Deduplicator
  • 9,209
kprobst
  • 641

10 Answers10

217

i and j have typically been used as subscripts in quite a bit of math for quite some time (e.g., even in papers that predate higher-level languages, you frequently see things like "Xi,j", especially in things like a summation).

When they designed Fortran, they (apparently) decided to allow the same, so all variables starting with "I" through "N" default to integer, and all others to real (floating point). For those who've missed it, this is the source of the old joke "God is real (unless declared integer)".

Most people seem to have seen little reason to change that. It's widely known and understood, and quite succinct. Every once in a while you see something written by some psychotic who thinks there's a real advantage to something like:

for (int outer_index_variable=0; outer_index_variable < 10; outer_index_variable++)
    for (int inner_index_variable=0; inner_index_variable < 10; inner_index_variable++)
        x[outer_index_variable][inner_index_variable] = 0;

Thankfully this is pretty rare though, and most style guides now point out that while long, descriptive variable names can be useful, you don't always need them, especially for something like this where the variable's scope is only a line or two of code.

Deduplicator
  • 9,209
Jerry Coffin
  • 44,795
67

i - index.

A temporary variable that is used for indexing loop iteration. If you already have an i, what's more natural than going to j, k, and so forth?

In that context idx is often used as well.

Short, simple, and shows exactly what the usage of the variable is, as the variable name should be.

oh whatever
  • 4,734
40

In FORTRAN i was the first letter to default to the integer type. The full range of implicit integer types were any starting with the letters i to n. Working with loops for three dimensions got us i, j, and k. The pattern continued for additional dimensions. Likewise, 'x', 'y', and 'z' which defaulted as floating point where used for loop variables which required real numbers.

As litleadv notes, i is also a minimal contraction for index.

Given the local use of these variables, I don't see a problem not using a more descriptive name.

ChrisF
  • 38,948
  • 11
  • 127
  • 168
BillThor
  • 6,310
34

I believe it comes from the Summation:

Summation

Raphael
  • 2,234
8

I was told, in an aside by my theory of computation professor a couple of years ago, that the origin was from mathematical summation notation. i,j,k were commonly used as integer iteration variables, and it transferred over from the early texts on computation theory (which of course is primarily applied mathematics). Apparently looking at the work of Knuth et al. backs this up.

x,y,z as floating point iterants apparently came in with the popularity of Fortran in the physical sciences, from physics researchers converting integrals to source code (since integrations were commonly over spatial bounds ... as x goes to 100 et cetera)

I don't have a reference to back this up, but as I say, it came from a fairly old professor and seems to make a lot of sense.

Hope this helps

Stephen
  • 2,141
  • 1
  • 14
  • 24
7

I think i, j, k are used in Mathematics to indicate indices in summations and for matrix elements (i for the rows and j for the columns). In mathematical notation, some letters tend to be used with a specific meaning more often than others, e.g. a, b, c for constants, f, g, h for functions, x, y, z for variables or coordinates, i, j, k for indices. At least this is what I recall from the Math I took at school.

Maybe this notation has been transferred to computing because at the beginning many computer scientists were mathematicians or at least had a strong mathematical background.

Giorgio
  • 19,764
3

i == iterator or index

My understanding has always been 'i' is a good choice since loops are typically for iterating or traversing with an known index step. The letters 'j' and 'k' follow well since nested loops are often needed and most will instinctively know 'j' == second index and 'k' == third index since they follow that pattern in the alphabet. Additionally, with many following this standard convention, one can safely assume a 'k' loop will likely have a 'j' and 'i' loops wrapping it.

jimp
  • 291
  • 2
  • 7
1

i, j, k are used in Cartesian coordinate system. Comparing with x and y which represent axises in the same system and which are always used as variables, we may say that i,j and k also came from there.

Sergey
  • 2,713
1

"i" is for index.

A single letter makes it easier for your eye's to scan the line of code. For something as common as loop, a single character for the index is ideal. There is no confusion as to what "i" means due to the convention.

jojo
  • 598
0

When I started programming, Dartmouth BASIC was new. All variables in this and some other languages at the time were single characters. Add to that the FORTRAN variable naming convention and the fact that it is much easier to type, only one character, and the understanding from older programmers and teachers. I don't know, but it sounds good.

Dave
  • 427