%%{init: {"flowchart": {"diagramPadding": 40}}}%%
flowchart TD
A(["Start"]) --> B["Take 2 kg of feed"]
B --> C["Scatter feed over the pond"]
C --> D{"Are the fish still scrambling?"}
D -- "Yes" --> E["Add 200 g of feed"]
E --> D
D -- "No" --> F["Record the total feed used"]
F --> G(["End"])
Chapter 2 — Thinking Like a Programmer: Writing Recipes for a Computer
Series B1 — Starting From Zero
Still no code in this chapter. Good programmers think first and type later. This chapter trains that thinking muscle using field work you already know.
Algorithm: a recipe that must not be ambiguous
An algorithm is a clear sequence of steps to reach a goal. You already use them without noticing: cooking recipes, directions, how to fill in a form.
The difference from ordinary instructions: an algorithm for a computer must not be ambiguous. Remember from Chapter 1 that a computer is an employee with no common sense. Compare:
| Instruction for a person | Instruction for a computer |
|---|---|
| “Feed the fish enough.” | “Scatter 2 kg of feed. While the fish are still scrambling, add 200 g.” |
| “Harvest when they’re big.” | “Harvest when the average weight is 500 g or more.” |
| “Water the plants early in the morning.” | “Water at 06:00 for 10 minutes.” |
Words like enough, about, rather, and when it’s big are easy for people but impossible for a computer to carry out. Whenever you write an instruction, ask: “Could someone who knows nothing at all do this correctly?”
The three basic ingredients of every program
A secret rarely told: a program of any size, from a calculator to a map app, is built from just three basic ingredients.
| Ingredient | Meaning | Example from the field |
|---|---|---|
| Sequence | Steps done one by one, top to bottom | Take feed → scatter feed → record the amount |
| Choice | If a condition holds, do A; otherwise, do B | If the water pH is below 6, add lime; otherwise, leave it |
| Repetition | Repeat a step while a condition still holds | While the fish are still scrambling, add a little more feed |
That’s all. The rest of this book is essentially how to write these three things in Python, then apply them to data from forests, plantations, rice fields, ponds, and barns.
Flowcharts: drawing an algorithm
A flowchart is a way of drawing an algorithm so it is easy to see. There are only a few shapes:
| Shape | Meaning |
|---|---|
| Oval | Start or End |
| Rectangle | One step to carry out |
| Diamond | One question answered “Yes” or “No” |
| Arrow | Direction of flow, to the next step |
Here is the algorithm for feeding a pond, using all three ingredients at once:
Trace it with your finger: start at the Start oval and follow the arrows. At the diamond you must choose “Yes” or “No”. The “Yes” road goes back to the same question. That is repetition: looping until the answer is “No”.
No special software is needed. A flowchart can be drawn on paper with a pencil. If you can trace your drawing without getting lost, the algorithm is clear enough to turn into code.
Break big problems into small steps
Suppose your boss asks: “Work out this month’s plantation harvest.” It sounds big and vague. Programmers break it down until each piece is clear:
- Gather the harvest records of every plantation block.
- For each block: add up the harvest weight of all days.
- Add up the results of all blocks.
- Compare with this month’s target.
- Display the result neatly.
Each number can be broken down further, until the smallest step is so clear it can be written as one or two lines of code. This habit of breaking problems down is worth more than memorising any language rule.
Testing an algorithm in your head
Before writing code, test the algorithm with small numbers. Example: 2 kg of feed, and the fish scramble twice before calming down.
| Step | Feed scattered so far | Fish still scrambling? |
|---|---|---|
| First scatter | 2000 g | Yes |
| Add 200 g | 2200 g | Yes |
| Add 200 g | 2400 g | No |
Result: a total of 2400 g. Testing by hand like this is called a dry run. If the result already makes sense on paper, only then are we confident to type it.
Besides flowcharts, programmers often write algorithms as pseudocode: everyday sentences arranged like code, for example “REPEAT while fish scramble: add 200 g”. Pseudocode is not run by a computer. It is only a thinking tool before writing real code.
Exercises
- Write an algorithm for making sweet tea in six or more steps. Make sure it contains no word like enough.
- Draw a flowchart for: “Check the pond water pH. If it is below 6, add lime and check again. If it is 6 or more, record the result.” Which part is a repetition?
- Challenge: break the task “Count the healthy trees in one forest plot” into at least five steps, then mark which steps are choices and which are repetitions.
Chapter Summary
- Algorithm = a clear, unambiguous sequence of steps to reach a goal.
- All programs are built from three ingredients: sequence, choice, repetition.
- A flowchart draws an algorithm: oval (start/end), rectangle (step), diamond (question), arrow (direction).
- Break big problems into small steps, then do a dry run on paper before typing.
Next, in Chapter 3: we meet the terminal, the black window where you “chat” with the computer by typing, with nothing to fear.