69

I have heard that the reason the i variable is used so much is because there was an old computer where each variable could only be a single letter and that reserved the variables a through h as internal variables, leaving i as the first one available to the programmer. This seems believable, as there would be exactly 8 reserved variables which is a nice binary number. I haven't been able to confirm or deny this story.

Is this true? If so, what system was the origin of this behaviour? If it's not true, why was i used so much in the past?

9

7 Answers 7

92

FORTRAN made all variables starting with I, J, K, L, M and N integer by default. So just I by itself could be conveniently used as a loop variable. I imagine that choice was made because I, J, K, M and N are very commonly used as indices in mathematics. e.g., a sum of a series will typically be expressed as summing the terms A(i) for i going from 1 to n.

Where the practice originated in mathematics I have no idea. Influential languages such a C followed suit in that the K&R book used i as an index variable in examples.

Final answer: math and inertia.

Notes:

I didn't mention FORTRAN variables starting with i, etc. because computers essentially did not have lower case back then. I also intentionally left out L in math as it tends to be avoided as an index due to confusion with 1 and I.

FORTRAN's connection to math is quite strong. After all, it stands for FORmula TRANSlation with the main foreseen use converting science and engineering forumlas into something the computer can calculate.

12
  • 3
    I think it was variables beginning with i through n that were integer. But I agree that it was early FORTRAN coding style that set the pattern here. Commented Dec 1, 2016 at 1:45
  • 10
    "i" == "index" sounds like a plausible explanation for where it originated in mathematics, other letters just follow (just like with "x" == "unknown", and then you go to y, z; and similarly with coördinates, where "x" == "cross" == "cartesian", while vectors are still "addressed" using indices, so i, j again). But that's all just guessing around, of course. We did use to pronounce i as "index" in both math and coding, but that just makes sense regardless of the origins.
    – Luaan
    Commented Dec 1, 2016 at 8:50
  • 3
    I always thought "i" was for "integer", though "index" makes sense too.
    – Mr Lister
    Commented Dec 1, 2016 at 9:02
  • 6
    When teaching FORTRAN it was common to mention, as a mnemonic aid, that the first two letters of "integer" were the first and last letters of the sequence i through n , the starting letters that made variables integers by default. Commented Dec 1, 2016 at 9:52
  • 9
    @MichaelKjörling Ehm... "index" is a latin word, borrowed by English. Both the seminal books on linear algebra and calculus in the western world were written in latin, and latin was still the lingua franca of science for long before and after that. I'm no native english (or germanic in general) speaker, and we still use "index". It's your view that "index" is an English world that's English-centric :P And I suspect the Chinese didn't use i in their math books...
    – Luaan
    Commented Dec 1, 2016 at 17:14
10

It came from math, where it was commonly used as a subscript forever. It was likely popular because of its small size, being legible when printed in a very small font.

6
  • 4
    Don't forget that almost all printing, especially of software listings, in those days was on 132 column music paper. The font size wasn't variable.
    – Chenmunka
    Commented Dec 1, 2016 at 15:42
  • 1
    Printing code at sizes that weren't legible would not be particularly useful...
    – user
    Commented Dec 1, 2016 at 16:31
  • 1
    @Chenmunka That, or punched cards.
    – user
    Commented Dec 1, 2016 at 16:31
  • 4
    @Chenmunka I think he's talking about typeset mathematics texts, not code.
    – Random832
    Commented Dec 2, 2016 at 2:11
  • This would be my guess, too. But do you have any sources to elevate your answer beyond educated guesswork? (It's possible that there are no sources and all we can do is guess.) Commented Dec 2, 2016 at 13:52
9

The Forth language (from 1970) also used I and J as fixed names for the innermost and second innermost loop counter, so there was precedence as early as then.

I think the convention could be even older than FORTRAN though. Several math notations, such as for summation and tensors, use 'i' as an abbrevation for 'index'. FORTRAN's default types may have been chosen because scientists and mathematicians were already using i in this way.

2
  • 1
    FORTRAN uses column-major arrays so the I,J loops are arraigned to read memory sequentially which speeds up code considerably. Optimized memory access remains a major advantage for numeric FORTRAN code and a reason that it remains in use in science and engineering arstechnica.com/science/2014/05/… Commented Dec 1, 2016 at 15:31
  • Before FORTH was a complete language, it was bunch of routines partially implementing an interpreter. Moore ported it to whichever language the current project was using, so, FORTRAN, also COBOL and ALGOL. So, his context for variable naming wouldn't be any different from anyone else's at the time (1968 and thereabouts).
    – gbarry
    Commented Apr 23, 2017 at 5:46
7

The letter 'i' is an abbreviation of "index", which refers to the index notation, that is, specifying elements of an ordered collection by numbers. Another popular letter in math is 'n', abbreviating the word "number". These two became popular in math books long before programming (example from 1816)

When loops and arrays were introduced in programming languages, the index notation was carried over from math along with preferred letters to denote indexes.

0

The practice originated with Fortran. i-n are the first two letters of "integer".

All other variables in Fortran default to floating point

4
  • Welcome to Retrocomputing! Make sure to check out our tour to earn your first badge.
    – JAL
    Commented Dec 1, 2016 at 17:45
  • Fortran defaulted I, J, K, L, M, and N as integers.
    – Mark
    Commented Dec 1, 2016 at 21:31
  • OK, here from Fotran 77 manual:
    – WallStProg
    Commented Dec 4, 2016 at 18:45
  • 1
    "If not explicitly specified by a type statement or a FUNCTION statement, the data type of a data item, data name, or function name is determined implicitly by the first character of its symbolic name. By default, symbolic names beginning with I, J, K, L, M, or N (uppercase or lowercase) imply an INTEGER data type; names beginning with all other letters imply a REAL data type. " web.iitd.ac.in/~skak/map707/f77-manual.pdf
    – WallStProg
    Commented Dec 4, 2016 at 18:48
-2

I was always taught 'i' for iteration - because it is so commonly used in loops.

5
  • 3
    Maybe so, but the question asks why is it commonly used in loops.
    – Chenmunka
    Commented Dec 1, 2016 at 16:20
  • 2
    It's wrong actually. The real reason is that it comes from math. You should use "i" when you iterate over integers, and "x" when you loop over a collection of objects (in the context of being forced to use single letter var name). Do you have multiple collections of objects? use x,y,z. Multiple collections of integers? i,j,k Commented Dec 2, 2016 at 15:34
  • @PascalvKooten: "should"? I'm a mathematician, and I use (and not just me) $j $ often to index nets, so it loops over a collection of objects. And, I have very rarely (if ever) seen $x,y,z $ as indices. Commented Dec 4, 2016 at 6:41
  • @MartinArgerami x,y,z are not indexes, they are the objects as in python like for x in ["a", "b", "c"]. Commented Dec 4, 2016 at 16:57
  • @PascalVKooten Unless your program deals in cartesian coordinates, then don't use x/y as iteration variables for objects. Because that would confuse the reader. Commented Feb 1, 2021 at 16:37
-5

Usually we use i in looping, and for looping we only need iteration. So, from the word 'iterator' came i.

3
  • 1
    Welcome to Retrocomputing Stack Exchange. The system flagged this up as a low quality answer, probably because it's a bit short. Please read our tour, have a little look around (perhaps post some more, longer answers!), then come back and add a little more info to this answer.
    – wizzwizz4
    Commented Dec 1, 2016 at 21:46
  • If you're suggesting that i came from iterator you're wrong via an anachronism of several hundred years, and if you're suggesting the oppose you're not answering the question.
    – user207421
    Commented Dec 4, 2016 at 0:41
  • 1
    Thanks for editing your answer! Unfortunately it's still quite short and doesn't really say anything that isn't in other answers. It'd probably be best to answer another question instead; this one has a lot of answers already and there's not really much else to say on the topic that isn't already covered by the other answers (as far as I know).
    – wizzwizz4
    Commented Dec 14, 2016 at 22:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .