Step by Step — Pseudocode

You ask your little brother to make you a sweet tea. Five minutes later he comes back with a glass of cold water and a tea bag that is still dry. He did…

You ask your little brother to make you a sweet tea. Five minutes later he comes back with a glass of cold water and a tea bag that is still dry. He did nothing wrong. You never told him the water had to be boiled first.

A computer is far more literal than your brother. It does exactly what you write, one line at a time, from top to bottom. It never guesses what you meant and never adds a step you forgot. So your first job here is a single one: write the steps complete and in order.

A recipe is a program too

A clear sequence of steps that gets one task done is called an algorithm. The tea recipe is an algorithm. The directions you send a motorbike taxi rider ("straight on to the mosque, turn left, third house with the green gate") are an algorithm too.

When the steps are written in words that look like code, it's called pseudocode, and the pseudocode here runs straight away:

PRINT "Boil the water"
PRINT "Pour the hot water over the tea bag in a glass"
PRINT "Wait three minutes, then take out the tea bag"
PRINT "Add sugar, stir and serve"
Boil the water
Pour the hot water over the tea bag in a glass
Wait three minutes, then take out the tea bag
Add sugar, stir and serve

PRINT shows whatever sits between the quotes, then moves to a new line. One line of code, one step. The computer starts at the top line and stops after the last one.

The order decides the result

Before reading on, guess: if the "Boil the water" line moves to the bottom, what gets printed first?

PRINT "Pour the hot water over the tea bag in a glass"
PRINT "Wait three minutes, then take out the tea bag"
PRINT "Add sugar, stir and serve"
PRINT "Boil the water"
Pour the hot water over the tea bag in a glass
Wait three minutes, then take out the tea bag
Add sugar, stir and serve
Boil the water

"Pour the hot water" comes first, before the water was ever boiled. The computer doesn't complain, because every line on its own is fine. The order is what's wrong, and only you can see that. Same with the directions for the motorbike taxi: "turn left, then straight on to the mosque" takes the rider somewhere else than "straight on to the mosque, then turn left".

A computer can't guess

Your brother can still ask "how many spoons of sugar?". A computer can't ask. A step that is obvious to a person has to be written out in full for a computer.

The text inside the quotes is read as it is, too. PRINT "Boil the water" prints Boil the water, with exactly its capitals and spaces. A typo inside the quotes gets printed as well.

The command word itself is more relaxed. PRINT, print and Print all read the same. Here we write PRINT in capitals, so the command is easy to tell apart from the text.

One more thing: a line that starts with // is a note for people. The computer skips it.

// Sweet tea recipe
PRINT "Boil the water"
Boil the water

⚠️ Why does the program stop at line 1?

When the computer doesn't understand a line, it stops and says which line confused it, then how to fix it. In a first program it's usually one of these:

The number after "Line" is the line number. Start looking there.

TL;DR: An algorithm is a clear sequence of steps that gets one task done, like a tea recipe or directions for a motorbike taxi rider. Pseudocode writes those steps in words that look like code. The computer runs them from the top line down, one line per step, and never guesses. PRINT "text" shows the text between the quotes exactly as it is. Swap the order of the lines and the result changes with it. A line that starts with // is only a note.

What you will learn