Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

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.

As a side note, I'll often use other short-hand variables in my code. For example, c is for counting, r is often a record in a foreach loop of many records, and so on. But i is almost always something I end up using in a for loop that has to count. –  Kerri Shotts Jun 25 '11 at 4:56
Already asked a looong time ago on SO; perhaps those should be migrated here: stackoverflow.com/questions/454303/… and stackoverflow.com/questions/4137785/… –  BlueRaja - Danny Pflughoeft Jun 25 '11 at 6:14
Its a duplicate question and i love this answer. Because of the name D*ijk*stra. –  Vivart Jun 27 '11 at 6:56
The chap who taught me to code had a personal idiosyncrasy of using n for this purpose, and I still do the same. I wouldn't if I was working on something completely unrelated, though. –  TRiG Jun 27 '11 at 10:21
I always thought that i, j and k was taken from from D ijk stra –  Christian Kjær Mar 25 '12 at 11:42

10 Answers 10

up vote 135 down vote accepted

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.

share|improve this answer
+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
+1 because I had missed (or possibly forgotten) the old joke :) –  Ryan Culpepper Jun 25 '11 at 2:28
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 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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
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". Sorry. –  user23157 Jun 25 '11 at 1:41
+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 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

share|improve this answer

I believe it comes from the Summation:


share|improve this answer
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 - 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.

share|improve this answer
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
@Louis - reference for what? There's no rule, it's a common convention, but don't have to always use i as index... –  littleadv Jun 25 '11 at 21:06
You can cite me as a reference if you want. –  Diego Deberdt 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

protected by gnat Jun 27 at 10:40

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.