views:

50

answers:

6

Hi experts.

This is the example:

for(int i = 0; i < 10;i++)
{
   for(int ? = 0;? < 10 ; ?++)
   {
   }
}

I usually use an "o" for the second loop, but is there any standard out there?

ideas? Thanks.

+1  A: 

When I was in school we always used j, but I don't believe there is a standard. If this is your own project use whatever you want and be consistent. If this is a company project follow the standard set by your Development Standards Document (You do have one?)

Waleed Al-Balooshi
I'm trying to define one :P
Fraga
+2  A: 

Logically, you would use 'j', but the best is to use something meanlingful, like 'row' and 'column' if you can. If you feel like joking, use 'c' or 'notepad'

greg0ire
A: 

Standard? No. i,j,k,l,m,n are popular (probably a throwback to the old FORTRAN rules).

However, a word of advice, don't use single character variables even for tiny iterate loops. The reason? When that little loop grows up and encompasses 20 or 30 lines of code and someone decides to refactor, finding all this 1 character variables names is going to suck. Someday, later in life, you'll thank me :)

KevinDTimm
So meaningful names, otherwise Index or InnerIndex but never sigle characters... that sounds good, but hard to type :P
Fraga
even something as simple as iItr, jItr, kItr is easily replaceable.
KevinDTimm
A: 

If I need to nest loops I try to find meaningful names for them, otherwise it gets very confusing very quickly. If there really is no meaningful name I'd often name them Index and InnerIndex so that it's immediately obvious which is which.

ho1
A: 

I'm using:

  'k'

My teachers used 'j', because it was the first letter after 'i'. But I found it unnecessary harder to read. But the next letter 'k' is much easier to see. Use 'k' and speed up your reading.

for(int i = 0; i < 10;i++)
{
    for(int k = 0; k < 10 ; k++)
    {
    }
}
radbyx
A: 

I use hungarian "k" plus whatever physical meaning the counter is supposed to be.

for (int kRow = ...)
{
  for (int kCol = ...)
  {
  }
}

Sometimes I will use k,m,n,p,q,r,s,t,w,x,y,z if the index means nothing more than permutations (i.e. they all have exactly the same physical meaning).

I usually don't use i,j for the same reason electrical engineers don't use i to denote imaginary number.

rwong