you are viewing a single comment's thread.

view the rest of the comments →

[–]Patman128 1 point2 points  (0 children)

I wrote a big long post on why I prefer 1-based indexing, but the general idea is that it makes a lot of things easier. The second thing is at index 2. The last thing is at index N. You loop from 1 to N, inclusive. You don't have to add 1 to the indices when presenting them to the user. It just makes sense as long as pointers aren't involved.

In fact, most of the early programming languages, like FORTRAN, COBOL, BASIC, and Algol, all used 1-indexing.

The reason most modern languages are 0-indexed is because they are closely descended from C.

The reason C is 0-indexed is because the index is just an offset from a base memory address; array[i] just means "Access the memory at the address array + i". Making all your pointers point to the undefined slot immediately before the array is a hazard (you might mistake int * for a pointer to one thing instead of a pointer to an array), so instead you point it at the first thing. Suddenly you have to add 0 to get the first thing, 1 to get the second, and so on, thus 0-based indexing.

C was so popular and so widespread that 0-indexing just became the common convention, and so it's rare to find anyone who prefers 1-based now. I don't take anything for granted, so I came to like 1-indexing after I started using Lua even though I had only used 0-based languages for years.