Why programmers should always start counting from zero

Felipe Ribeiro
2 min readJan 30, 2015

--

When you want to perform the same action n times, there are two common ways of writing the loop:

for (int i = 0; i < n; i++) {
// do stuff
}

or

for (int i = 1; i <= n; i++) {
// do stuff
}

And both work, but why is the first one better? (besides the obvious fact that it matches with the iteration on zero-based array indices).

  1. Counting from zero encourages us to use asymmetric ranges to express intervals. [0, rows) instead of [1, rows] and that’s easier to use because [m, n) has n-m elements, while [m, n] has n-m+1. So, the number of elements in [0, rows) is obviously rows-0=rows but in [1, rows] it is not as obvious.
    And as we all know, there are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.
  2. Asymmetric ranges represent better an empty interval: you would use [n, n) instead of [n, n-1] to do it.
  3. It makes easier to express loop invariants:
// loop invariant: i lines have been processed
for (int i = 0; i < rows; i++) {
// do stuff..
}
// loop invariant: i lines have been processed

--

--

Felipe Ribeiro

Staff Software Engineer @ Google, previously @ Netflix and Spotify. Dad of two, road cyclist wannabe and citizen of the world.