Why do most of us use ‘i’ as a loop counter variable

coding-standardscoding-styleprogramming practices

Has anyone thought about why so many of us repeat this same pattern using the same variable names?

for (int i = 0; i < foo; i++) {
    // ...
}

It seems most code I've ever looked at uses i, j, k and so on as iteration variables.

I suppose I picked that up from somewhere, but I wonder why this is so prevalent in software development. Is it something we all picked up from C or something like that?

Just an itch I've had for a while in the back of my head.

Best Answer

i and j have typically been used as subscripts in quite a bit of math for quite some time (e.g., even in papers that predate higher-level languages, you frequently see things like "Xi,j", especially in things like a summation).

When they designed Fortran, they (apparently) decided to allow the same, so all variables starting with "I" through "N" default to integer, and all others to real (floating point). For those who've missed it, this is the source of the old joke "God is real (unless declared integer)".

Most people seem to have seen little reason to change that. It's widely known and understood, and quite succinct. Every once in a while you see something written by some psychotic who thinks there's a real advantage to something like:

for (int outer_index_variable=0; outer_index_variable < 10; outer_index_variable++)
    for (int inner_index_variable=0; inner_index_variable < 10; inner_index_variable++)
        x[outer_index_variable][inner_index_variable] = 0;

Thankfully this is pretty rare though, and most style guides now point out that while long, descriptive variable names can be useful, you don't always need them, especially for something like this where the variable's scope is only a line or two of code.

Related Topic