DEV Community

Allen Joseph
Allen Joseph

Posted on

1-based vs 0-based array indexing

image
How many 0-based indexing languages do you know? C,C++,C#….. I’m sure you can name a lot of languages which offer 0-based indexing.
Now if I rephrase the question and ask the same, How many 1-based indexing languages do you know?. I’m sure that you can’t name a lot of them.

“​ FORTRAN arrays are 1-primarily based in contrast to many other programming languages..​ ”

image

There are relative merits between 0-based and 1-based indexing of arrays​.

1-based indexing is actual indexing like in arithmetic.
0-based “indexing” isn’t indexing in any respect however pointer arithmetic.

1-based is intuitive and saves your intellectual ability.
0-based indexing is one in every of many mistakes of programming.

For any experienced developer, 0-based programming must not be an issue where as for a novice programmer 0-based indexing might be a big issue.

People have different opinion on which type of indexing is better.
For instance Dijkstra in his article “Why numbering need to start at 0“, argues zero ≤ i < N is a “nicer” variety than 1 ≤ i < N+1 and doesn’t actually care for 1 ≤ i ≤ N. This certainly doesn’t make an awful lot feel — there are not any aesthetic traits associated with conditional statements.
People’s Opinion?
Link

image
image
image

Conclusion

So you must be wondering which one is better?
Well, some people like 0-based indexing while others like 1-based indexing. There might not be a right answer to this question.
Both types of indexing are done to get the same outputs regardless of the type of indexing.

Top comments (0)