# Why do most of us use 'i' as a loop counter variable?

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.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

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.

• +1 I hadn't thought about the math correlation here. This was an interesting realization. – Casey Patton Jun 25 '11 at 0:14
• +1 for attributing to psychosis what some imagine to be style. – Crashworks Jun 25 '11 at 2:21
• OK - so why are i and j used as subscripts in mathematics? I think this use predates FORTRAN :-) I'm sure Knuth knows... – Guy Sirton Jun 25 '11 at 3:38
• @Guy 'i' is for index, j is the next letter after i – Martin Beckett Jun 25 '11 at 4:02
• i and j are fine or even preferable in most cases, as Jerry alluded to. A bigger problem is loops that are so big you can't see and comprehend the whole thing on one screen. Those need to be refactored so that they're more concise. – Craig Feb 28 '15 at 8:30

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.

• ijk = "I'm just kidding". :-) – Mahmoud Hossam Jun 24 '11 at 23:59
• can you cite a reference? – Louis Rhys Jun 25 '11 at 9:46
• You can cite me as a reference if you want. – Captain Sensible Jun 26 '11 at 0:01
• Maybe worth adding that it's often literally an index (subscript) into an array that's accessed in the loop. Not always, but whether you view it as an index into the interations or not, it's still convenient to generalize from a common class of loop to a wider class of loop. BTW - personal freakiness - to me "index" tends to imply something like "database index", including key-to-subscript dictionaries/inverted tables in memory, so I prefer "subscript" rather than "index" to avoid the ambiguity. But I still use i for loops. – Steve314 Jul 22 '11 at 12:01
• In your train of thought i could mean iterator as well. – totymedli Apr 10 '13 at 8:07

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.

• For single loops I agree, but for loops containing loops I like a bit more description. – Crazy Eddie Jun 24 '11 at 23:54
• Ultimately the answer is inertia. I started doing it with WATFOR (Waterloo FORTRAN) in 1973 and ... I'm still doing it in 2011. – Peter Rowell Jun 25 '11 at 0:01
• @Crazy Eddie: If the loops contain a lot of functionality, I would agree. But with tightly nested loops, the length of the code should be on the order of n+1 or 2n+1 where n is the number of dimensions. In that case longer variable names may decrease readability. – BillThor Jun 25 '11 at 0:04
• +1 for "In Fortran" because that's how that tradition has started - it does not matter whether you like Fortran or not. – artem Jun 25 '11 at 2:26

I believe it comes from the Summation:

• Interesting! This predates FORTRAN, but in the context of CS? – kprobst Jun 25 '11 at 6:18
• Since Backus had a Masters in mathematics it seems logical. – dbasnett Jun 25 '11 at 14:13
• Most early CS geniuses were either into math or electrical engineering, which required extensive math. Even today, your average "computer nerd" typically has above average math skills, because math and computers are so logically oriented. – corsiKa Aug 19 '11 at 17:21

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

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.

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.

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.

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

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.

## protected by gnatJun 27 '14 at 10:40

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).