Language / Technology

Why do programs sometimes start counting at zero?

The short answer

If the first element is distance zero from the start, the index directly represents how many positions have been moved.

How it works

The third element is two positions after the start, so its zero-based index is 2. Some languages or APIs use one-based numbering, so check the specification.

A familiar example

Human ordinal numbers and program indexes may differ, causing off-by-one bugs.

Sources