Index Variables

This is a discussion on Index Variables within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Whenever I write for loops, I always do the first one starting with i as the index variable.(and go on ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Index Variables

    Whenever I write for loops, I always do the first one starting with i as the index variable.(and go on down the alphabet from there) In books the indecies always appear as i, j and k, does anyone know how this came to be, and do also follow this rule?
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    A book of mine says that the usage of i as the index variable dates back to Fortran or something like that.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    A book of mine says it's from math, where they use i a lot as index. Thinking of this, I remember that i, j and k were used a lot as indices. And p, q and r are used a lot as variables, at least in my math and computer science books. And don't forget about x, y and z in geometry.

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Although I do use i, j, and k as index variables, I like to use x, y, and z first.

    This probably springs from the fact that I work with graphic modes a lot, and so I can directly relate the indexes to (x,y) positions on the screen.
    My Website

    "Circular logic is good because it is."

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    call me weird but i've always used integers with actual names that are named after what they are doing, it makes it easier for others to read and understand what my program is doing, but hell ignore me i'm also a huge commenter.

  6. #6
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    >i'm also a huge commenter.

    That is good. Lots of times even good coders who do comment there code dont actually comment it enough. People dont realize the importance of commenting code very liberally.

    I have a friend who is currently looking for work in the game industry, and he has a sample game which has developed. In his game he used comments very liberally, there was comments on nearly every line of code.

    When he showed his code to game companies who he was wanting to sign on with, they saw his use of comments liberally and really gave him a "thumbs up" for doing that. They said that comments in code is one of the main things they look for when somebody submits sample code to them.
    My Website

    "Circular logic is good because it is."

  7. #7
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Well good then i'm on the right track. My CS2 teacher insists we don't comment to increase time effecency(sp), god she IS dumb.

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >My CS2 teacher insists we don't comment to increase time effecency(sp), god she IS dumb.

    Sure she didn't mean: don't comment too much?

    Maybe a tip: Commenting is important, but it should be useful commenting. I sometimes see students doing a project at our company who do a lot of unnecessary commenting. Things like "increase variable A", "compare values", etc. Commenting should not describe code, but explain code. Why increasing variable A? Why comparing values?

    Readers of your code usually understand the programming language so the understand what you are doing, but they would like to know, at certain places in the code, why you are doing specific things.

    Anyway, if you're used to commenting, that's great, but try to improve your way of commenting. When I was a student, I often let a friend of mine, who was proffessional programmer, review the code. Very useful, someone who teaches programming often looks at other aspects of the code than someone for whom programming is his/her job.

  9. #9
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    thats why i like this site, i can post code and have the comments and style reviewed. I perfer comments like

    //Increasing variable a to continue loop

    rather then like u said

    //increasing variable a

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    > //Increasing variable a to continue loop

    That is a much better comment. But a programmer could probably see from your code that you're increasing variable a. More information would give a comment like:

    // End not reached, continue loop.

    This, explains why to continue the loop. I also find it hard to do useful commenting. Normally I don't comment lines, but blocks of functionality. Only when in a line something special is done or more comment is required, then I would put it there. But these are things for which I ask my more experienced collegues to review.

  11. #11
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Originally posted by face_master
    A book of mine says that the usage of i as the index variable dates back to Fortran or something like that.
    i is for iterator

    i use i,j,k,etc... all the time in simple loops. sometimes i use 'count' if it starts getting complicated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 06:40 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM

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