Welcome to the second post in my introduction to computer programming series. In the first post we learned about basic computer literacy and wrote our first “Hello World!” program. We also talked about how computers need information, which we usually refer to as data, to operate on. This post will cover the topic of representing data in further detail. We will be working with data often, so it’s important to know what it looks like.
Now, seasoned programmers may accuse me of oversimplification on this topic. They have a valid point, but my goal is to help newbies learn the craft, not to bog them down in the details. In due time your programming journey will fill in many of the gaps I am intentionally leaving blank.
Data Types
Let’s start talking about representing data by discussing the three main types of data: Numbers, Text, and Booleans (true/false). While there are variations on these, these three types serve as a useful place to begin our discussion.
Numbers
Numbers could be integers, but they could also have decimal points in them. Or they could be negative. Different types of numbers are not always represented in memory the same way, but we can save those details for a later discussion.
Text
Text just refers to a series of characters. In the “Hello World!” example we covered in the previous post, “Hello World!” was a piece of text data. We usually call a piece of text data a string because it is a string of ASCII or Unicode characters. These are just standards for mapping characters to numbers so we can represent them as binary numbers. From now on we will refer to any piece of text data as a string.
Booleans
Booleans are the simplest of the three data types. Much like a transistor, they can only be found in one of two states, in this case true or false.
The best thing about a boolean is even if you are wrong, you are only off by a bit. 😜
Operating On Data
We’ve now seen that there are three main ways of representing data. There are also three main operations that a computer can perform on data. Let’s refer to these three operations as Math, Comparison, and Boolean. These three operations are ways of using the data we already have to create new pieces of data.
Math
We can use math to create new numerical data from the data we already have. For example, adding the numbers 5 and 2 gets us a new piece of numerical data, 7. Here are some mathematical operations we can perform:
- Addition, often represented in code by the + symbol.
- Subtraction, often represented in code by the – symbol.
- Multiplication, often represented in code by the * symbol.
- Division, often represented in code by the / symbol.
- Modulo, often represented in code by the % symbol.
Modulo might be a foreign term to you. It simply means performing division and returning the remainder.
Comparison
We can compare multiple pieces of data to create new boolean data. For example, asking if 2 is less than 7 gets us a new piece of boolean data, true. Here are some comparisons we can perform:
- Less than, often represented in code as <
- Less than or equal to, often represented in code as <=
- Equals, often represented in code as ==
- Greater than, often represented in code as >
- Greater than or equal to, often represented in code as >=
Boolean
We can use boolean logic to create new boolean data. For example, it is true that 2 is less than 7, and it is false that 2 is less than 1. Asking if either of these values are true gets us a new piece of boolean data, true. However, asking if both of these values are true gets us a different answer, false. For further study on this, read up on boolean logic and De Morgan’s laws. Here are the three ways of combining boolean data:
- AND, often represented in code as &&
- OR, often represented in code as ||
- NOT, often represented in code as !
The AND operation checks if both values are true, while the OR operation checks if any of the values are true. The NOT operation returns the opposite value of the original piece of boolean data.
Why Representing Data Matters
Wow, we covered a lot of ground! But it feels a bit academic, doesn’t it? The point of all of this is not to bore you to tears, but to show you that the amazing and wonderful computer programs that you use on a daily basis are just doing basic operations like these on the data you provide them. From your favorite websites to your favorite games, there is nothing magical going on.
However, combining these operations so that something useful happens can be a challenging task. This activity is called creating algorithms. Simply put, an algorithm is a set of instructions for the computer to follow. When we write code, we are really just writing algorithms that the computer can understand. We will talk much more about algorithms in the next post.
If you had trouble with any of this, leave a comment so myself or somebody else can help you out. And remember to subscribe so that you can stay up to date with this series and the rest of the content I publish.
One Reply to “Representing Data in Computer Programs”
Comments are closed.