How a computer thinks: algorithms without code
Lesson 1 of 4
1How a computer thinks: algorithms without code
The big pictureA computer does not understand "make me tea". It understands very small, ordered, unambiguous steps. The algorithm is those steps, and programming is writing them in a language the machine reads.
What the picture shows: Every algorithm takes something in, walks ordered steps, decides at a fork, repeats what it must, then hands back a result. You will see this same shape in every program you write later.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
Imagine handing a friend who has never been in a kitchen a sheet of instructions for a cup of tea. Write "heat the water" and they may put it in the oven. You must be exact: "fill the kettle to the line, press the button, wait until it clicks off". That sheet is an algorithm, and the friend is the computer.
An algorithm is a finite sequence of deterministic instructions turning inputs into outputs. The structured programming theorem (Böhm–Jacopini) shows sequence, selection and iteration suffice to express any computation. You write them here as pseudocode and a flowchart, then see them in Python and notice the translation is almost word for word.
Before You Start
📝Pen and paper
You write steps before any code. Nothing to install.
🔌How a computer works
It helps to know a computer runs very simple instructions very fast.
The Core Idea
1An algorithm is a precise recipe
An algorithm is a set of steps that are ordered, clear and finite. Drop any of the three and it is no longer one: unordered steps give different results, a vague step can be read more than one way, and steps that never end hang the machine.
An example from your day: finding a friend in your contacts. Open the list, read the first name, if it is them stop, otherwise move to the next, and repeat until you find them or the list ends.
2The three moves: sequence, decision, repetition
Sequence: one step after another in order. Decision: "if this, do that, otherwise do the other". Repetition: "repeat this while the condition holds". Any program, from a calculator to a banking app, is built from these three alone.
Pseudocode: a language between English and code that no machine runs but any programmer reads.
start
read the list of contacts
for each name in the list: <- repetition
if name == "Sara": <- decision
show her number
stop
say "not found" <- sequence continues
end3Pseudocode and flowcharts
Before writing code, write the steps in your own words. Pseudocode is text; a flowchart is a drawing: a rectangle is a step, a diamond is a decision with two exits (yes/no), an arrow sets the direction, and an oval is start or end.
4The same algorithm in a real language
Here are the same search steps written in Python. Notice how each line matches a line of the pseudocode. Press run, then change the name you look for to one that is not there and run again.
contacts = {"Ali": "0501", "Sara": "0502", "Noura": "0503"}
wanted = "Sara"
found = False
for name in contacts: # repetition
if name == wanted: # decision
print(name, contacts[name])
found = True
break
if not found: # sequence continues
print("not found")Worked example
Problem: an ATM allows three PIN attempts. Steps: (1) counter = 0. (2) ask for the PIN. (3) if correct, open the account and finish. (4) otherwise add one to the counter. (5) if the counter is under 3, go back to step 2. (6) otherwise keep the card. It has sequence (1, 2), decisions (3, 5) and repetition (back to step 2).
attempts = ["1111", "2222", "4321"] # what the user types, in order
pin = "4321"
counter = 0
for typed in attempts:
if typed == pin:
print("open account after", counter + 1, "tries")
break
counter += 1
if counter == 3:
print("card kept")
breakMyth vs Reality
| Myth | Reality |
|---|---|
| Programming is hard maths. | Most of it is organised thinking: breaking a problem down and ordering steps. You need maths only in certain areas. |
| The computer is smart and gets what you mean. | It does literally what you wrote, even when it is wrong. Precision is your job. |
| You must memorise a language before starting. | The algorithm comes before the language. Whoever masters the steps learns any language in weeks. |
Where you meet it at work
🗺️Map apps
They try routes, decide the shortest and repeat until arrival: the same three moves.
🏦Bank transfers
A chain of decisions: is the balance enough, does the account exist, is the code right?
🛒The shop basket
Repetition over each item to total it, then a discount decision.
Test your understanding
Which of these is not one of the three moves of an algorithm?
An algorithm is deterministic: the same input always gives the same output. Guessing breaks that.
In a flowchart, which shape is a decision?
The diamond has two exits, yes and no. A rectangle is a step and an oval is start or end.
You wrote "repeat until the cup is full" but the cup is broken. What is wrong?
An algorithm must finish. Add a maximum number of tries or a check on the state.
Quick review cards
Check Your Understanding
Why write pseudocode before code?
Because it separates "what do I do" from "how do I write it in this language", so you catch a missing step before getting lost in syntax errors.
Name a sequence, a decision and a repetition in a "wash the dishes" algorithm.
Sequence: turn on the water then add soap. Decision: if the plate is still dirty, wash it again. Repetition: for each plate in the sink, run the steps.
What makes a step "vague"?
That it can be read more than one way. "Heat the water" is vague; "boil the water in the kettle until it clicks off" is clear.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Done with this lesson?
Sign in to save your progress.