Thread: Natural Loop Varible

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Natural Loop Varible

    When I write a loop, I always naturaly use the varible n.a. I know it seems wierd but quite a while ago, I wrote
    Code:
    struct LOOP { int a, c, d, e, f, g; }; LOOP n;
    in my program for loops becuase I already had a being used as a string, and now it's stuck with me, and I have and use it in practicly ALL my programs. Whats your natural loop varible?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not to be egotistical or anything, but it is i.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    i. if it's nested, j k l etc.

    If it's an iterator I use some name to indicate it's an iterator to a type of data structure

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    index, callNum, session, etc depending on object type I'm iterating
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    i, j, k, l for loops. iter, iter2, iter3 for iterators.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    x, y, z and then sometimes a, b etc or rarely i and j. (Usually I don't have that many loops though.)

    I must say that Queatrix's loop variables are probably unique.

    There was a thread much like this a while ago . . . maybe it got lost in the board restore.
    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.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    i normally. However, depending on the order of nesting it will start with k or j. Then it goes into ii, ij, ik, ji, jj, jk ... I think ye get the idea.

    I made that up. Sorry.

    i, j, k .... I use single letter variables for my iterator thingys, or a c for a colouring class, m for a menu class, and a d for a default class. To be honest if I have to use i, j, k, l (here I would run into problems), and one more I should probably have to rethink the code I'm writing.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    i used to use count, count2, count3, etc..
    but that took too long to type
    i changed to i and j, i never nested more than 2 for loops since that change

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    i, j, k. Very rarely will I do other index variables for loops, at least not for for loops.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would use i, j, k if there was no context or for examples, but that rarely happens. I find myself using index a lot since most of my integer for loops involve indexes in an array-style container. I also use current_[object name] for iterators. Anything else I try to give explicit variable names as well, just like any other variable.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    i for single for loops.

    j,k,l for nested for loops.

    index, offset for non-for loops.

  12. #12
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Always i for single, j,k,l for nested (I've never gotten to m, thankfully).
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    There was a thread much like this a while ago . . . maybe it got lost in the board restore.
    That was my thread. It was a little while back. I asked if i j and k variable names were taken from the Fortran language. I think Prelude said I was correct, but all variables have been this way. Hmm, I remember using short variable names when I used to code in BASIC for the Acorn systems back in the early 90's. I miss this:

    Code:
    LET A = 200
    ahh, memories... :P
    Double Helix STL

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Usually i, j, k... although many times I'll use loop, loop1, loop2, etc..
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Using i, j, k as index variables is older than programming. Those are the standard indexing variables for math proofs as well.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM