Natural Loop Varible

This is a discussion on Natural Loop Varible within the A Brief History of Cprogramming.com forums, part of the Community Boards category; For a single loop I either use i or x. I use x fairly often, especially when iterating an array, ...

  1. #16
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    For a single loop I either use i or x. I use x fairly often, especially when iterating an array, because I often think about it as if I am iterating over the "x-axis". Consequently, with matrixes, I often use x and y. I guess this comes from doing a lot of work with graphics. z is also used if it is nested another time.

    Other than that I use i, j, and k fairly often as well. I usually kind of alternate between x,y,z and i,j,k.

    When a LOT of loops are in order...I will go off to l, n, p, a, b, and c.

    With iterators, usually something like iter, iter1, iter2, or maybe myIter, myIter1, myIter2. I tend to put the prefix of "my" on a lot of variables...it is a thing that has rubbed off on me since high school when we would work a lot with the "AP classes" for strings, vectors, and matrixes...and they named their variables like that.
    My Website

    "Circular logic is good because it is."

  2. #17
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I've tried to get away from single-letter identifiers, especially if I've got nested loops. On the first code-writing pass, I'll usually use an i, but it will quickly get replaced by something more descriptive. It's a lot easier to suddenly see logic errors (why in the world am I adding those two indexes together?) when you don't have to juggle a mental key around in your head to remember which index goes to which loop.
    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

  3. #18
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I use iter for iterators too

  4. #19
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Wow... 17 posts about a loop variable... *cry*
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  5. #20
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    index

    then endex for nested. it's confusing, but i have to do it. i think i may try using i-j-k-l-m, though

  6. #21
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    If I'm using iterators I usually name them "begin" and "end".
    And HAI SENTRAL!!!
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #22
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Many iterators are not indices, in which case the loop variable names i, j, k don't make sense. If using a loop to iterate through each element in a container, then I avoid using an index if possible, and instead name the iterator curr_type. i, j, k ideally only comes up if I'm actually doing something numerical.


    Code:
    for (curr_node = begin; curr_node != NULL; curr_node = curr_node -> next);
    Code:
    for (curr_char = string_name; *curr_char != '\0'; ++curr_char);
    Callou collei we'll code the way
    Of prime numbers and pings!

  8. #23
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Arrgh! Loop variables should be declared in the for-construct! Using "n.a" and weird constructs like that really cripples the compiler's ability to optimize your code.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #24
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I never thought about iterators, but now that it's mentioned I use n for them too. Though I must admit it gets confusing some times when I start crossing indexes and iterators like this:
    Code:
    loop: n.a
    iterator: n.b
    nested loop: n.c
    nested loop: n.d
    iterator: n.e
    nested loop: n.f

  10. #25
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Might I ask why you choose to stray from the i, j, k "convention"? Your method seems like extra overhead and added obfuscation for no real gain.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #26
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I really don't know, I guess it's just one of my many unexplainable habits.

    And I don't have to worry about it interfiring with "imported" *ahem* code.
    Last edited by Queatrix; 04-04-2007 at 11:05 PM.

  12. #27
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    M'kay. I only bring it up because in a number of situations if you showed me that in a job interview, I'd consider you in need of re-training, and a lesser applicant -- unless you could explain a bit better than Idunno. If that's a habit you want to keep, that's your business.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #28
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by DavidP View Post
    For a single loop I either use i or x. I use x fairly often, especially when iterating an array, because I often think about it as if I am iterating over the "x-axis". Consequently, with matrixes, I often use x and y. I guess this comes from doing a lot of work with graphics. z is also used if it is nested another time.

    Other than that I use i, j, and k fairly often as well. I usually kind of alternate between x,y,z and i,j,k.

    When a LOT of loops are in order...I will go off to l, n, p, a, b, and c.

    With iterators, usually something like iter, iter1, iter2, or maybe myIter, myIter1, myIter2. I tend to put the prefix of "my" on a lot of variables...it is a thing that has rubbed off on me since high school when we would work a lot with the "AP classes" for strings, vectors, and matrixes...and they named their variables like that.
    this is exactly what I'm doing too

Page 2 of 2 FirstFirst 12
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, 11: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, 11:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03: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

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