# why is i the standard counting variable?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-19-2005
ssjnamek
why is i the standard counting variable?
in school they always say use good variable names and it makes sense but i have to ask why is "i" the standard name for a counting variable?

i know when there was hardly any ram using a one letter variable made sense along with 20000000000 pages of goto but years later we use good names for everything but that for loop it still has i lol is there some reason to this madness that i missed???

oh well i was jsut curious since its kind of odd we use good names for everything BUT that.
• 09-19-2005
sand_man
It's easier and looks cleaner. Well that is why I do it anyway. If I have nested for loops I use i and j. If I'm working with screen coords then I use x,y and z if needed.
• 09-19-2005
Rashakil Fol
They probably borrowed the letter from common use in mathematics. i for index.
• 09-19-2005
Thantos
Quote:

Originally Posted by ssjnamek
in school they always say use good variable names and it makes sense but i have to ask why is "i" the standard name for a counting variable?

i know when there was hardly any ram using a one letter variable made sense along with 20000000000 pages of goto but years later we use good names for everything but that for loop it still has i lol is there some reason to this madness that i missed???

oh well i was jsut curious since its kind of odd we use good names for everything BUT that.

It harken backs to the days of old. i, j, k are the "standard" variable names for integers which is what most use for counters. There is nothing wrong with using those names in that context. Since the variable has a very short lifespan its not unreasonable to expect a person who is reading the code to not be confused by its purpose. Also I think that it is a good variable name if used in this context.
• 09-19-2005
ssjnamek
oh ok kind of suspected as much since it is well easier to read than count 1000 times over

but oh well was curious anyway since im no expert

LONG LIVE i lol
• 09-20-2005
Ken Fitlike
>>why is "i" the standard name for a counting variable? <<

i is for iteration.
• 09-20-2005
Boksha
i is also for index.
Anyway, it's a one-letter variable, so it's very easy to type. Generally just lazyness. Convention also has a role in it. "i" may not be very descriptive, but if you ALWAYS use i for counting, you'll know exactly what a variable i does in a program when you see it.
• 09-20-2005
DougDbug
Quote:

i know when there was hardly any ram using a one letter variable made sense...
FYI - Variable names do not exist in the final compiled EXE code. Long variable names will make your source code a little longer, but they have no effect on the final program.

Quote:

Since the variable has a very short lifespan its not unreasonable to expect a person who is reading the code to not be confused by its purpose.
Yeah, this is the key. If the scope of i is a few lines of code... maybe up to a half-page, then anyone reading the code can fully understand its use. But, you don't want that non-descript i showing-up a few pages later, where its not clear what it represents. Of course you can use i again (actually a new i) in another little loop.

There is nothing wrong with using a more descript variable names like LoopCount I probably use LoopCount almost as much as i.

With simple nested loops, i and j are OK. But, sometimes the code is more clear with names like RowCount, ColumnCount, InsideLoop, OutsideLoop, etc.
• 09-20-2005
curlious
Like DougDebug says a more discriptive variable is often good even in this situation, but requires more typing.

LoopCount doesn't help much but LedgerIndex or VectorCount. The idea is to capture the Abstraction or meaning of the variable as it is used.

• 09-20-2005
ssjnamek
wow i actually didnt think there would be this much a discussion on it just didnt seem like it had that much to it. though that is surprisingly interesting for a little old "i". eh but lazyness sounds like a better excuse lol. only reason it occured it cause in program for school i was getting tired of making new counter varia ble names and said screw it replaced all the little one or two line for loops with i that had no relevance outside of that.

oh so variable names have no barring on the exe then cause it just locates a memory address and stores the value there? ( i musta been thinking of goto earlier and gotten them confused). i know this was in operating systems class i just took or at least i think it was?

oh and what this codecomplete you speak of?
• 09-20-2005
curlious
Code Complete is a book Published by Microsoft and written by Steve McConnell.

It is a book mainly about the down and dirty work of writing code. It has some very good advice on coding.

Although I don't follow the book to the T I would suggest it as a good read.
• 09-20-2005
ssjnamek
oh that book lol i just recently saw it at barnes and nobles and it looked pretty code and decided to get it though i havent gone through it too much yet the bit i have scanned was very nice. i intend to read more of it but yea i just looked and saw OH THAT book. it was kind of on the floor cause im moving stuff around so i didnt see it at first lol
• 09-20-2005
Dae
I would think we still use it because its quite easy 'portable' to other programmers. What I mean is in O(n^3) loops or binary trees, etc. it would be a lot easier for the other programmers to understand what you're doing if you use i and j, because they automatically know that i is the first loop, and j is the second loop, without having to look back and figure out - which allows them to process the purpose of the loops quicker. I would believe thats why we continue to use it in books and tutorials, just simply because it is sort of standard, and allows others reading to grasp on quicker.

Besides that.. its i for index, index is a number in a loop, and using i in an array (e.g. array[i]) is a lot prettier than say index. But then again why not use e, for element. Who knows.
• 09-20-2005
okinrus
Quote:

in school they always say use good variable names and it makes sense but i have to ask why is "i" the standard name for a counting variable?
Its more appropriate to use variable names that depend on the scope of the variable. For variables that have greater scope, such as class variables and global variables, use longer names by all means. But for loop counters, the scope of the variable is very short that "i" suffices. When naming variables you can also use well understood acronyms. While "i" isn't quite an ancronym practically everyone understands it to mean a counter.

Quote:

i know when there was hardly any ram using a one letter variable made sense along with 20000000000 pages of goto but years later we use good names for everything but that for loop it still has i lol is there some reason to this madness that i missed???
It doesn't matter for compiled languages. The variables you use with "i" are either turned into a register or a memory address.

Quote:

oh well i was jsut curious since its kind of odd we use good names for everything BUT that.
Fortran allowed you to have implicit integers by starting the variable name with letters i through r, I think. But before Fortran, "i, j, k" were a lot used in mathematical summations, so that's where the idea likely occurred.
• 09-21-2005
grumpy
Quote:

Originally Posted by DougDbug
FYI - Variable names do not exist in the final compiled EXE code. Long variable names will make your source code a little longer, but they have no effect on the final program.

What you say is true now, but not if you go back further in time.

The resources available to compilers were severely limited, particularly by modern standards, a small number of decades back. And, to interpret variable names, the compiler needs to track what each variable name corresponds to. To do that, it uses available memory. In days when total memory resources in a machine were a few kilobytes or so, it was often necessary to keep variable names short and non-descriptive so the code could be compiled. That is the reason that some really old languages had relatively few features (by today's standards) and (in some cases) limited variable names to be less than 5 or so characters.

Conventions such as default types for particular variables (eg if it started with 'i', it was an integer) date from that period, because it was easier to implement a compiler --- to know a variable type, all the compiler had to do was examine the first character of it's name. Conventions such as i, j, and k for counters followed on from that.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last