Why does counting start from 0?

Question

Why does counting start from 0?

Answer

In short, just remember that almost all programming languages start counting from zero because it’s optimal.
A famous Dutch computer scientist, Edsger Dijkstra, gives a great mathematical explanation for this! It basically comes down to optimization, which is a common reason behind computer system design choices like this. Dijkstra says, “when starting with subscript 1, the subscript range 1 ≤ i < N+1; starting with 0, however, gives the nicer range 0 ≤ i < N,” in his paper called “Why numbering should start at zero.”

7 Likes

2 posts were split to a new topic: How to Print the Length of a String?

Dijkstra’s algorithm solves the single-source shortest path problem with non-negative edge weight.

AppleScript is one language I know that doesn’t start at “0” which messes with a lot of scripters. Then again, AppleScript is a scripting language not a code language.

For reference:

Zero-based numbering - Wikipedia.

The paper mentioned in the OP is discussed in that article along with exploration into other fields. A good read.

2 Likes

R isn’t zero-indexed either.

1 Like

When we use index/indeces in programming languages, we cannot compare it with (or refer to it as) counting. It is more of an ordinal number. Most programming languages refer to the 1st character in a string with index 0, first element in an array also using 0 index.

Just a trivia, all numbering systems start with 0. But we never count starting at 0, because its equivalent to nothing, we start with 1.

Decimal system has 10 figures/characters used for its numbers - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Binary system has 2 - 0, 1
Octal system has 8 - 0, 1, 2, 3, 4, 5, 6, 7
Hexadecimal has 16 - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

All these numbering systems has 0 as their first number.

1 Like