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
  • 8,591
  • 5
  • 31
  • 50
kprobst
  • 641
  • 2
  • 6
  • 9
  • 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 04:56
  • 5
    Already asked a looong time ago on SO; perhaps those should be migrated here: http://stackoverflow.com/questions/454303/why-are-we-using-i-as-a-counter-in-loops and http://stackoverflow.com/questions/4137785/why-are-variables-i-and-j-used-for-counters – BlueRaja - Danny Pflughoeft Jun 25 '11 at 06:14
  • 9
    Its a duplicate question and i love this answer. [Because of the name D*ijk*stra.](http://stackoverflow.com/questions/4137785/why-are-variables-i-and-j-used-for-counters/4150641#4150641) – Vivart Jun 27 '11 at 06:56
  • 1
    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
  • 2
    I always thought that i, j and k was taken from from D **ijk** stra – Christian Kjær Mar 25 '12 at 11:42
  • 1
    BTW: I always use `ii` (and `jj`, `kk`, ...) so that it is easier to locate when searching. –  May 05 '15 at 14:17
  • I think that there might a psychological reason behind this. I mean it **just feels right**. – Luke A. Leber Oct 30 '16 at 06:02
  • 2
    What else would you use? – Thorbjørn Ravn Andersen Dec 30 '17 at 05:33
  • @Sabuncu just find "all words only" or a similar option, preferably case sensitive, and `i` will match just fine – phuclv Jan 24 '20 at 15:57
  • @phuclv You probably mean **whole** words only. That requires the searcher to make an effort to change the find settings; I wouldn't call that easy. –  Jan 24 '20 at 19:43
  • I usually use `i`, `n`, `t` for local "throwaway" variables, but I have no idea where I picked that up. I always thought at least `i`,`n` was the most common, but I realize now that I'm probably in an obscure minority compared to the `i`,`j`,`k` crowd... – Magnus Sep 15 '21 at 19:32

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
  • 8,591
  • 5
  • 31
  • 50
Jerry Coffin
  • 44,385
  • 5
  • 89
  • 162
  • 16
    +1 I hadn't thought about the math correlation here. This was an interesting realization. – Casey Patton Jun 25 '11 at 00:14
  • 84
    +1 for attributing to psychosis what some imagine to be style. – Crashworks Jun 25 '11 at 02: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 Jun 25 '11 at 03:38
  • 38
    @Guy 'i' is for index, j is the next letter after i – Martin Beckett Jun 25 '11 at 04:02
  • @Martin Beckett @Jerry Coffin can you cite a reference? – Louis Rhys Jun 25 '11 at 09:46
  • 2
    The trouble with allowing the rules to vary (ie you can use "i", "j", etc, but only sometimes) is that the judgement call comes in - and then your nice flexible style guide results in 27 pages of crap with variables called "i", "r", and "rr". Just ban it. Thats not being psychotic, thats being pragmatic. – quickly_now Jun 25 '11 at 14:26
  • @Louis: A reference for what? The rules in Fortran, use of `i` as a subscript before Fortran, or something else? – Jerry Coffin Jun 25 '11 at 14:28
  • 1
    @quickly_now: that problem is with the rules, not the naming. If i makes sense, a rule that bans all 1-letter variables is wrong. Pragmatic would say that all variables be "descriptive of their purpose" and leave it at that. – gbjbaanb Jun 25 '11 at 17:44
  • 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. – Craig Tullis Feb 28 '15 at 08:30
  • adding this question to my favourites, just for the "phsychosis" comment – sakis kaliakoudas Jul 03 '15 at 14:28
  • 1
    @LouisRhys, what kind of reference are you looking for? If you want one about the mathematical notation, almost any linear algebra textbook will show plenty of examples. Ditto any competent numerical methods text. If you are looking for a FORTRAN reference, dig out any *OLD* (by definition!) FORTRAN IV textbook. Alternatively, you could try to dig out an old FORTRAN reference manual (I think CDC document number 60279900 was the FORTRAN IV reference manual). – John R. Strohm Jul 14 '15 at 18:47
68

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.

littleadv
  • 4,704
  • 27
  • 26
  • 36
    ijk = "I'm just kidding". :-) – Mahmoud Hossam Jun 24 '11 at 23:59
  • 3
    can you cite a reference? – Louis Rhys Jun 25 '11 at 09: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
  • that i is used as the shorthand for "index" – Louis Rhys Jun 25 '11 at 22:40
  • 26
    You can cite me as a reference if you want. – Captain Sensible Jun 26 '11 at 00: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. –  Jul 22 '11 at 12:01
  • 3
    In your train of thought `i` could mean iterator as well. – totymedli Apr 10 '13 at 08:07
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,878
  • 11
  • 125
  • 168
BillThor
  • 6,232
  • 17
  • 17
  • For single loops I agree, but for loops containing loops I like a bit more description. – Edward Strange Jun 24 '11 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. – Peter Rowell Jun 25 '11 at 00: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 Jun 25 '11 at 00: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 Jun 25 '11 at 02:26
35

I believe it comes from the Summation:

Summation

Raphael
  • 2,234
  • 1
  • 22
  • 23
  • 3
    Interesting! This predates FORTRAN, but in the context of CS? – kprobst Jun 25 '11 at 06:18
  • 3
    Since Backus had a Masters in mathematics it seems logical. – dbasnett Jun 25 '11 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 Aug 19 '11 at 17:21
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,121
  • 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,486
  • 16
  • 84
  • 135
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,693
  • 3
  • 18
  • 22
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
  • 3
  • 4
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
  • 3
  • 7