Summary of Edsger Dijkstra’s notes Why Numbering Should Start at Zero.
Range convention
There are 4 conventions to denote a range of natural numbers a, a + 1, …, b:
- a ≤ i < b + 1.
- a − 1 < i ≤ b.
- a ≤ i ≤ b.
- a − 1 < i < b + 1.
Excluding the lower bound like in range conventions 2 and 4 gives a lower bound of −1, which is not a natural number, to denote a range starting at 0 (convention 2: −1 < i ≤ b; convention 4: −1 < i < b + 1). Including the upper bound like in range conventions 2 and 3 gives a negative upper bound, which is not a natural number, to denote a range starting at 0 that has shrunk to the empty range (convention 2: −1 < i ≤ b, where b < 0; convention 3: 0 ≤ i ≤ b, where b < 0). Including the lower bound and excluding the upper bound like in range convention 1 gives a lower bound of 0, which is a natural number, to denote a range starting at 0 (0 ≤ i < b + 1), and an upper bound of 0, which is a natural number, to denote a range starting at 0 that has shrunk to the empty range (0 ≤ i < 0). So range convention 1 should be preferred.
Index convention
There are 2 conventions to index n elements:
- Starting at 0.
- Starting at 1.
Starting at 1 like in index convention 2 gives the index range 1 ≤ i < n + 1, following the preferred range convention. Starting at 0 like in index convention 1 gives the index range 0 ≤ i < n, following the preferred range convention, which is nicer. So index convention 1 should be preferred.