
Your first array element is item zero. Not one. Zero. Zero-indexing in programming means an array's first element lives at index 0, its second at index 1, and its last at length minus one. Python, Java, JavaScript and C all work this way.
It looks like an odd habit. It isn't. It comes from one small idea about memory: an index is a distance from the start, not a place in line. Once that idea clicks, a whole family of crashes stops being mysterious. This article follows that idea from memory addresses, through BCPL and C, into the loops you write every day.
- Zero-indexing means the first element of an array is at index 0, and the last is at length − 1.
- An index is an offset: how many steps to move from the start of the array.
- Computers find any element with one formula: address = base + i × size.
- The first element is zero steps from the base, so its index is zero, with no extra math.
- Loop with i < length, never i <= length, or you read one slot past the end.
- 0:00Intro
- 0:20One step too far crashes
- 0:59The first element is index 0
- 1:35An index is a distance
- 2:11One contiguous block of memory
- 2:47Address = base + i × size
- 3:24Zero steps means index zero
- 4:00From BCPL to modern languages
- 4:31Walk a loop through memory
- 5:08Where zero bites back
- 5:44Zero-indexing in six lines
- 6:08An index is an offset, not a count
Why does my loop crash on the last pass?
It happens to everyone. Your loop runs fine, pass after pass, then dies on the very last one. The clues are right there in the code: a condition like for i <= length, followed by an ArrayIndexOutOfBounds error. The loop went one step too far.
When i equals the length, you're already one slot past the final element. Java throws an exception. Python raises an IndexError. C doesn't stop you at all and just reads whatever memory sits there. Get the starting point wrong and your code walks off the end of the array. Understand where zero comes from, and this bug becomes easy to spot.
- Index equal to length = one slot past the last element
- Java: ArrayIndexOutOfBounds
- Python: IndexError
- C: reads whatever memory comes next
What is zero-indexing?
Zero-indexing is simple to state: an array's first element is at index 0, not index 1. Take a Python list of three colours. items0 gives red, the first. items2 gives blue, the last. Ask for items3 and Python raises an IndexError, because there is no fourth slot.
Two rules follow. First is zero, every time. Last is length minus one, so three items means the last index is 2. That gap between how many items you have and the highest index you can use is exactly where bugs hide. It isn't a quirk of one language either. Python, Java, JavaScript and C share the same rule.
- First element: index 0
- Last element: index length − 1
- Index equal to length: out of bounds
items = ["red", "green", "blue"]
items[0] # 'red' (the first)
items[2] # 'blue' (the last)
items[3] # IndexError
Why is an array index a distance, not a position?
Here's the key idea. An index isn't a position in a line. It's a distance from the start. Put the two numbers side by side: positions go first, second, third, fourth. Steps away from the start go 0, 1, 2, 3. The index is simply that second number.
Think of a ruler. Its first mark is 0, because you haven't moved yet. One centimetre means one step from the start. Arrays work the same way. A count asks how many items there are. An offset asks how far to jump. Arrays only care about the jump, and the first jump is no jump at all.
- Position: 1st, 2nd, 3rd, 4th
- Steps away: 0, 1, 2, 3
- Index = steps away
How are arrays stored in memory?
So where does that distance come from? An array is one contiguous block of memory. Your variable, say scores, points to one thing: the base address where the block begins. Slot 0 sits right there. Slot 1 follows immediately. Then slot 2. No gaps.
Contiguous means every element sits right next to the previous one, and every element is the same size. Nothing is scattered across memory. That matters, because the computer doesn't need a map of every element. It only needs to remember where the block starts. Everything else can be calculated.
How does a computer find an array element?
With one line of arithmetic: the address of element i equals the base address plus i times the element size. Plug in real numbers. Say scores holds four ints starting at address 1000, four bytes each. Then scores2 lands at 1000 + 2 × 4, which is 1008.
That's one multiply and one add. Finding element 0 costs the same as finding element 1,000,000, which is why arrays give instant access to any slot. Notice what i does in the formula. It only scales the step size. It's pure distance. So what happens when i is zero?
# address of element i
addr(i) = base + i * size
# int scores[4]; base 1000, size 4
scores[2] -> 1000 + 2 * 4 = 1008
Why do arrays start at 0 instead of 1?
Run the formula for the first element and watch the offset vanish. Base address: 1000. i × size: 0 × 4 = 0. Add the offset: 1000 + 0 = 1000. The first element lives at the base address itself, zero steps away. So its index is zero.
Now try starting at one. The formula becomes base + (i − 1) × size. That's an extra subtraction on every single access, or a wasted slot at the front of every array. Counting from zero makes the index and the offset the same number. The math stays clean, and nothing needs correcting.
- Zero-based: address = base + i × size
- One-based: address = base + (i − 1) × size
- Zero-based means index and offset are identical
Where did zero-indexing come from?
Zero-indexing was inherited from BCPL, where an address plus an offset found your data. C cemented it: indexing in C is pointer arithmetic, so the index is literally the offset from the start. Python, Java and JavaScript then kept zero as the default.
The argument for it came later. Edsger Dijkstra made the case that starting at zero gives cleaner ranges and simpler math. What began as a hardware habit became a principle, and it now runs through almost everything you write.
- BCPL: address plus offset
- C: indexing as pointer arithmetic
- Python, Java, JavaScript: zero by default
What happens inside a loop over an array?
Take a plain loop over four scores, the way you'd write it in Java or C: start at i = 0, continue while i < 4. Each pass turns an index into an address. Index i goes 0, 1, 2, 3. The offset is i × 4 bytes: 0, 4, 8, 12. The address is 1000 plus that offset: 1000, 1004, 1008, 1012.
The loop runs exactly four times and ends at index 3. Notice the condition compares against the length, not the last index. And there's no wasted math. Every pass is just a multiply and an add, with nothing to correct.
- Index: 0, 1, 2, 3
- Offset: 0, 4, 8, 12
- Address: 1000, 1004, 1008, 1012
Common zero-indexing mistakes and how to avoid them
Zero-indexing is simple, but three mistakes keep tripping people up. The first is the classic loop bug: i <= length runs one pass too many and reads a slot that doesn't exist. The fix is strictly i < length. One character, one crash avoided.
The second is the general off-by-one error, where you treat a count like an index. Ask yourself: am I counting, or measuring distance? The third is switching languages. Lua, MATLAB and R start counting at one. Move between them and Python, and your habits will betray you. Check the convention first.
- Loop with i < length, not i <= length
- Don't use a count where an index belongs
- Lua, MATLAB and R are 1-based
# bug: one pass too many
for (int i = 0; i <= a.length; i++)
# fix: stop before length
for (int i = 0; i < a.length; i++)
The bottom line
- An index is a distance from the start, not a place in line.
- An array is one contiguous block starting at a base address.
- Any element's address is base + i × size.
- The first element is zero steps away, so its index is 0.
- Zero-indexing passed from BCPL to C to Python, Java and JavaScript.
- Always loop with i < length.
Quick-fire questions
Why do arrays start at 0 and not 1?
Because an index measures distance from the start of the array. The first element is zero steps from the base address, so its index is 0. Starting at 1 would need an extra subtraction on every access or a wasted slot.
What is the last index of an array?
The last index is the length minus one. An array of three items has indexes 0, 1 and 2. Asking for index 3 is out of bounds.
Why does i <= length cause an error?
When i equals the length, it points one slot past the last element. Java throws ArrayIndexOutOfBounds, Python raises an IndexError, and C reads whatever memory sits there. Use i < length instead.
Which programming languages are zero-indexed?
C, Python, Java and JavaScript all start counting at zero. The convention was inherited from BCPL and cemented by C.
Which languages start counting at 1?
Lua, MATLAB and R use 1-based indexing. If you switch between them and a zero-based language like Python, check the convention before you write your loops.
What is an off-by-one error?
It's a bug where you mix up a count and an index by exactly one, such as looping one pass too many. The quick check is to ask whether you're counting items or measuring distance from the start.