3 ms·
Whether an array index should start at 0 or 1, and whether loop index range intervals should be open, half-open, or closed are long-standing religious debates a
by randcraw 10y ago
Whether an array index should start at 0 or 1, and whether loop index range intervals should be open, half-open, or closed are long-standing religious debates among programmers. It seems everyone has a preference.
It's all about each language's default semantics -- what can and should be left unsaid, and how that expresses the language's model for array access, pointer de/referencing, and loop control.
For array indexing, the most accommodating syntax is to do what Fortran does: allow the programmer to explicitly specify each array index's closed array range. That may be the most user-tolerant and unambiguous model for arrays, but it doesn't work well if you also want the same syntax to dereference pointers, whose indices must start at 0. Thus array indices of pointer-based languages (always?) start at 0. But that annoys the hell out of folks who work extensively with arrays AND real numbers, where half-open 0-based integer index ranges compel clumsy semantics when iterating over their precious arrays. (Folks like me, BTW.)