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.


10 Answers 10


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.

  • 16
    +1 I hadn't thought about the math correlation here. This was an interesting realization. Commented Jun 25, 2011 at 0:14
  • 84
    +1 for attributing to psychosis what some imagine to be style.
    – Crashworks
    Commented Jun 25, 2011 at 2:21
  • 3
    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
    Commented Jun 25, 2011 at 3:38
  • 38
    @Guy 'i' is for index, j is the next letter after i Commented Jun 25, 2011 at 4:02
  • 3
    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. Commented Feb 28, 2015 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.

  • 36
    ijk = "I'm just kidding". :-) Commented Jun 24, 2011 at 23:59
  • 3
    can you cite a reference?
    – Louis Rhys
    Commented Jun 25, 2011 at 9:46
  • 26
    You can cite me as a reference if you want. Commented Jun 26, 2011 at 0:01
  • 1
    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.
    – user8709
    Commented Jul 22, 2011 at 12:01
  • 3
    In your train of thought i could mean iterator as well.
    – totymedli
    Commented Apr 10, 2013 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. Commented Jun 24, 2011 at 23:54
  • 2
    Ultimately the answer is inertia. I started doing it with WATFOR (Waterloo FORTRAN) in 1973 and ... I'm still doing it in 2011. Commented Jun 25, 2011 at 0:01
  • 1
    @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
    Commented Jun 25, 2011 at 0:04
  • 12
    +1 for "In Fortran" because that's how that tradition has started - it does not matter whether you like Fortran or not.
    – artem
    Commented Jun 25, 2011 at 2:26

I believe it comes from the Summation:


  • 3
    Interesting! This predates FORTRAN, but in the context of CS?
    – kprobst
    Commented Jun 25, 2011 at 6:18
  • 3
    Since Backus had a Masters in mathematics it seems logical.
    – dbasnett
    Commented Jun 25, 2011 at 14:13
  • 9
    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
    Commented Aug 19, 2011 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.

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