Do I need to know math to learn to code?
By Jay Wengrow, CEO of Actualize
If you’ve ever looked at a screen of computer code, you’d probably notice some numbers within the code. This can give the impression that computer programming is math heavy, or at least that you have to be well-versed in math to write code.
The truth, however, is that for most software engineering roles, the only math you’ll need to know is basic arithmetic. Addition, subtraction, multiplication, and division are pretty much the only math concepts that come up in the average software developer’s day-to-day work.
So what’s the deal with all the numbers you see in computer code?
The primary skill of writing code is the ability to give instructions to a computer. This is a skill that takes a lot of practice, and isn’t something people have naturally. This is because people are used to communicating with… well, people, and people have a built-in intuition as to what you’re saying to them. When communicating with a computer, however, you’re talking to an inert piece of metal that doesn’t have any intuition whatsoever. Thus, you’re forced to provide it with very literal instructions. So, tasks that are simple for humans can require complex instructions for a computer.
Here’s a simple example. Let’s say you wanted the computer to display all the words in a given list.
Here’s the list, which is called an array in programming jargon. The array is structured as follows:
[“apple”, “banana”, “cucumber”, “date”, “elderberry”, “fig”]
If I asked you to display these words by writing them down on a piece of paper, you wouldn’t require any instructions other than me telling you, “Please write all these words on a piece of paper.”
However, a computer requires very literal handholding to tell it how to display these words. Let’s jump into what the computer instructions might be.
The way arrays (lists) work is that each item in the array has an assigned number given to it, based on the order of the words. This number is called the index. The first word (“apple”) has an index of 0. The next word (“banana”) has an index of 1. The word after that (“cucumber”) has an index of 2, and so on and so forth.
So, we can tell the computer to print the words using these instructions:
print array print array print array print array print array print array
As you can see, we’ve used numbers to explain to the computer how to print the list of words. So, are numbers involved with coding? Sure. Is there heavy math involved? Usually not.
Going a bit further, we can give the computer an alternate set of instructions that is more ideal. The problem with the code above is that while it works for an array that only has six words in it, imagine that we had a list of 100 words. Or 10,000 words. We’d have to write 10,000 lines of code, which would be tedious and pretty much defeat the purpose of us using a computer in the first place. After all, computers should be great at automating tedious tasks.
The code we’re about to write does involve a number of fundamental coding concepts, so if you’re brand new to coding, don’t worry if you don’t follow everything precisely. The idea here is to simply give you a sense of how arithmetic makes its way into everyday code.
We’re going to first create a variable called index and set it to 0. A variable is like a box. It can hold something in it, and we can change what it holds over time. This is useful for keeping track of different kinds of information, as you'll see in our example. In our case, we’re going to keep track of which index we’re up to in the array as we print out each word.
So, our first line of code is:
index = 0
Next, we are going to use a special programming concept called a loop. The idea of a loop is that the computer will run the code within a loop multiple times. We are specifically going to use a loop known as a “while loop” - which will run the loop as long as a certain condition is true.
Our loop will look like this:
while index < array.length end
This means that any code we put within this loop will run repeatedly as long as the index variable is less than the number of words in the array. (array.length represents the number of items inside an array.)
Here’s the complete code - we’ll explain it momentarily:
index = 0 while index < array.length print array[index] index += 1 end
Again, we’ve first created a variable called index and we told it to hold the number 0. Next, we start our loop. This means that the code within the loop will run repeatedly up to a certain point. In our case, the code will run only as long as the number inside the index variable is less than the number of words in the array.
Next, let’s examine the code within the loop.
print array[index]. Since index is 0 right now, it’s as if we wrote
print array, and the first word (“apple”) will be printed to the screen. On the next line, we write
index += 1, which is a fancy way of saying that the number inside the index variable should increase by 1. So since index was 0, now it will turn into 1.
end keyword tells the computer to jump back to the top of the loop and run the code within the loop again. So the computer now runs again the line that says
print array[index]. Since index is now 1, it’s as if we said
print array, and the computer will display the word “banana” to the screen. On the next line, we increase index by 1, so now index is 2.
We then run the loop again. So it says
print array[index]. Since index is currently 2, it’s as if we said
print array, and “cucumber” will be displayed on the screen. We then increase index to 3.
And so on and so forth.
The loop runs up until the point where index is 5. This is because once index gets bumped up to 6, it is no longer less than array.length. (It’s equal to array.length, but not less than it.) At this point, the loop stops running.
The advantage of our new code is that it will work with an array of any size - even for one that has one million words in it!
If you’ll notice, we used a little arithmetic in our code. Namely, within the loop we kept increasing index by 1. Adding 1 to a number is simple math indeed, but that’s the whole point we’re making. You do often use basic math when writing code. But the math is very basic!
Obviously, there are some types of programmers who need to use advanced math for their work, but the majority of programmers need nothing more than their good old arithmetic. So don’t be intimidated by not having a strong math background. Simple math is all you’ll need.
Get Expert Advice
The Actualize Blog is where you can get expert advice and insights for learning to code from our CEO, Jay Wengrow. Subscribe to the Actualize Blog and get notified each time we publish a new article.