3 ms·
Why are variables “i” and “j” used for counters?
- RiderOfGiraffes 16y agoDup: http://news.ycombinator.com/item?id=1971507 http://news.ycombinator.com/item?id=1971507 Many comments there.
- davewiner 16y agoDates back to Fortran, a language that was designed primarily to work on real numbers, for scientific calculation. They needed whole numbers or "integers" -- but they didn't have the concept of type declarations yet (or it was a hodgepodge and non-standard) -- so they came up with a convention that variables whose names began with i through (some upper bound I don't remember) were integers. Since loop indices are integers it made sense that their names should begin with I. If your outer loop's variable is named I and you all of a sudden need an inner loop, what are you going to name it? J of course. Which is how this convention began. Obviously today's programming languages not only allow you to declare the types of variables, many of them support variable types depending on what's been stored in a variable. But this was at the dawn of the computer era, before such coolness had come about.
- tzs 16y ago> Dates back to Fortran... I would expect it is far older than that. Mathematicians and others of that ilk have long used i, j, and k when writing sums using the capital sigma notation (and when writing products using the capital pi notation). Fortran almost certainly acquired it from that.