The use of i
, j
, k
, and l
as loop variables or integer counters has deep roots in both mathematics and early programming conventions. Here’s a bit of history and reasoning behind this tradition:
Mathematical Origin:
– The letters i
, j
, k
, and l
have long been used in mathematics as counters or indices in summations, matrices, and vector notation, particularly in linear algebra. For example, i
and j
are often used as row and column indices in matrices.|
– These variables are typically used as subscripts or iterators in formulas and equations. This usage has likely influenced their adoption in programming.
Fortran and Early Programming Languages:
– Fortran, one of the earliest high-level programming languages developed in the 1950s, influenced a lot of programming conventions, including loop variable naming. In Fortran, variables starting with I
to N
were integers by default, so i
, j
, and k
became natural choices for counters, especially in loops.
– Because Fortran was widely adopted, these conventions spread to other programming languages, which influenced generations of programmers to use these variables as loop counters.
Compactness and Readability:
– Variables like i
, j
, and k
are compact and readable. They’re short and easy to type, making them convenient for small loops or simple, local iterations.
– In nested loops, i
, j
, and k
provide an easy-to-remember sequence, particularly for indexing multi-dimensional arrays.
Widespread Convention:
– By the time languages like C, Pascal, and later Java and Python emerged, the practice of using i
, j
, k
, etc., for loop variables was already standard. These languages adopted many of the conventions from their predecessors, creating a legacy effect.
So, this usage was introduced in the earliest programming languages like Fortran and has since been carried forward, shaping conventions across almost all programming languages today.