4

I am curious if there are common loop index variables (of single character or not) for dealing with 4 dimensions and above?

I was helping another student working through CS50x who was just learning about loops and she asked more or less "if x,y,z are standard for 3D axises with indexes i,j,k , respectively, what if there was a 4D matrix?" I guessed it would be "i,j,k,l" with x,y,z,w , (general form w-- and l++ for 5D etc) but I wanted to check what other people thought. I realise that at that point, it is uncommon enough that there isn't really any "standard", or complex enough that that the loops should be either simplified or the variables named better.

That said, are there "common" loop variable names for indexes in 4D and above?


Similar questions that I found that didn't quite answer this: 1. using-single-characters-for-variable-names-in-loops-exceptions 2. why-do-most-of-us-use-i-as-a-loop-counter-variable

5

Something meaningful? Nesting lots of meaningless loop variables quickly becomes unreadable.

Imagine seeing something like this embedded inside four levels of loops:

do_stuff(i,j,k+4, l*i)

and trying to understand what it does vs something like:

do_stuff(row, col, height + 4, function_value * row)

This is a simple example. A lot of this sort of logic will be much more complicated than my simple example.

Additionally, it makes writing your loop definitions more clear. Something like:

for row in range(0, max_rows):
   for col in range(0, max_cols):
       for height in range(0, max_height):

is much more clear when you are looking at the loop without familiarity with it. This is even more important the more complex the logic of your loop(s) are.

You can "get away" with poorly named iterators in single or even doubly nested loops because it's trivial to conceptually parse. This is not the case when you have 4 layers of nested loops.

Note that even x, y, and z are useful names for iterators. But i, j, k? Those are not and if you have a complex loop you will require yourself (or others) to have to "translate" them.

  • The_"translate" them_ part is a good point. It creates unnecessary mental work. – MathFromScratch May 17 '16 at 17:29
4

If there are four or more dimensions, then they represent something more than just cartesian (or other) co-ordinates, so just use a (short) word that signifies what each level actually is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.