top | item 2680877

(no title)

seanx | 14 years ago

I have come to the conclusion that 0 based indexing is basically building "off by one errors" as a language feature.

The fact that for(int i = 0; i < length; i++) is nicer than for(int i = 1; i <= length; i++) is irrelevant. The c style for loop is designed to make the complex case easier while making the common case harder. It also makes some compiler optimisations impossible

A better for loop would be something like for(int i = start to end)

However with 0 based indexing you end up with e.g in delphi:

for i := 0 to count-1 ... which is an open invitation to error.

In other words, the for loop construct should not be used as an argument for or against 0 based indexing, it should be derived from the indexed used.

discuss

order

william42|14 years ago

C starts at 0 because of pointer arithmetic.

And the proper for loop is "for x in n".