It is imperative to understand that computer code is not algebra

Iphone-weather-coldI am responsible for teaching computing to 2nd years. Some students take to programming (in this case in Fortran, but this applies generally) like a duck to water, others sometimes sink a bit at first. I think part of the problem is that some can think like a computer very naturally, while for others it is a lot less natural. It probably does not help that code in the type of computer languages called ‘imperative languages’ (which includes Fortran and C), looks like algebra, but it really isn’t. I can illustrate this with an example.

Consider the 2 lines of Fortran code:

x = 1.5
x = x + 1.0

They concern a Fortran variable called x. In a language like Fortran (or C), this variable is a little tiny part of the computer’s memory. You can imagine this as a little box that will take one number in it, at any one time.

In this case we have two lines of code, that we do in order. First we do the first line, we open up the box, and put the value 1.5 in it. Then the second line tells us (we need to read the line starting from the right-hand side of the equation) to open up the box called x, take the current number in there, which is 1.5, add 1.0 to it to make 2.5, and put it back in the box. So after doing both lines, the box called x has the value 2.5 in it.

In algebra, the two lines

x = 1.5
x = x + 1.0

together make no sense. The first line tells us that the variable x = 1.5, which is fine. But the second line tells us that the variable x equals itself plus 1.0 – this makes no sense in algebra. In algebra a variable cannot equal itself plus 1.0, it can only equal itself.

I guess the problem is that variables in basic maths, and in computing (in particular in imperative languages like Fortran) are different things but we use the same symbols for them, which is confusing for people who can do maths, and are learning programming for the first time. Computers basically don’t do algebra, they have billions of little boxes to store numbers (memory) plus a CPU to take numbers from these boxes and do arithmetic on them, e.g., add 1.5 to one of them.

This sounds simple, but if you do enough of this you can even model with reasonable accuracy the notoriously changeable British weather. The UK’s Met Office runs a complex model (written in Fortran) called the Unified Model to make the weather forecasts we rely on, and it gets at least the short-range forecasts mainly right.