This page serves as an introduction to basic programming concepts. If you have already programmed and you understand things like conditional statements and for loops you can probably move on. If those sound foreign to you, best to stick around. If you are looking for the page about how these basic concepts look in python, check out the python page here.
We are trying to understand how to write instructions that perform a particular task (a program). In order to do that we have to understand the general ways in which we can communicate to a computer. Let’s see an example of instructions given to students about to read a book from the Donald Knuth’s The Art of Computer Programming pages xiii-xiv:
- Begin reading this procedure, unless you have already begun to read it. Continue to follow the steps faithfully
- Read the Notes on the Excersices
- Set N equal to 1
- Begin reading Chapter N
- Is the subject of the chapter interesting to you? If so, go to step 7, if not, go to step 6.
- Is N <eq; 2? If not, go to step 16; if so, scan through the chapter anyway.
- Begin reading the next section of the chapter; if you have already reached the end of the chapter, however, go to step 16.
- (steps 8-15 aren’t important for us)…
- Increase N by one. If N = 3,5,7,9,11, or 12, begin the next volume of this set of books.
- If N is less than or equal to 12, go back to step 4.
- Congratulations. Now try to get your friends to purchase a copy of Volume 1 and to start reading it. Also, go back to step 3.
These volumes are not for the faint of heart but they are still considered classics (and he is still writing them as there are still volumes unfinished). However, he demonstrates in this simple example, everything that we need to know. We will go over these things in the following sections.
One of the most important ways that we communicate with a computer is with variables. In the demonstration above, Knuth introduces a variable that is used throughout, N. This N takes on new values (with addition by 1) and is used for comparison (is it equal to or less than 12, etc.). This is how we will use variables. It is simple, it can get more complicated if you want to understand more about how they are stored in memory. If you are interested in those kinds of things, we can talk more specifically about resources, or the answers to those questions. However, for the purposes of our study, this is basically it.
In the example with N, we would say, I think safety, that it is always an integer. It is always a number, at least in this example, between 1 and 12. As you can imagine there are other types of variables. For example, there might be numbers that have decimals (we will call those floating point numbers and you may have heard of doubles, which are also floating point numbers). You might also imagine that there are variables that are strings of characters like a book title or name of a person.
Classes and object oriented programming
Every datatype (integer, floating point, string of characters) are all pretty basic and python understands them immediately. You might be able to imagine that there would be types of data that we might want to define, that would be specific to the problem or to the program. For example, a phylogenetic tree might be a type of data, or a DNA sequence. In order for python to understand these, we will have to write and describe exactly what these data are. This type of programming is actually a different paradigm called “Object Oriented programming”. Because we work in data-driven science, this type of programming will be very common for us and we will talk more about it in the python tutorial.
Knuth also introduces the concept of the conditional. The conditional would be something like if something, then do something, otherwise do something else. This is a very simple concept and you can see that Knuth uses this, for example, on step 5, where it says if the chapter is interesting then go to step 7, if not, go to step 6. We will use conditionals in much the same way throughout.
We will see a couple kinds of loops throughout our programming examples. One, we will call for loops and the other we will call while loops.
Knuth employs something like a while loop where he says, while N is less than 12 go back to step 4, read and increase N by 1. You can see how this will loop until N is more than 12. That is why it is a loop.
You might can imagine a somewhat simpler loop that would say “do something for three times”. That might be something more similar to a for loop. We will see more of these later.
Now that you have come this far, best to move on to how these concepts are actually applied in a working programming language, in this case python.