Peering into the programming world can be like trying to decipher a code. Thankfully, I’m here to answer your burning questions like, why do programmers count from zero?
Programmers count from zero because computers count from zero. They are not robots. Arrays are common programming tools that utilize a series or list of data. When a computer recalls an item from an array, the computer starts counting from zero.
Unfortunately, programming concepts can be difficult to fully understand from one paragraph of text. Thankfully, I’ll be diving deeper into the topic of arrays in the rest of this article to give you a full understanding of why programmers count from zero.
Table of Contents
Why Computers and Programmers Count From Zero
Allow me to clear up any confusion you have about arrays. I find the best teaching method for arrays is to write out an example, so here is an example array of some of my favorite foods:
[‘Pizza’, ‘Burrito’, ‘Mac N Cheese’, ‘Caesar Salad’]
If either of us were to count this list, we would assign ‘Pizza’ a value of 1 and ‘Caesar Salad’ a 4. Our numbered list would look like this:
[‘Pizza’ 1, ‘Burrito’ 2, ‘Mac N Cheese’ 3, ‘Caesar Salad’ 4]
Computers have a different form of counting arrays. It looks something like this:
[‘Pizza’ 0, ‘Burrito’ 1, ‘Mac N Cheese’ 2, ‘Caesar Salad’ 3]
Computers Count the Number of Movements, Not Items
When a computer counts an array, it starts at zero because it counts the number of movements it needs to make to reach an item in the array. Therefore, when a computer recalls ‘Burrito’, it would need to move one item, meaning ‘Burrito’ is assigned a value of 1.
Programmers Count Like Computers for Efficiency
Now that you understand why computers count from zero, you’re probably wondering why programmers need to count the same as computers. Programmers count the same as computers because it’s more efficient.
My high school programming teacher always said, “computers are dumb machines.” He would say computers are dumb machines because they don’t think on their own; they do exactly what you tell them to do.
If programmers were to count from one, they would need to ask the computer to make an additional calculation every time they recall an item from an array. Computers are dumb machines, so they can’t assume you’re counting as a human, hence the additional calculation.
Even though the additional calculation would be adding one to the array values or subtracting one from the inputted value, small calculations over and over can lead to massive efficiency loss at scale. Therefore, programmers have to speak the computers’ language when talking to computers.
I should also mention that programmers do not count from zero daily. I’m sure if we asked a programmer to number our array example from before, they would count the same as us unless they’re actively programming.
So, programmers are not robots; they just talk to robots and have to talk with robot language in certain situations.
What Is an Array?
An array is a list of elements that are assigned a numerical value. The assignment of a numerical value to each element in an array allows programmers to easily recall an element from an array while programming and recalling random items from a list.
Arrays play an important role in computer programming. They can deal with a huge variety of elements depending on the task at hand.
What Kind of Elements Can Be in an Array?
An array can be made up of any kind of element. You can make an array with the names of different colors, numbers, countries, or whatever your creativity can muster. Additionally, arrays can be any length the programmer chooses.
As you can see, arrays are extremely versatile. Their many uses make them an irreplaceable tool in the programming world.
What Is One-Based Indexing?
One-based indexing is numbering arrays starting from one instead of zero. One-based indexing has advantages over zero-based indexing, like always knowing the length of an array. One-based indexing is also easier for programmers to use since programmers count from one in daily life.
Put simply, one-based indexing is another method of organizing arrays. This type of indexing can come in handy for programmers trying to accomplish different types of tasks.
What Programming Languages Use 1 Based Indexing?
Almost every mathematics-based programming language uses one-based indexing. A few examples of programming languages that use one-based indexing are Julia, Matlab, and R. However, some non-math-focused languages like COBAL also use one-based indexing.
For a more comprehensive list of programming languages that use one-based indexing, you can check out Open Genus’ article on Programming Languages where indices start from 1 (NOT 0). In the Open Genus article, you’ll find 20 different programming languages that use one-based indexing.
Why Do Mathematics Programming Languages Use 1 Based Indexing?
Mathematics-focused programming languages use one-based indexing because it is a more natural system for mathematicians. When developing a programming language, the developers can decide between one or zero-based indexing and pick the indexing most natural for their user base.
It’s important to consider the type of programming language you are using when it comes to utilizing arrays and their different methods of indexing.
What Was the First Programming Language To Count From 0?
The first programming language to count from zero was Basic Combined Programming Language (BCPL). BCPL is not a common programming language today, but it was an important precursor to current zero-based indexing programming languages.
Does Python Count From 0?
Python counts from zero, along with most other web programming languages. A few other web programming languages that count from zero include but are not limited to PHP, Javascript, and Ruby.
List of Programming Languages That Count From 0
Here are some programming languages that count from zero:
- C
- C++
- Java
- Lisp
- Ruby
- PHP
- Perl
- BCPL
- C Sharp
- TypeScript
- Fantom
- Ring
- Zig
The above list does not include every programming language that uses zero-based indexing. Zero-based indexing is so common that most modern programming languages could be on the above list.
Does Coding Just Involve 0s and 1s?
Coding does not just involve zeros and ones. Most coding projects will utilize the entire alphabet. The zero and one coding you see on TV and in movies is called binary code. Computers still read binary code, but programmers write their code in a more similar program to natural language writing.
What Is Binary Code?
Binary code is a base-two numerical system, meaning there are only two digits. When a programmer finishes their code, the coding program will convert what the programmer has written into binary. Once the code is binary, the computer can read the code and run the program.
Binary code is usually grouped in collections of four digits because it’s easier for humans to read. If you see binary code written down, it will look like this:
1100 0101 1011 1001
The above binary number would equal 50,617 in our hexadecimal numbering system. The following section will explain how to convert binary numbers to hexadecimal numbers.
How To Convert Binary to Hexadecimal
Let’s start with an easy number, like 1101. Now that we have our number to convert, you can follow these easy steps to convert binary to hexadecimal:
- Write the number (in this case, 1101) on a piece of paper.
- Write a small number one above the furthest right number.
- Multiply the small number you just wrote by 2, and write it above the next number in your binary code, moving left.
- Continue step three for all the numbers in your binary code. Your page should look like this: 18 14 02 11
- Cross out all zeros in your binary code.
- Add the values over all the ones. The sum of all the above numbers will give you your conversion.
If you calculated our example number correctly, the answer should be 13.
If you’re up for a challenge, we can try calculating our original example!
First, we’ll start with the binary number:
1100 0101 1011 1001
Next, we’ll write our little numbers. You might want to get a calculator—the numbers will get pretty big on this one.
132,768 116,384 08192 04096 02048 11024 0512 1256 1128 064 132 116 18 04 02 11
Now we can remove the zeros:
132,768 116,384 11024 1256 1128 132 116 18 11
Finally, we can add up our remaining numbers:
32,768 + 16,384 + 1024 + 256 + 128 + 32 + 16 + 8 + 1 = 50,617
I find converting binary to hexadecimal a fun process, but if you don’t, there are binary code conversion sites that make the process easier.
What Is Base 2 Numbering?
Base two numbering is the numerical system binary code uses. All numerical systems can be referred to as base X numbering. For example, our normal numbering system is called base-ten numbering (or hexadecimal) because there are ten digits in our numerical system.
Final Thoughts
To cap off this article, I’ll reiterate that programmers count from zero because computers count from zero. To re-quote my high school computer science teacher, “computers are dumb machines.”
Since computers are dumb machines, programmers need to speak the computer’s language. Therefore, since computers count arrays starting from zero, programmers often need to do the same.
What did you think of this article?
Share your thoughts by replying on Twitter of Become A Better Programmer or to personal my Twitter account.