02. Solving problems on paper first
Most people start by jumping straight to the keyboard. They type something, delete it, type something else, get stuck, get frustrated, and decide programming is hard. They are right that it is hard — but the hard part is not the typing. It is the thinking.
The good news: thinking can happen on paper, where nothing can go wrong. A pencil costs nothing to erase; a program that does the wrong thing costs hours to fix. This chapter shows you a simple picture-language called a flowchart for sketching a program before you write it.
Why bother with paper?
Three reasons:
- Changing your mind is free. Cross a box out, draw a new one. On the computer, that change might be ten minutes of re-typing.
- You see the whole shape at once. A program on a screen scrolls — you cannot see lines 1 and 20 together. A flowchart fits on a page.
- You think more slowly. That sounds bad, but it helps. Most bugs come from skipping past a part you have not thought through. Pencils are slower than typing, which forces you to notice when you do not know what comes next.
Professional programmers do not always use flowcharts, but every one has some way of slowing down before typing. For your first programs, paper is the right tool.
The four flowchart shapes
This book uses a tiny set of shapes. Textbooks have more; you do not need them.
| Shape | Meaning |
|---|---|
| Rounded oval | The start or end of the program. |
| Rectangle | An action — something happens. |
| Diamond | A question with a yes/no answer. |
| Arrow | What comes next. Follow the arrow. |
On screen, the four shapes look like this — a rounded start/end, a rectangular action, a diamond question, and the arrows that join them:
On paper you would draw the same ovals, rectangles, and diamonds by hand. Either is fine.
A worked example: making a sandwich
A flowchart for "make a peanut butter and jam sandwich":
Read it top to bottom. Each arrow means now do this. There are no questions here — every step happens, in order, every time. The whole thing is one straight line down the page.
Reading a flowchart out loud
A good test is to read a flowchart aloud, tracing your finger along the arrows. For the sandwich flowchart:
Start. Take two slices of bread. Then lay them flat on a plate. Then spread peanut butter on slice 1. Then spread jam on slice 2. Then put slice 2 on top of slice 1. Done.
If it sounds natural out loud, the flowchart is probably right. If it sounds like something is missing, something probably is.
A flowchart with a question
A yes/no question uses a diamond. Two arrows leave it — one labelled
yes, one no — leading to different actions,
and afterwards the two paths usually join back up.
A flowchart for "should I bring an umbrella?":
Now there are two possible paths. One day the program runs the left side, another day the right side. The question decides which.
On paper, draw a flowchart for should I wear a coat? with one diamond. The question is "is it cold outside?". The two outcomes are "put on coat" and "leave coat behind". They join back up at "walk out the door".
A flowchart with a loop
The third pattern is doing the same thing again and again until something changes. In a flowchart, that is an arrow pointing back to a step you already passed.
A flowchart for "wash the dirty dishes":
Read it out loud:
Start. Are there dirty dishes? If no, done. If yes, wash one dish. Then go back up and ask the question again.
Every loop has two parts: a question (when do we stop?) and an action (what do we do each time?). If you cannot say both aloud, it is not finished.
When NOT to use a flowchart
Flowcharts are a tool for thinking, not a rule. A three-line program needs none. A program with ten or twenty steps usually repays the time it takes to draw one.
A good rule for now: if you cannot picture the whole program in your head at once, draw a flowchart first.
Homework
Paper homework, no computer needed. Tear pages out of an old notebook if you have to.
Problem 1 — Brush your teeth
Draw a flowchart for the morning brush-your-teeth routine. Use only actions (rectangles) — no questions, no loops. Five to seven steps is about right. Start at pick up the toothbrush and end at put the toothbrush back.
Problem 2 — What to watch
Draw a flowchart for choose something to watch. Include exactly one diamond with the question have I seen this episode before?. If yes, loop back and pick again. If no, end with press play.
Problem 3 — What to wear
Draw a flowchart for decide what to wear today. Include at least two diamonds. Suggested questions: is it cold? and is it raining?. Each question leads to a different action (or pair of actions). At the end, the paths join back up and end with put it on and leave.
Challenge — A loop with a counter
Draw a flowchart for push ten press-ups. It must use a loop — an arrow pointing back to an earlier step — and stop after exactly ten, not nine and not eleven. Hint: you will need a number that goes up by one each time and a question that checks whether it has hit ten yet.
Stuck or finished? Open the homework solutions page.