Programming can seem quite complicated, and in some cases, it is, but that doesn’t mean that only geniuses can learn to do it. Starting our journey to becoming a programmer lies in understanding the principles behind writing code, which are rather simple. Before delving into these principles though, let’s make sure we all understand what a program is.
So, what exactly is a program, and what is it that these programmers do to make all the cool apps we use on our smartphones? Well fundamentally a program is a set of instructions that tells a computer to do something. A programmer’s job is to create programs, that is to give the computer instructions so that the computer can be used to carry out a specific task. Programs can be quite complex, but it is worth keeping in mind that no matter what you are programming, the basic concept applies, write instructions to tell the computer what to do. The instructions given to the computer are communicated in a way a machine can understand, and this is done using a programming language.
There are many programming languages that allow us to create programs for a computer, but each can differ in many ways. The main reasons being the purpose of the language, what types of programs the language was built to make, and what words make up that language. Regardless of these reasons there exists a set of core principles, that extend across all languages, and are therefore important to understand if wanting to learn anyone one of them.
Before discussing what, these principles are I thought it would be best to show a simple example of a set instructions written in English that we would give to someone who wanted to make a cup of tea, and then discuss why these instructions are not useful to a computer, and how these instructions could be translated into something that could be understood by one. The instructions for making a cup of tea can be described as:
Place teabag in cup
Pour hot water
Leave to brew
Remove tea bag
As you look at these instructions you may be wondering why we cannot simply give these instructions to a computer, and well it’s because computers are too dumb to understand them. A computer needs very simple and specific instructions to do anything, and our language is just too complicated. So, if that’s the case, then how can we translate the instructions above into something the computer understands? Well let’s pick apart these instructions and find out. Although a computer isn’t very good at understanding our language they are very good at understanding certain things: data, conditions, repetition, and functions.
Data is just a word that means information that a computer can use or store. A computer can store lots of different types of data: images, numbers, audio, words, and so forth. When we want to write a program, we will want to use or store data. In the above example, we can see a lot of information that is required for us to be able to make a cup of tea. We need to be aware of the amount of tea bags, hot water, sugar, and milk we have available. We can use variables to store this type of information, a single value that represents something we would like store.
Sometimes we may want to store more than a single value. Like a cupboard in our kitchen that stores all our cups, when writing a program, we may want to store lots of values of the same type of data. Data structures are a method that allow us to store a cupboard full of cups in our program.
When referring to the example if we look at instruction 3. Pour hot water. We know ourselves that this action means to pour water from the kettle into the cup, and because we can see the size of the cup and the water filling up the space in that cup we know that once the water nears the top of the cup we should stop pouring as not to spill it everywhere. Computers cannot do this naturally, but can be told to do so. We can use conditions to tell the computer that we want to execute certain instructions based on some condition that can make use of a variable, such as if we have any sugar then add sugar to the cup.
Additionally, pouring water, milk, or stirring are all events that involve repetition of an action, which is carried out, until a condition is met. We would keep adding sugar until the brew was sweet enough for our taste, we would pour the water in until it wasn’t far of the top of the cup, we would add milk until we got the colour we wanted, and we would stir until the sugar had dissolved. Computers can be told to carry out instructions repeatedly using a condition that specifies how many times we repeat a set of instructions.
What if we wanted to make tea for 3 of our friends? We would have to repeat the set of instructions 3 times for each person, meaning we would have 21 instructions to carry out, most of these are repeating the same steps. To make it easier for us to tell the computer to carry out a repeated set of instructions, we can place them in functions. To make tea for 3 friends we would need to use repetition to call the function three times to execute all the instructions, based on the condition that all 3 of our friends had a cup of tea.
Finally, instructions provided to make a cup of tea, require that we follow them in order from step 1 to 7. At least with this example we could alter the order in which add the various components of a cup of tea: water, milk, tea, and sugar. However, we must be sure that the water is hot before we add it, and that all ingredients are placed in the cup before stirring. Writing programs works the same way, we must ensure that our instructions are written out in the correct order, otherwise things are bound to go wrong, or not work at all.
As you can see writing programs comes down to writing instructions using a programming language, where these instructions are used to tell the computer to do something. There exist many languages, which each have their purpose, but underlying all of them is a set of principles that if understood allow you to write programs no matter the language. Computers cannot understand the complexities of our language, but they are able of understanding certain things.
They can use and store information, as a single value, or as multiple.
They can be told to carry out a set of instructions based on a certain condition, and can use a condition as the basis of repeating a set of instructions multiple times.
Finally, instructions that we want executed multiple times can be placed in functions, removing the need to write the same instructions multiple times.
This article has provided a basic overview of a program, programming, and the fundamentals, and how they apply to a simple example of making a cup of tea. The next article will discuss more in depth the concept of variables, their uses, and their importance in the role of creating programs.