why is i the standard counting variable?

This is a discussion on why is i the standard counting variable? within the C++ Programming forums, part of the General Programming Boards category; in school they always say use good variable names and it makes sense but i have to ask why is ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    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.
    hooch

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    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.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    They probably borrowed the letter from common use in mathematics. i for index.
    Attached Images Attached Images  

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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
    hooch

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>why is "i" the standard name for a counting variable? <<

    i is for iteration.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    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.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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.

    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.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    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.

    CodeComplete has a good discussion about this topic.

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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?
    hooch

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    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.

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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
    hooch

  13. #13
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    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.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    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.

    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.

    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.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    5,852
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which variable can store words?
    By Hunterofman in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2008, 05:59 PM
  2. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  3. defaulting ifstream variable to standard input
    By risby in forum C++ Programming
    Replies: 16
    Last Post: 02-06-2008, 07:11 AM
  4. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21