My experience is that most people use single letters, e.g.:
i,
j,
k,
...
or
x,
y,
or
r,
c (for row/column)
or
w,
h (for width/height)
, etc.
But I learned a great alternative a long time ago, and have used it ever since: double letter variables.
// recommended style ● // "typical" single-letter style
●
for (ii=0; ii<10; ++ii) { ● for (i=0; i<10; ++i) {
for (jj=0; jj<10; ++jj) { ● for (j=0; j<10; ++j) {
mm[ii][jj] = ii * jj; ● m[i][j] = i * j;
} ● }
} ● }
In case the benefit isn't immediately obvious: searching through code for any single letter will find many things that aren't what you're looking for. The letter i occurs quite often in code where it isn't the variable you're looking for.
@JustMike . . . A FEW C EXAMPLES: . . . to accompany the Java ones.
NON-NESTED loop: . . . limiting scope where possible
/*LOOP_DESCRIPTION*/ {
int i;
for (i = 0; i < LOOP_LENGTH; i++) {
// loop body
}
}
NESTED loop: . . . ditto
/*LOOP_DESCRIPTION*/ {
int row, column;
for (row = 0; row < ROWS; row++) {
for (column = 0; column < COLUMNS; column++) {
// loop body
}
}
}
One good thing about this layout is it reads badly without comments, thereby encouraging them.
It's verbose perhaps, but personally this is how I do loops in C.
Also: I did use "index" and "idx" when I started, but this usually got changed to "i" by my peers.
for numerical computations, matlab, and the likes of it, dont use i, j
these are reserved constants, but matlab wont complain.
My personal favs are
index first,second counter count