Thread: i j k variable names

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    i j k variable names

    Am I correct in the face programmers use I J and K as variable names for for loops since the FORTRAN language? Was the language designed to accept only these variables?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I use i, j, k out of habit, and always assumed i stood for iterator or something and that j and k followed suit alphabetically. I don't know about the fortran link ... but I couldn't see any language accepting only three variables!

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    If the variables are loop specific, then I use i,h,j, and k mostly. i is just for interger/iterator really, the others are just near it in the alphabet.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Check any math book/paper, you'll see i, j, and k are pseudo standards for indices on summations, integrals, etc... I'm pretty sure math predates computers.

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I am with Perspective, it has been in math for a lot longer than computers have been around.

    One reason you see a lot of it though, is habbit, some programmers who are newer, or even ones that have been doing it for a while, learned to use i in loops, and if you have nested loops j, k, and so on.

    I don't even think about it anymore, when I go to do a for loop or while loop

    Code:
    for(int i = 0; i < value; i++) {
      int j = 0;
      while(j < 100) { 
        /*stuff that changes j in more/less than +1 per loop */ 
      }
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    New York, New York
    Posts
    19
    I've never even heard of people using i, j and k as variables for anything. I'm used to seeing variable names like count or x for loops.

  7. #7
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    I've seen names like 'idx' or 'iter' used occasionally, but usually i, j, k, etc.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I use i, j, k on the first pass. After that, I'll substitute a more meaningful name so that I don't have to wonder why I've got i+j*60 somewhere in my code.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Agree with those that say it comes from mathematics, and want to add that 'i' derives from index, not integer or iterator.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I use i, j, k for iterations or simple short lived flag-like variables when it makes sense. To me. Which probably not always makes sense to others. I'm also known to use n and m. Usually when I want to convey limits on dimensions or sizes
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User Rennor's Avatar
    Join Date
    Aug 2006
    Location
    Finland
    Posts
    45
    As a beginner I used to use x mostly and then y and z. When starting to write some 2D gfx effects those quickly changed to i, j and k since x,y and z are for coordinates

    Then the mixture was complete, sometimes i, j and k were just for looping, sometimes they represented "temporary" variables where x = i, y = j and z = k.

    Go figure.

    Nowadays I use only i when I try out something quickly, if I need more variables they usually come out with proper names. Being professional and responsible for quality makes me automatically name everything and comment them properly according to our styleguides

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by swgh
    Am I correct in the face programmers use I J and K as variable names for for loops since the FORTRAN language? Was the language designed to accept only these variables?
    By my thinking, you are correct. In FORTRAN, variables that begin with the letters I thru N are by default of type INTEGER. Variables that begin with any other letter are by default REALs. So you can see how this would have led to using I, J, and K for for-loop indices.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I usually use I, N and E. I don't know why.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I use x, y, and z (and a, b, c etc if required). If I need to use coordinates, I use xp, yp, etc (for X-position, Y-position). I don't know why, either.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random variable names
    By NeMewSys in forum C Programming
    Replies: 7
    Last Post: 06-12-2008, 11:26 AM
  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. Random Variable names
    By Milhas in forum C Programming
    Replies: 14
    Last Post: 03-26-2008, 05:03 PM
  4. How to manage variable and function names
    By jefflieu in forum C++ Programming
    Replies: 14
    Last Post: 02-07-2008, 03:33 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM