Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are variables "i" and "j" used for counters?

People also ask

What does i and j mean in coding?

i=iteration while j=after interation.

Why do people use i for for loops?

It is common practice to use i in for loops. This is just what programmers are used to. It may relate to the concept of dimension in 3D space in mathematics. The 3 mutual orthogonal unit vectors i, j and k represent the row, column and depth of a 3D array.

What is the use of i and J in Java?

i, and j are also used in math and physics. i is often the notation for Cartesian x-axis basis unit vector while j is used for Cartesian y-axis basis unit vector.

What is J loop used for?

Description. The BD J Connector Loop is an intravenous adapter for infusion therapy. It comes with standard bore, flexible extension tubing with twist-on luer lock to be used with an IV Start Kit to draw blood or begin infusion therapy. The J Connector Loop is packaged sterile to not introduce bacteria.


It comes ultimately from mathematics: the summation notation traditionally uses i for the first index, j for the second, and so on. Example (from http://en.wikipedia.org/wiki/Summation):

\sum_{i=1}^{n} i = \frac{n^2 + n}{2}

It's also used that way for collections of things, like if you have a bunch of variables x1, x2, ... xn, then an arbitrary one will be known as xi.

As for why it's that way, I imagine SLaks is correct and it's because I is the first letter in Index.


I believe it dates back to Fortran. Variables starting with I through Q were integer by default, the others were real. This meant that I was the first integer variable, and J the second, etc., so they fell towards use in loops.


Mathematicians were using i,j,k to designate integers in algebra (subscripts, series, summations etc) long before (e.g 1836 or 1816) computers were around (this is the origin of the FORTRAN variable type defaults). The habit of using letters from the end of the alphabet (...,x,y,z) for unknown variables and from the beginning (a,b,c...) for constants is generally attributed to Rene Descartes, (see also here) so I assume i,j,k...n (in the middle of the alphabet) for integers is likely due to him too.


i = integer

Comes from Fortran where integer variables had to start with the letters I through N and real variables started with the other letters. Thus I was the first and shortest integer variable name. Fortran was one of the earliest programming languages in widespread use and the habits developed by programmers using it carried over to other languages.

EDIT: I have no problem with the answer that it derives from mathematics. Undoubtedly that is where the Fortran designers got their inspiration. The fact is, for me anyway, when I started to program in Fortran we used I, J, K, ... for loop counters because they were short and the first legally allowed variable names for integers. As a sophomore in H.S. I had probably heard of Descartes (and a very few others), but made very little connection to mathematics when programming. In fact, the first course I took was called "Fortran for Business" and was taught not by the math faculty, but the business/econ faculty.

For me, at least, the naming of variables had little to do with mathematics, but everything due to the habits I picked up writing Fortran code that I carried into other languages.


i stands for Index.
j comes after i.


These symbols were used as matrix indexes in mathematics long before electronic computers were invented.


I think it's most likely derived from index (in the mathematical sense) - it's used commonly as an index in sums or other set-based operations, and most likely has been used that way since before there were programming languages.