PDA

View Full Version : Natural Loop Varible



Queatrix
04-02-2007, 09:01 AM
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
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?

laserlight
04-02-2007, 09:06 AM
Not to be egotistical or anything, but it is i.

indigo0086
04-02-2007, 09:42 AM
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

vart
04-02-2007, 09:54 AM
index, callNum, session, etc depending on object type I'm iterating

Happy_Reaper
04-02-2007, 10:12 AM
i, j, k, l for loops. iter, iter2, iter3 for iterators.

dwks
04-02-2007, 11:20 AM
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.

twomers
04-02-2007, 11:40 AM
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.

h_howee
04-02-2007, 05:05 PM
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

MacGyver
04-02-2007, 05:07 PM
i, j, k. Very rarely will I do other index variables for loops, at least not for for loops.

Daved
04-02-2007, 05:08 PM
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.

VirtualAce
04-02-2007, 10:58 PM
i for single for loops.

j,k,l for nested for loops.

index, offset for non-for loops.

cboard_member
04-03-2007, 03:11 AM
Always i for single, j,k,l for nested (I've never gotten to m, thankfully).

swgh
04-03-2007, 05:18 AM
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:


LET A = 200 ahh, memories... :P

hk_mp5kpdw
04-03-2007, 05:32 AM
Usually i, j, k... although many times I'll use loop, loop1, loop2, etc..

QuestionC
04-03-2007, 08:30 AM
Using i, j, k as index variables is older than programming. Those are the standard indexing variables for math proofs as well.

DavidP
04-03-2007, 10:45 AM
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.

pianorain
04-03-2007, 11:28 AM
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.

twomers
04-03-2007, 11:53 AM
I use iter for iterators too

Sentral
04-03-2007, 01:20 PM
Wow... 17 posts about a loop variable... *cry* :(

divineleft
04-03-2007, 01:37 PM
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

cboard_member
04-04-2007, 06:11 AM
If I'm using iterators I usually name them "begin" and "end".
And HAI SENTRAL!!! :)

QuestionC
04-04-2007, 07:49 AM
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.



for (curr_node = begin; curr_node != NULL; curr_node = curr_node -> next);

for (curr_char = string_name; *curr_char != '\0'; ++curr_char);

Sang-drax
04-04-2007, 03:18 PM
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.

Queatrix
04-04-2007, 09:09 PM
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:


loop: n.a
iterator: n.b
nested loop: n.c
nested loop: n.d
iterator: n.e
nested loop: n.f

Dave_Sinkula
04-04-2007, 09:16 PM
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.

Queatrix
04-04-2007, 10:02 PM
I really don't know, I guess it's just one of my many unexplainable habits. :D

And I don't have to worry about it interfiring with "imported" *ahem* code. :D

Dave_Sinkula
04-04-2007, 10:24 PM
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.

KONI
04-05-2007, 12:49 AM
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 :)