Computing
Algorithms and Scratch
Think in clear steps, draw them as flowcharts, add loops and decisions, then build a working program in Scratch.
Ask Abena to make tea and she will manage. She knows to boil the water first, and that "add sugar" means a spoonful, not the whole tin. A computer knows none of this. It does exactly what it is told, in exactly the order it is told, and nothing more.
That is why computing starts not with a keyboard but with thinking in steps. A set of clear steps that solves a problem is called an algorithm. Writing an algorithm in a language a computer can run is called programming.
In this book you will learn to:
You will need access to a computer with a browser, or the Scratch app on a phone or tablet. If you do not have one today, you can still do every thinking task with pencil and paper, which is how all programs begin.
2 / 39
An algorithm is a precise list of steps that, followed in order, solves a problem or completes a task.
Algorithms are everywhere, and most have nothing to do with computers:
A good algorithm has four qualities:
"Cook the rice until it is ready" fails the first test: what is "ready"? "Cook for 20 minutes on a low fire, then taste a grain" passes.
Write an algorithm, in no more than eight steps, for washing your hands properly. Give it to a friend to follow exactly. Did they do anything you did not expect?
3 / 39

Sequence means doing steps one after another in a fixed order. It is the simplest building block of every algorithm, and it is where most mistakes hide.
Here is an algorithm for making fufu. Spot the problem.
Step 1 cannot happen before steps 2 and 3. Pounding raw, unpeeled cassava gives you a mess, not fufu. The correct sequence is 2, 3, 1, 4.
Computers follow sequence exactly. If you tell a Scratch character to say "Hello" and then move 10 steps, it says hello first, every time. Swap the two blocks and the behaviour changes.
Here are the steps for sending a WhatsApp message, shuffled: type the message; open WhatsApp; press send; choose the contact. Put them in the correct sequence. Is there more than one correct order?
4 / 39
Try this with a friend. One of you is the robot and must do exactly what is said, no more. The other gives instructions to make the robot draw a square on paper.
"Draw a square" is not allowed; the robot only knows: put pen down, move forward N cm, turn right 90°, lift pen up.
A first attempt often goes:
Put pen down. Move forward 5 cm. Turn right. Move forward 5 cm. Turn right. Move forward 5 cm. Turn right. Move forward 5 cm.
A good robot asks: Turn right by how much? Fix it: Turn right 90°.
What happens at the end? The pen is still down. If the next instruction is move forward 10 cm, you get a tail on your square. Add lift pen up.
This is debugging: finding and fixing mistakes in an algorithm. Every programmer, from a Basic 8 pupil to the engineers at Google, spends most of their time doing it. The computer is not being stupid when it misbehaves; it is doing precisely what you said.
5 / 39
Big problems are solved by cutting them into smaller ones. This is called decomposition.
Suppose you want to make a Scratch game where a cat catches falling mangoes. That sounds hard. Decompose it:
Each small problem can be solved and tested on its own. If the cat moves correctly, you never need to touch that part again while you work on the mango.
Decomposition also makes teamwork possible: Kofi writes the cat, Ama writes the mango, and the pieces fit together.
Decompose "organise an inter-class football match" into six or seven smaller tasks.
6 / 39

A flowchart is a diagram of an algorithm. It shows the steps as shapes and the order as arrows. Flowcharts are useful because you can see the whole algorithm at once, including where decisions are made.
The standard shapes:
| Shape | Name | Used for |
|---|---|---|
| Rounded box (oval) | Terminal | Start and Stop |
| Rectangle | Process | an action: add sugar, score = score + 1 |
| Parallelogram | Input/Output | ask for a name, display the total |
| Diamond | Decision | a yes/no question: Is it plugged in? |
| Arrows | Flow lines | the order of steps |
Rules:
The lamp flowchart above shows them all: a problem, two decisions, actions for each case, and an end.
7 / 39
Read a flowchart by starting at Start and following the arrows, doing what each box says. At a diamond, answer the question and follow the matching arrow.
Trace this one for buying waakye:
Start → Join the queue → Is it your turn? No → Wait → back to Is it your turn? … Yes → Say how much you want → Pay → Do you want pepper? Yes → Add pepper → Collect your food → Stop. (If No, skip Add pepper and go straight to Collect your food.)
Notice two things:
Those two patterns, plus sequence, are enough to describe any algorithm ever written.
Draw the waakye flowchart with the correct shapes, labelling each decision's Yes and No arrows.
8 / 39
Let us draw a flowchart for checking whether a pupil passed a test, where the pass mark is 50.
Tips for tidy flowcharts:
Draw a flowchart for a trotro mate deciding the fare: if the passenger is a child, charge GH₵1; otherwise charge GH₵2. Then extend it: if the passenger is going past the market, add GH₵1.
9 / 39
10 / 39
A loop repeats a step or a group of steps. Without loops, drawing a square would need eight instructions; with a loop it needs three.
Repeat 4 times: move forward 5 cm turn right 90°
There are two main kinds of loop.
Count-controlled loop – repeats a fixed number of times. Repeat 4 times. Repeat 10 times. You know in advance how many.
Condition-controlled loop – repeats until something is true, or while something is true. Repeat until the mango touches the cat. Wait while the light is red. You do not know how many times; it depends on what happens.
Scratch has a third: forever, which repeats until the program is stopped. Games use it to keep checking for key presses.
Loops are why computers are useful. Nobody wants to write add the next mark 300 times to total a school's test results. Repeat for every pupil: add their mark does it in one line, for 30 pupils or 3,000.
Rewrite these as a loop: Clap. Clap. Clap. Clap. Clap. Stamp.
11 / 39
In a flowchart a loop is an arrow that goes back up. The decision diamond is the gate that decides whether to go round again.
Counting to 10:
Start → count = 1 → Output count → count = count + 1 → Is count > 10? No → back to Output count … Yes → Stop
Trace it: output 1, count becomes 2, is 2 > 10? No, go back. Output 2 … Output 10, count becomes 11, is 11 > 10? Yes, stop. Ten numbers printed.
Waiting for the light:
Start → Is the light green? No → Wait 1 second → back to the question … Yes → Cross the road → Stop
The first is count-controlled; the second is condition-controlled. In both, if the decision could never become Yes, the loop would run forever: an infinite loop, one of the commonest bugs. (What if the light is broken?)
Draw a flowchart that outputs the 5-times table from 5 to 50 using a loop. What must be true for the loop to stop?
12 / 39
A condition is a question with a yes/no (true/false) answer. Selection means choosing what to do depending on the answer.
In words: If it is raining, take an umbrella.
In a flowchart: a diamond.
In most programming languages, including Scratch:
if
then take an umbrella
With two choices:
if <mark ≥ 50> then say "Pass" else say "Fail"
Conditions are built from comparisons: equals (=), greater than (>), less than (<). You can join them with and, or and not:
Every decision a computer seems to make, from a traffic light changing to a phone unlocking with your face, is a condition like these, checked thousands of times a second.
13 / 39
Sequence, loops and conditions combine to describe anything. Here is an algorithm for a mate collecting fares on a trotro with 15 seats:
Start total = 0 Repeat 15 times: ask the passenger for their stop if
then fare = 2 else fare = 3 collect fare total = total + fare output total Stop
Look for the pieces: a sequence of steps; a count-controlled loop (15 times); a selection inside the loop choosing the fare; a variable (total) that stores a changing value.
The indentation shows what is inside the loop and what is inside the if. In Scratch the blocks physically wrap around their contents, so you can see this nesting.
Change the algorithm so that children pay half fare. Where does the new condition go?
14 / 39
A variable is a named box where a program stores a value that can change: a score, a count, a name, a fare.
In the mate's algorithm, total was a variable. It started at 0 and grew with every fare. fare was another; its value changed depending on the stop.
Rules of thumb:
Maths uses letters for unknowns; programming uses variables for values that change over time. The equation total = total + fare would be nonsense in maths, but in a program it means "take the current total, add the fare, and store the result back in total".
In Scratch, variables live in the orange Variables category. You make one with Make a Variable, then use set, change and the round reporter block that shows its value.
15 / 39
16 / 39

Scratch is a free programming language made by the Lifelong Kindergarten group at MIT. Instead of typing code, you snap together coloured blocks, like Lego. It runs in a web browser at scratch.mit.edu, and there is an offline app for computers and tablets.
The screen has three main parts:
A script is a stack of blocks starting with a hat block (an Event, such as when green flag clicked). Click the green flag above the stage to run every script that begins that way; the red stop sign halts everything.
17 / 39
Open Scratch and click Create. Follow these steps exactly, then experiment.
The cat greets you, slides to the right and meows. You have written a sequence of four instructions.
Now debug: click the flag again. The cat moves another 50 steps. Keep going and it leaves the stage. Fix it by adding, right after the hat block, go to x: 0 y: 0 from Motion. Now every run starts in the middle.
Save your project with a name (File → Save to your computer, or sign in to save online).
18 / 39
Let us turn the robot square from earlier into a program. Add the Pen extension: click the blue button at the bottom left of the palette and choose Pen.
Script:
when green flag clicked erase all go to x: 0 y: 0 point in direction 90 pen down repeat 4 move 100 steps turn ↻ 90 degrees pen up
Run it. A square appears behind the cat.
Now experiment, one change at a time:
Do you see the pattern? The number of repeats times the turn angle is always 360. That is a mathematical fact about going round any shape once, and your program just proved it.
Put the square script inside repeat 12 with turn 30 degrees after the inner loop. Run it before you read the next sentence. You have made a flower.
19 / 39
The Control category (orange) holds Scratch's loops. Each is a C-shaped block that wraps around the blocks it repeats.
repeat (10) – count-controlled. Runs the inside blocks the given number of times.
forever – runs until the stop sign is clicked. Nothing can go under it, because it never ends. Used for game loops that keep checking keys and collisions.
repeat until < > – condition-controlled. Keeps going until the condition in the hexagonal slot becomes true.
wait (1) seconds – not a loop, but you will use it inside loops to slow things down so you can see what happens.
A bouncing cat:
when green flag clicked forever move 10 steps if on edge, bounce
A cat that walks to the right edge and stops:
when green flag clicked go to x: -200 y: 0 repeat until <(x position) > 200> move 5 steps
Make the cat change costume every time it moves (Looks → next costume, inside the loop, with wait 0.1). It walks.
20 / 39
Scratch has three condition blocks in Control: if < > then, if < > then … else, and wait until < >. The hexagonal slot takes a Boolean: anything that is true or false.
Booleans come from:
Making the cat move with the arrow keys:
when green flag clicked forever if <key [right arrow] pressed?> then change x by 10 if <key [left arrow] pressed?> then change x by -10
Why is the if inside a forever? Because a condition is checked once when the program reaches it. Inside a forever loop it is checked again and again, so the cat responds whenever a key is held down.
A pass/fail checker:
when green flag clicked ask [What was your mark?] and wait if <(answer) > 49> then say [Pass!] else say [Try again.]
21 / 39
Go to Variables (dark orange) and click Make a Variable. Name it score and choose For all sprites. Three new blocks appear: set score to 0, change score by 1, and a round score reporter. Tick the box beside the reporter and the score shows on the stage.
Rules in practice:
Counting clicks on the cat:
when green flag clicked set score to 0
when this sprite clicked change score by 1 play sound Meow
Two separate scripts, both in the cat. The first resets; the second counts. Scratch runs them at the same time; that is normal, and it is how games work.
Add a variable lives, set to 3 at the start. Make it go down by 1 when the space key is pressed, and make the cat say "Game over" when lives = 0.
22 / 39
23 / 39

We will build the game we decomposed earlier. Start a new project.
Step 1: the cat moves.
In the Cat sprite:
when green flag clicked go to x: 0 y: -140 forever if <key [right arrow] pressed?> then change x by 10 if <key [left arrow] pressed?> then change x by -10
Test it. The cat should sit near the bottom and slide left and right. If it drifts off the edge, add if on edge, bounce or a condition to stop at x = 220.
Step 2: a mango appears.
Click Choose a Sprite (bottom right) and search for a fruit; if there is no mango, pick an apple or an orange and rename it Mango. Or draw your own in the Paint editor.
In the Mango sprite, make it fall:
when green flag clicked forever go to x: (pick random -220 to 220) y: 170 repeat until <(y position) < -170> change y by -5
Test. The mango should fall from a random spot, then reappear at the top.
24 / 39
Step 3: catching scores a point.
Make a variable score, for all sprites. In the Mango sprite, change the falling loop so that touching the cat ends the fall early:
when green flag clicked set score to 0 forever go to x: (pick random -220 to 220) y: 170 repeat until <<(y position) < -170> or <touching [Cat]?>> change y by -5 if <touching [Cat]?> then change score by 1 play sound Pop
Test. Each catch adds one; a miss adds nothing. If the score jumps by two on one catch, the mango is still touching the cat when the next fall starts; the go to at the top of the loop fixes that.
Step 4: a timer.
In the Stage (click it, bottom right) add:
when green flag clicked reset timer wait until <(timer) > 60> stop [all]
Now the game ends after a minute. You have a working game, built from sequence, loops, conditions and variables. Save it.
25 / 39
A program that runs is not necessarily a program that works. Testing means trying it on purpose to find mistakes. Use a table:
| Test | What I expect | What happened | Fixed? |
|---|---|---|---|
| Press right arrow | cat moves right | moves right | ✓ |
| Hold right arrow | cat stops at edge | cat disappears | add edge check |
| Miss a mango | score unchanged | score unchanged | ✓ |
| Catch a mango | score +1 | score +2 | move go to to loop start |
| Wait 60 s | game stops | stops | ✓ |
Common Scratch bugs and their fixes:
Fix one thing at a time, then test again. Changing three things at once hides which one worked.
26 / 39

A working game is a starting point. Each improvement is a small algorithm problem.
Harder over time – make a variable speed, set to 5 at the start; use change y by (0 − speed); every 10 points, change speed by 1.
Rotten mangoes – duplicate the Mango sprite, give it a brown costume, and make touching it change score by -2.
Sounds and looks – a cheer when score reaches 10; the cat changes costume on each catch.
A start screen – a sprite that says "Press space to start" and wait until <key space pressed?> before broadcasting start.
Broadcast – in Events, broadcast [game over] lets one sprite tell all the others something has happened; when I receive [game over] in each sprite responds.
Choose one, plan it in words, build it, test it. That cycle (plan → build → test → improve) is how every piece of software you use was made.
Plan one improvement as a numbered algorithm before you touch a block. Then build it. Was your plan complete?
27 / 39
28 / 39
The ideas in this book run the world around you.
Mobile money. When Ama sends GH₵20 by MoMo: if
Traffic lights at Kwame Nkrumah Circle: a forever loop stepping through green, amber, red with wait blocks, and sensors (conditions) that lengthen the green when traffic is heavy.
Search engines follow algorithms to decide which pages to show first.
Phone cameras run algorithms to sharpen faces and brighten dark rooms.
Weather forecasts for the farming season come from algorithms running on huge computers.
Social media uses algorithms to decide what you see next, which is why it is worth understanding them: an algorithm has goals, and they are the goals of whoever wrote it, not necessarily yours.
Write, as a flowchart, the algorithm an ATM follows from "card inserted" to "cash dispensed". Include at least two decisions.
29 / 39
Suppose a class register of 40 names is in alphabetical order and you want to find "Mensah".
Linear search: start at the top; check each name in turn until you find it. Simple, always works, but might take 40 checks.
Binary search: open the register in the middle. Is "Mensah" before or after the middle name? Throw away the half it cannot be in. Open the middle of the remaining half. Repeat.
40 names → 20 → 10 → 5 → 3 → 2 → 1
Found in about six checks instead of up to forty. With a million names, binary search needs only about twenty checks. This is how a dictionary, a phone contact list and a database find things quickly.
The catch: binary search only works on a list that is already sorted. Linear search works on anything.
Two algorithms, same problem, very different speed. Choosing a good algorithm matters more than having a fast computer.
Play a guessing game: a friend thinks of a number from 1 to 100, and you may ask "higher or lower?" Use binary search. How many guesses did you need?
30 / 39

Sorting is so common that dozens of algorithms exist for it. Here is bubble sort, the simplest to understand.
Line up these test marks: 45, 12, 78, 33.
Pass 1: compare neighbours, swap if out of order.
Pass 2:
Pass 3: no swaps. Sorted.
The big values "bubble" to the end, one pass at a time. As a flowchart it is a loop (passes) containing a loop (comparisons) containing a condition (swap?). It is slow for long lists; computers usually use cleverer sorts, but all of them are built from the same three patterns.
Sort 9, 3, 7, 1, 5 with bubble sort on paper. How many passes? How many swaps?
31 / 39
Between a flowchart and a real program sits pseudocode: an algorithm written in structured English that anyone can read, without worrying about the exact rules of a language.
The mango game's mango, in pseudocode:
SET score TO 0 REPEAT FOREVER MOVE mango TO random x, top of stage REPEAT UNTIL mango at bottom OR mango touching cat MOVE mango down 5 END REPEAT IF mango touching cat THEN ADD 1 TO score END IF END REPEAT
Conventions:
Pseudocode is used in exams and in real software teams to agree what a program does before arguing about how. If you can write good pseudocode, you can learn any programming language, because they all have the same bones.
Write pseudocode for the trotro mate's fare algorithm from earlier.
32 / 39
Scratch has an online community where millions of young people share projects, and you may want to join it. The same rules apply there as everywhere online.
Algorithms also raise fairness questions. If a program decides who gets a loan or which news people see, and the rules inside it are unfair, the unfairness happens at machine speed. People who understand algorithms can ask: What is this program actually told to do? Who wrote the rules? You are now one of those people.
33 / 39
Section A
Section B
34 / 39
35 / 39
Algorithm — a clear, ordered, finite set of steps that solves a problem.
Sequence — steps carried out one after another in order.
Selection — choosing between paths using a condition (if … then … else).
Iteration / loop — repeating steps; count-controlled or condition-controlled.
Condition — a question with a true/false answer.
Boolean — a value that is either true or false.
Variable — a named store for a value that can change.
Flowchart — a diagram of an algorithm using standard shapes and arrows.
Pseudocode — an algorithm written in structured plain English.
Decomposition — breaking a big problem into smaller ones.
Debugging — finding and fixing mistakes.
Sprite — a character or object in Scratch with its own scripts.
Stage — the area where a Scratch program runs.
Script — a stack of Scratch blocks beginning with a hat block.
Broadcast — a message one sprite sends so that others can respond.
Infinite loop — a loop whose stopping condition can never become true.
36 / 39
37 / 39
38 / 39
39 / 39
This book is for reading on SchoolSpec and cannot be printed.