Take the 2-minute tour ×

Throughout all tutorials and books I've read whilst learning programming the practice for iterating through arrays has always been with for loops using:

int len = array.length();
for(int i = 0; i < len; i++) {//loop code here}

Is there a reason why people don't use

for(int i = length(); i > -1; i--) {//loop code here}

From what I can see the code is shorter, easier to read and doesn't create unnecessary variables. I can see that iterating through arrays from 0 to end may be needed in some situations, but direction doesn't make a difference in most cases.

share|improve this question
length() what exactly is that? Where is his parent?!? Call the truancy officers! –  Pinch Oct 16 '12 at 20:35
I assume he meant array.length() –  Rodolfo Oct 16 '12 at 20:35
Do you think in negatives? –  Pinch Oct 16 '12 at 20:38
In a nutshell - because in general it doesn't really matter any more. –  Preet Sangha Oct 16 '12 at 20:41

4 Answers 4

First reason is how you think about it. Generally, as humans, we do iterate from the beginning to the end, not the other way around.

Second reason is that this syntax stems from languages like C. In such languages, going from the end to the beginning was downright impossible in some cases, the most obvious example being the string (char*), for which you usually were supplied with the start address and you had to figure out its length by enumerating until you found 0.

share|improve this answer

Depends on the platform you use. In Java for instance, compiler optimization is so good, I wouldn't be surprised if it doesn't make any noticeable difference. I used to benchmark all kinds of different tricks to see what really is faster and what isn't. Rule of thumb is, if you don't have strong evidence that you are wasting resources, don't try to outsmart Java.

share|improve this answer

direction doesn't make a difference in most cases

True, in many cases you'll get the same result in the end, assuming there aren't any exceptions.

But in terms of thinking about what the code does, it's usually a lot simpler to think about it from start to finish. That's what we do all the time in the rest of our lives.

Oh, and you've got a bug in your code - you almost certainly don't want length() as the initial value - you probably want array.length() - 1 as otherwise it starts off being an invalid index into the array (and that's only after fixing length() to array.length()). The fact that you've got this bug demonstrates my point: your code is harder to reason about quickly than the code you dislike.

Making code easier to read and understand is much, much more important in almost every case than the tiny, almost-always-insignificant code of an extra variable. (You haven't specified the language, but in many cases in my own code I'd just call array.length() on every iteration, and let optimization sort it out.)

share|improve this answer

The reason is readability. Enumerating from 0 and up makes most sense to the most of us, therefore it is the default choice for most developers, unless the algorithm explicitly needs reverse iteration.

Declaring an extra integer variable costs virtually nothing in all common programming platforms today.

share|improve this answer

Your Answer


