Why is "i" chosen in programming loops?

In imperative languages we frequently use i as the counter in loops (and sometimes in recursive cases in non-imperative languages). Example:

for i := 0; i < 10; i++ {
    // do stuff

I’m wondering how this was coined and what it stands for. My two guesses are either iterator (or iteration, since it’s usually the iteration count), or is a perverted version of Hungarian notation, where one denotes a variable with a single letter expressing its type. Prefixing with “i” means “integer”, thus a variable just named “i” would mean “a temporary integer variable of no real significance”, which expresses its meaning pretty well.

I do know that “i” is also frequently used in summations (sigma notation) and sequences in math, and I suspect its use in programming derives from there, in which case “iteration” makes more sense, but that just moves the problem back to “why do we, by convention, use i for variables of summation in math?”

This question is surprisingly hard to Google.

I learned it when I learned FORTRAN, and this seems to agree.

Because programmer egos know no bounds?

i for integer is what I learned. n makes a good general purpose integer too.

I started programming in the 70s. I didn’t even hear of hungarian notation until the late 80s.

Yes, old versions of FORTRAN mandated variable names based on type, “I” was the first available integer name.

I would content that it comes from the usual notation used for mathematical expressions involving ranges. Particularly sums, but occurring in other places as well. Why mathematicians use i,j,k is however back to the same question, and it is almost certainly that i denotes an Integer. This distinguishes the variable from a real valued one, for which the usual notation uses x,y,z. Mathematicians tend to use n as a count, and thus a member of the Natural Numbers. You may see a sum run i from -n to n, which is consistent with this use. But you don’t see r as a real value, probably because x got there much earlier.

Early Fortran codified the I though N as Integers by default. Take it from someone who sometimes rewrites ancient dusty deck Fortran code for a living - “implicit none” is the most important statement you can have at the top of your code.

As if Hungarian notation isn’t already perverted enough? :smiley:

And while the transfer from “integer” via the mathematical tradition seems likely to be nearest the truth, I think ‘i’ for “index” is at least a happy coincidence.

i means “index”. Most example loops I have seen use “x”, but i is short for “index”.

I took a FORTRAN class ca 1974.

I concur with “I” being the first and smallest name acceptable for an integer.

Micros eliminated the uppercase shift (nearly universal on mainframes, due to printer* limitations).

    • the S360 printer (I used to load one and still can’t remember the name) used a rotating chain with letters on it. When the desired letter came into desired position, a solenoid would fire, stamping the letter onto the paper.
      The chain could hold 52 alpha positions - you could get chains with 1 set of upper and one lower. Or two sets of upper case.
      Using the all upper case chains doubled the print speed.

And no, we did not type with cap lock on - VTAM (the sub-system which handled terminal communication) was set to automatically shift all lc to uc.

No one thinks “i” stands for “index”?

At any rate, there’s a lot of speculation here but not a lot of evidence…

ETA: Whoops, should’ve refreshed before posting. (About the “index” thing, not the evidence thing)

The reason i means index is because the, uhh, loop index determines where you are in the loop. Why on earth would it mean anything else? The data type might be integer, or it might be something else…

I’ve never heard “i” referred to as the “loop index”, so I definitely wouldn’t have guessed it. If someone said “index” to me I’d almost always assume they were talking about an array or list (or possibly map/dict). While it’s true that if you’re iterating over an array “i” would be called the “current index” you’re often not iterating over an array.

I have never seen an example loop use “x”. Doesn’t mean it doesn’t happen, but I’ve never seen it personally.

We could do with a bit of programming history lessons here.

As others have said, it goes back to the days of FORTRAN, which in turn seems to have borrowed the usage from common algbraic usage.

There’s more that could be said, for you software history buffs.

The most extensively used version of FORTRAN for a long long time was FORTRAN IV. But before that there was FORTRAN II, which is what I first learned. There was no FORTRAN III.

All versions of FORTRAN has a neeto-keeno :rolleyes: feature that seemed like a great idea at the time, but has since been universally recognized as a major clusterfuck. You never had to declare variables (although you could if you wanted).

The syntax was clear enough that the compiler could tell variables from keywords by the context, even without them being declared before otherwise being used. Even the key words of the language were not reserved words, and could be used as variable names (with just a few exceptions). As the compiler read the source code, line by line, the first time it encountered any mention of a variable, it was automatically declared. This saved you the hassle of having to declare them all.

So there was a default assumption about names and types: Any variable beginning with I, J, K, L, M, or N was automatically made an INTEGER; all others were automatically made a REAL (what we now commonly call a float. You could, however, explicitly declare any variable with any name to be of any type you wanted. (I don’t even remember whether that possibility first appeared in FORTRAN II or IV.) Note, FORTRAN II didn’t have very many data types. I think INTEGER and REAL was pretty much it.

FORTRAN IV added more data types – LOGICAL (what we now call Boolean), DOUBLE PRECISION (which I believe first appeared in FORTRAN IV, not II, and mean double float), and even COMPLEX. (Yes, COMPLEX was a native data type.) Variables of these types all had to be declared, as there were no names that automatically defaulted to any of these types.

So what was the clusterfuck? Have you figured it out yet?

The problem was that any name that you accidentally mis-spelled as you keypunched up your program into a deck of cards, that mis-spelled name would automatically be defined as a distinctly different variable from what you meant it to be, with no warning whatsoever to the programmer. Some compilers tried to be helpful by warning you of variable names that were only used once, or that appeared on the right-hand side of an = before appearing on the left-hand side. But this was always a hit-and-miss approach.

So programs were always full of subtle errors, arising from mis-spelled variable names that you probably never noticed. Proof-reading your programs was more important than ever before or since, and that still wasn’t fool-proof. That is why rocket ships blew up on launching pads, why submarines sank, why medical devices killed their patients, why the Great Depression happened, and why Alexander the Great never got farther than India.

Algol was the first language that came into wide-spread use that required ALL variables to be declared. A lot of programmers (this one included) thought that was a total pain in the ass at the time, and totally unnecessary bullshit. But the importance of that simple rule was probably as important as the invention of the digit 0 in the larger scheme of things.

Forget about that IMPLICIT statement in later versions of FORTRAN. That was just an after-thought. And, as you can see, it just defeated the purpose (well, one of the purposes) of requiring declarations, which programmers of the time were just beginning to appreciate.

So you can see, what we later came to know as Hungarian Notation had its earliest roots in FORTRAN, although Charles Simonyi (who was a Computer Science grad student at UC Berkeley at the same time I was a freshman or sophomore there) took that idea and built extensively upon it. (I vaguely knew who he was, but I’m certain he didn’t know me, or even know of me. He wrote the Berkeley SNOBOL compiler/interpreter, and some of my classes were done using his version of the SNOBOL language.)

So, you think Hungarian Notation is perverted? You don’t know the half of it! Hungarian Notation, as it popularly became known, is indeed a perverted and garbled version of the scheme as Microsoft (and presumably Simonyi himself) originally meant it to be. For an interesting history of this, and an essay in general on using variable naming conventions to assist with software reliability, see Making Wrong Code Look Wrong by Joel Spolsky, May 11, 2005. His discussion about Hungarian Notation, and how it got perverted, begin about two-thirds of the way through the essay. Recommended reading!

In most modern languages the loop variable is often not an index at all, rather it is an instance derived from an iterator. Personally, if I’m coding Python or Java (or, God help me, C++ or Perl) and the loop variable is an integer, it will usually be called i,j,k unless it has some clear semantics. But if I’m iterating over some other bundle of objects, the loop variable will be called something sensible. Often I call it the_thingy when iterating over a bundle of thingys. That is however just me.

Unless you have an indexable class of thingys you can’t index them, but we love to iterate over sets, trees, all sorts. Also, a common paradigm is to iterate with keys.

A lot of the time in loops, especially if you are doing much mathematics, you aren’t indexing anything, but rather iteratively calculating something. For instance a sum.

Back in the day of course there was only one way of creating an entity with more than one thingy in it. An array. So back then they were indexed. With integers. If a program uses x as a loop variable there is a very good chance that it means the X dimension, and somewhere else there is a y and possibly even a z. That would be a reasonable thing to do in many cases. I certainly do it in such circumstances (although I seem to spend most of my life indexing by Easting, Northing, and Depth.)

If this is in response to me, I wasn’t disagreeing with you. Inside my own head, “i” means “index” as well. I was expressing shock that I was the first in the thread to suppose this (for I had not yet refreshed the thread and seen your post making the same proposal).

But regardless, I acknowledge that my own belief is also not supported by strong evidence. I don’t know who started this notational trend, or when it started, or where (in the context of mathematics; the Fortran explanation for the programming side of things seems reasonably well-supported). And without such cites, this is merely a just-so story.

Senegoid’s explanation is very thorough and completely correct IMHO.

There were only a llmited number of letters that could begin variable names used as integers and “I” was the first of those letters. You could use some other letters like “J”, “K”, “L”, “M” and “N” just as well. But people used to start with “I” because it was first on the list.

The people who say that I is used because it represents “Index” is not correct. It is not based on any fact that I ever heard. I started using Fortran around 1972 and used other similar languages after that. I’m confident I would have heard of any language that claimed to use “I” because it stood for “Index”. That is just not the case.

You must be a young’un. When I first learned FORTRAN in the early 60’s, there was no default. Variables that began “I” through “N” were always integer, by definition. “I” was typically used for outer loops, with “J” and, if needed, “K” were typically used for nested loops.


Yeah, optional declaration came with Fortran IV which is what I started with. IMPLICIT statement came with Fortran 77.

It comes from the father of (almost) all programming-Fortran. Back in the Fortran II days, integers had to start with one of six letters: i,j,k,l,m,n. Or maybe that was the default-I was 18 when I learned Fortan. But integers always started with one of those letters. It was kind of traumatic when your code got so complex that you had to start using ii or mm.

yeah. early 60s predates me. I didn’t learn FORTRAN until 1970. :slight_smile:
I thought i,j,k,l,m,n were defined as integers and only back in the day. Along with column 6 being the first column available for the command.