Computer hardware and programs

The basics of computer science: part one

Dan Quine
5 min readFeb 17, 2014

This is the first in a series of posts talking about some of the fundamental ideas of computer science. These posts are aimed at anyone who wants to understand how computers work and why they are so important. Hopefully, you won’t need to know anything about computers to follow along.

What is a computer?

Computers can do a lot of different tasks: running a spreadsheet, editing a video, running Facebook in your web browser. The list of things they can do seems endless. What is a computer and how can it do all those things?

We all know what a computer is. I’m writing this blog post on one. My computer is a laptop, yours might be a tablet, a smartphone or a desktop computer. A computer has a keyboard, a screen and a bunch of complex electronics inside. Right? Actually, this is a misconception — something much more interesting is going on and these posts will talk about what that is and why it is important.

But let’s start with the basics: what is inside the box, what makes it tick, and how we can make it obey our commands.

What is inside the box?

Computer hardware — the electronics inside the box — is complex. But at their heart, all computers have three main parts. The Central Processing Unit (CPU) is the brains of the machine; it runs programs (see below) to manipulate the information stored in the computer. That information is stored in the Random Access Memory (RAM) which holds information temporarily: when you switch off your computer the information stored in the RAM is lost, so it can be copied from RAM to your hard disk for permanent storage. Any time you need information from the hard disk, you can load it back into RAM so that the CPU can work on it.

Obviously there are lots of other interesting pieces of hardware — ones that make words, pictures and videos appear on the screen, or send and receive information over the Internet, for example. But the heart of the machine is the CPU, RAM and hard disk.

But without programs, the hardware does nothing. What is a program and how does it run?

What is a program?

A program is a set of instructions that tell the machine what to do. A program can tell the CPU to get information from RAM, manipulate it and then put it back. For example, the text that I am writing is stored in RAM. The CPU is running a program that takes the letters I type and adds them to that text. It also lets me change the format of bits of the text — making them bold or italic — and eventually publish the text as a blog post.

The important point about a program is that it is a precise, step-by-step description of what must happen. The computer does exactly what the program tells it to, nothing more, nothing less.

So let’s look at how a program is written.

There are many different programming languages: these define how the instructions are written so that the computer can understand them.

Most of them share a few common characteristics. The most important of these are loops and logic.

Loops

A loop is a way of telling a computer to repeat an action a certain number of times. For example the following program displays the words “Hello there” on the screen ten times:

FOR i=1 TO 10
PRINT “Hello there”
END

The first line says how many times the loop will run. It says “count from 1 to 10, and each time do what follows”. The final line just shows where the end of the loop is. Everything between these lines get done (in this case) 10 times. Here we use the PRINT command to display the words “Hello there” on screen. Although this loop does just one thing, it could contain a much more complex sequence of instructions to be repeated.

Logic

Logic is a slightly more complex idea: it is how a program makes a decision. About the simplest type of logic is the “IF” statement. This allows you to say “depending on what information is stored in memory, either do this… or do that…”. Here’s an example:

IF age=10 THEN
PRINT "Ten"
ELSE
PRINT "Not ten"
END

The first line of this code contains a variable. Variables are a program’s way to access particular bits of information stored in RAM. The name age refers to a specific piece of information .

The program tells the computer to check to see what information is stored in age. If that information is the number 10, then the computer will display the words “Ten”, otherwise it will display “Not ten”. So now we have a program that can tell us if 10 is stored in a particular place in RAM or not. Again, instead of a single PRINT instruction, there could be complex sequences of instructions between the IF, the ELSE and the END.

Everything is loops and logic

So you now you know how to make programs that repeat instructions and decide on which further instructions to run based on the information stored in RAM.

It turns out that loops and logic are the basic building blocks from which any computer program can be written. Every piece of software you have ever used, or could ever be written (and there’s an infinite number of those), can be broken down into sequences of loops and logic. With these two simple ideas programmers have built the Internet and every application ever written: every game, every accounting package, every search engine, every spreadsheet.

The richness and variety of computer software comes not from the complexity of the instructions but from how they are put together. This is a very important idea in computer science: string a lot of simple actions together and you get extremely complex behaviour in return.

Next time

This post has covered the basics of hardware and programming. The next post will talk about abstractions and algorithms, two ideas at the heart of computer science.

--

--

Dan Quine

SVP of Eng at Hologram. Past: Mode, Lever, AltSchool, Songkick, Google, Blurb, Apple. Startup tech entrepreneur. Proud father of twins. @crowquine