tags:

views:

661

answers:

5

Count up and count down seems no difference, we can implement either one as we like. But actually what is the advantage for using count up and count down. what is the difference between them (besides that one is increment, another one is decrement)?

+3  A: 

Counting up is a lot harder than counting down. Which is easier - running up a hill or running down a hill?

mrnye
Have you considered the psychological angle of running up the hill backwards? Even though you're going up hill, your brain thinks your going down!
Dave Barker
+14  A: 

If you're counting down in C-like language, you can use this cute bit of syntax:

int n = 10;
while (n --> 0) {
    ...
}
jleedev
+1  A: 

If you know what the end is, then maybe countdown makes more sense - the current count tells you how many there are left to do. On the other hand, count up also works perfectly well, with the current count telling you how much you've done.

If you don't know where the end is, then count-up makes a lot more sense.

In terms of general efficiency, there isn't a lot to choose.

If you use unsigned integers, then the countdown has a gotcha if you decrement zero.

Jonathan Leffler
+1  A: 

Taking a SWAG on the question here:

Counting in a traditional for-loop requires the condition to be checked on every iteration.

for (i = 0 ; i < lengthof(loooong_list) ; i++)
{ 
print(loooong_list[i])
}

requires you to calculate the length of the list each time. And since lengthof is assumed to be an O(n) operation executed n times, the loop turns into an O(n^2) operation.

However,

for (i = lengthof(loooong_list) - 1 ; i >= 0 ; i++)
{ 
print(loooong_list[i])
}

is an O(2n) operation because you calculate the length of the list once, and iterate over it, printing its values, once.

The downside to the second approach is that it's not always acceptable to iterate in reverse; however, it's easy enough to store the length in another variable and subtract, in order to iterate forwards again.

Mark Rushakoff
A good practice is to store the list size separately BEFORE the loop begins:<pre>l = lengthof(loooong_list);for (i = 0 ; i < l ; i++){ print(loooong_list[i]);}</pre>
FrustratedWithFormsDesigner
or for (i = 0, n = length_of(loooong_list); i < n; i++) ...
erikkallen
+1  A: 

If you are talking about loops indexes and whether to count the index variable up to a limit or down to a limit, then I've always taken these two points into consideration:

  1. Counting up always makes for more readable code.
  2. Counting down is ever so slightly more efficient if your limit is zero and you use a integer decrement of 1. (at least is used to be)

Compilers are getting smarter year by year, so it might no longer be the case in general.

Nicholas
Recent Java VMs can still optimize the count-to-0 case better. However *counting up always makes more readable code* is kind of silly without better context -- e.g. perhaps the idea was to go "backwards".
pst
Fair point. If the context is naturally downwards you are probably right. I find such context uncommon, but that could just be me.
Nicholas