Programs: talking to a computer
A program is an algorithm written in a language a computer can follow. See variables, if, loops and functions, and why bugs happen.
You now know that a computer follows an algorithm: a clear list of steps. But a computer cannot read English. How do we give it the steps? We write a program.
From steps to a program
A program is an algorithm written in a language that a computer can follow. These are called programming languages. Some famous ones are Python, JavaScript and Scratch.
A computer's processor only understands bits, patterns of 1s and 0s called machine code. Writing those by hand would be slow and painful. So programmers write in a friendlier language, and a special program (an interpreter or compiler) translates it into machine code for the processor.
Here is the "find the biggest number" algorithm from the last lesson, written in Python:
numbers = [4, 9, 2, 7]
biggest = numbers[0]
for n in numbers:
if n > biggest:
biggest = n
print(biggest)
Even if you have never programmed, you can probably guess what it does. Read it out loud:
- Make a list of numbers.
- Say the biggest is the first one.
- For each number
nin the list: ifnis bigger than the biggest, make it the new biggest. - Show the biggest.
It prints 9.
The main ideas
Nearly every language has the same few ideas.
Variables: labelled boxes. A variable is a box with a name, for storing a value. biggest = numbers[0] puts the first number in a box called biggest. Later you can change what is in the box.
Choice: if. Do something only when a test is true.
if score > 10:
print("You win!")
else:
print("Keep trying")
Repetition: loops. Repeat steps.
for count in range(3):
print("Hello")
This prints "Hello" three times. Imagine writing that for a million repeats by hand! A loop does it in two lines.
Functions: named recipes. A function is a set of steps with a name, so you can use it again.
def double(x):
return x * 2
print(double(21))
This prints 42. The program gives the steps a name, double, and now it can be used anywhere.
Even a puppy can be programmed
Here is a tiny piece of a puppy's world, in the same style:
while not at_treat:
move("right")
"Keep moving right until you reach the treat." Train the Puppy goes further: instead of the programmer writing every step, the puppy learns its steps. But the learning itself is also a program, with a loop and a few lines of arithmetic.
Bugs
When a program does the wrong thing, it has a bug. Bugs come in two main kinds:
- Syntax errors. You broke the grammar rules, for example by forgetting a bracket. The computer usually refuses to run the program and points at the mistake.
- Logic errors. The program runs, but it does not do what you wanted. For example, using
<instead of>would find the smallest number instead.
The second kind is trickier, because the computer is not complaining. It is doing exactly what you said. That is the golden rule of programming:
A computer does what you tell it, not what you meant.
Remember the puppy and the sneaky toy? It did exactly what the rewards said, not what the designer meant. Same lesson, many years later and in a new form.
Finding and fixing bugs is called debugging, and it is a big part of every programmer's day. A good method is to read your program slowly, line by line, and check what each step really does.
Where programs run
The games on this site are programs. They are written in a language called TypeScript (a version of JavaScript), and they run in your web browser. When you move a slider in Launch Lab, a function runs, does some sums, and draws the answer.
You do not need to install anything to start. Beginner-friendly tools like Scratch let you drag blocks together to make programs. Try making a program that counts from 1 to 10. Then change it to count to 100 by changing one number.
Words to know
- Program
- An algorithm written in a language that a computer can follow.
- Programming language
- A language for writing programs, such as Python or JavaScript.
- Machine code
- The 1s and 0s that a processor actually understands.
- Variable
- A named box that stores a value.
- Loop
- A way to repeat steps.
- Function
- A named set of steps that you can use again and again.
- Bug
- A mistake in a program that makes it do the wrong thing.
Check yourself
Why do programmers use languages like Python instead of writing in 1s and 0s?
Show the answer
Machine code is slow and painful to write. A friendlier language is translated into machine code for them.
What does a variable do?
Show the answer
It stores a value under a name, like a labelled box.
What is the difference between a syntax error and a logic error?
Show the answer
A syntax error breaks the grammar rules, so the program will not run. A logic error runs but does the wrong thing.
What is the golden rule of programming?
Show the answer
A computer does what you tell it, not what you meant.