# Why programmers should always start counting from zero

Jan 30, 2015 · 2 min read

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).

- Counting from zero encourages us to use asymmetric ranges to express intervals.
instead of*[0, rows)*and that’s easier to use because*[1, rows]*has*[m, n)*elements, while*n-m*has*[m, n]*So, the number of elements in*n-m+1.*is obviously*[0, rows)*but in*rows-0=rows*it is not as obvious.*[1, rows]*

And as we all know,*there are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.* - Asymmetric ranges represent better an empty interval: you would use
instead of*[n, n)*to do it.*[n, n-1]* - 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