Hacker News new | past | comments | ask | show | jobs | submit login
Why are variables "i" and "j" used for counters? (stackoverflow.com)
81 points by J3L2404 on Dec 5, 2010 | hide | past | web | favorite | 36 comments

i, j, and k come from Hamilton's Quaternions.

Euler picked i for the imaginary unit.

Hamilton needed two more square roots of -1:

    ii = jj = kk = ijk = -1
Hamilton was really influential, and quaternions were the standard way to do 3D analysis before 1900. By then, mathematicians were used to thinking of (ijk) as a matched set.

Vector calculus replaced quaternionic analysis in the 1890s because it was a better way to write Maxwell's equations. But people tended to write vector quantities as like this: (3i-2j+k) instead of: (3,-2,1). So (ijk) became the standard basis vectors in R^3.

Finally, physicists started using group theory to describe symmetries in systems of differential equations. So (ijk) started to connote "vectors that get swapped around by permutation groups," then drifted towards "index-like things that take on all possible values in some specified set," which is basically what they mean in a for loop.

What variable did mathematicians use for summation before physicists started using group theory to describe symmetries in systems of differential equations? Isn't that a fairly recent development?

My search for an old paper with a bloody summation symbol found nothing. I did, however, find this OT gem about Newton, written in 1848:

From the thick darkness of the middle ages man's struggling spirit emerged as in new birth; breaking out of the iron control of that period; growing strong and confident in the tug and din of succeeding conflict and revolution it bounded forwards and upwards with resistless vigour to the investigation of physical and moral truth; ascending height after height; sweeping afar over the earth, penetrating afar up into the heavens; increasing in endeavour, enlarging in endowment; every where boldly, earnestly out stretching, till, in the Author of the Principia, one arose, who, grasping the master key of the universe and treading its celestial paths, opened up to the human intellect the stupendous realities of the material world, and in the unrolling of its harmonies, gave to the human heart a new song to the goodness, wisdom, and majesty of the all creating, all sustaining, all perfect God.

If you want an old paper with a summation symbol, you could do worse than the book where the symbol was first used, by Euler: http://www.math.dartmouth.edu/~euler/docs/originals/E212sec1...

(He uses x as the index variable there.)

Do you mean to imply that the convention for loop variables came from on high?

What a great comment. So little historical knowledge circulates online.

In FORTRAN identifiers starting with I,J,K,L,M,N declared as INTEGER by default.

It is exactly I was taught in university when I learned FORTRAN. But it is probably influenced from mathematical notation of indexing.

FORTRAN's system of giving default types based on the first letter of a variable name gives rise to the old joke, "God is real... unless declared integer".

I still use 'n' and 'f' every so often.

Reason being, on the ZX Spectrum, it had all the BASIC keywords on each key, and pressing the key would input the keyword itself (depending on where you were etc). The 'N' key had 'NEXT', and the 'F' key had 'FOR'.

So to type 'next n' you just pressed 'N' twice. Typing in 'for f' you pressed 'F' twice.

I agree, it probably comes from maths.

As a side note, the first letters are used for constants, the last letters are for variables; and it was Descartes who popularized this usage: http://en.wikipedia.org/wiki/Polynomial#Notation

I believe they used i for index, and also for the reason that it wasn't used by other usages as above.

Similarly, the integration sign is actually a long s, standing for sum. http://en.wikipedia.org/wiki/Integral_symbol

This is correct. I assure you that the physics texts all tend to use i and j as index variables. Particularly for matrices, whose terms have a tendency to be written as M_i_j. [1]

Remember that modern electronic computing was invented by mathematicians and physicists.


[1] HN isn't about TeX, so read that as "M sub-i sub-j".

Next question: what other single character variable names do people use? I've always wanted to work up a master list somewhere.

Speaking to my own usage, I use i and j for indices, x, y, and z for Cartesian coordinates, n for "number", k and v for "key" and "value" when iterating through a hash, and r for a variable I'm using for a return value later on.

A t for a type variable. I sometimes name variables to the data structure that they are holding, e.g. p for pair, h for heap. The names x, y and z for things that have a generic type, and xs ("axes"), ys ("whys"), zs ("zeds") for collections of general things (and xss for collections of collections of things).

For example, an F# pairing heap:

    type 't heap = Empty | Heap of 't * 't heap list

    let findmin (Heap(x,_)) = x
    let merge h1 h2 = 
      match (h1,h2) with
      | Empty,h | h,Empty -> h
      | Heap(x,h1s),Heap(y,h2s) -> if x < y then Heap(x,h2::h1s) else Heap(y,h1::h2s)
    let deletemin (Heap(_,hs)) = List.fold merge Empty hs
    let insert h x = merge h (Heap(x,[]))
First line: t for type. Second line: x for something of generic type t. Third line: h1 and h2 for heap. Fifth line: h1s and h2s for lists of heaps corresponding to h1 and h2. This saves time coming up with names, and it ends up being more readable if you're used to it. Of course these short names will only be used for variables that are used internally in a function, not for function names and such.

I use a description of what I;m iterating over. If it's a list of people, I use 'person'. If the keys are session IDs, I use 'sessionid'. I do this because it's easier for me and I think I'm being nicer to the next person to come along.

I do that if I'm doing collection.each in Ruby or something, but if I'm in C or even if I'm doing collection.each_with_index, I use "i" for the index.

master list = the alphabet

Yes, I was hoping for a list of what each letter is used for.

I use 'i' for 'index' / 'iterator', j because it follows i, and k because it follows j. The first time I wrote a double-deep loop, the only ones I could recall having seen had used x and y, because they mapped to x,y coordinates.

So why don't I use x and y where it's not coordinate based? Weaker fingers. 'X' is inherently much harder to hit than 'i', 'i' serves as a mnemonic as it's usually referring to the index being accessed, and 'y' takes a larger movement than 'j'.

Fortran is the culprit

Not quite. Fortran enshrined the convention, so the question becomes "Why did Fortan pick that range for integers?"

I don't agree that the question changes. I would argue that the fact that Fortran enshrined the convention is a more relevant answer than the answer to why Fortran picked that range. The reason being that the answer to the latter question is most likely from another domain, such as Mathematics.

Although I do find the answer to the latter question interesting as well, just not as interesting as the answer to the first.

Because mathematicians, physicists, and engineers use i through n for integers in their mathematical formulas, and Fortran was their FORmula TRANslator into computerese. (Additional fun fact: They begin their matrix row and column counts with 1, which is why Fortran array indices start at 1.)


You didn't begin in column 7 but wisely avoided the continuation character.


FORTRAN taught me that languages should understand some spacing.


Identifying the first place i and j are used is a start, but it doesn't really answer the question. The question is "why?", not "where did it start?"

Of the explanations on the SO page, the fact that i is the first letter of index seems like the most likely. j is popular because it follows i.

Cause Fortran's integer types where prefixed by: I - N

Not only prefixed, I barely remember my grandfather telling me a story about a thousand years ago identifiers where so precious, they were only ONE char long, from A to H were asigned to strings, from I to P were for integers and the rest for floats.

Oh those long gone years...

When I first learned programming, the tradition was to use 'f' and 'n'.

On the ZX81, the "FOR" command was accessed by pressing the F key in a special "command mode", so by typing the F key twice you got "FOR F". Similarly "NEXT N".

Edit: Yes, duplicate of what axod said here: http://news.ycombinator.com/item?id=1971594

I rarely use i-j-k, I tend to use n-m-r for some reason(possibly because I've been programming with iterator syntax so long, that I always think first of grabbing "thing n" rather than incrementing "counter i.") But on reflection I think this is probably less legible.

For me i has always been "iterator variable" or something similar, j being the logical next iterator variable. Then again I haven't done this (code) for very long.

When I first started studying graduate math, professors would write how my usage of i as bizarre since I would use it for my index in sums.

sqrt(-1) = 1,2,3,... ????? WTF

I always used and thought of i as "index", c as "count", and n as "number".

More specifically, I think the N stands for "Natural number".

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact