03. The three building blocks
The previous chapter showed you flowcharts with actions, questions, and loops. This chapter zooms out and gives those three patterns their proper names. Once you can see them, every program in the rest of the book will be made of them, and only them.
Yes — every program. There is no fourth one. People have studied this for sixty years and the answer always comes back the same: every program is built from three patterns.
The three patterns
The names are:
- Sequence — do this, then this, then this.
- Decision — if something is true, do A; otherwise do B.
- Repetition — do something over and over until a condition says stop.
That is the whole list. Reread it. The rest of the chapter shows what each one looks like, with an everyday example.
1. Sequence
A sequence is a list of actions in order. The flowchart is a straight line down the page; each box happens once.
You used sequence in the brush-your-teeth flowchart in Chapter 02. It
is the simplest pattern and the easiest to write. It is also the only
pattern in this book's first project — the ASCII Name Banner — which is
nothing but a long sequence of print calls.
You will write this in code in Chapter 7. A sequence in Lua is just one line after another:
print("Put bread in toaster")
print("Push the lever down")
print("Wait two minutes")
print("Take the bread out")
That is literally a sequence flowchart, written down.
2. Decision
A decision asks a yes/no question and runs different actions depending on the answer. The flowchart uses a diamond with two arrows leaving it.
A decision is what makes a program react. Without decisions, a program runs the same way every time, no matter what happens. Add one decision and it can take different paths on different days.
You used decisions in the what-to-watch and what-to-wear homework. Real programs use them for every kind of "if". If the player's score is more than 100, give them a bonus. If the file does not exist, create it. If the touch came from a Player, give them a coin.
You will write this in code in Chapter 16. A decision in Lua looks like this:
if door_is_locked then
find_the_key()
unlock_it()
else
open_it()
end
Same shape: question on top, two paths, joined at the end.
3. Repetition
Repetition is doing the same kind of thing over and over until a condition says stop. The flowchart uses an arrow pointing back to a step you have already passed.
Every loop has the same parts:
- a starting condition (count is 0),
- a question (is count 10?),
- the work done each round (do a push-up),
- something that moves toward the stop (add 1 to count).
If a loop is missing the moves toward the stop part, it never ends. That is a bug called an infinite loop, and it is the most common bug in early programs. If your program seems frozen, you probably wrote one. (Use Ctrl + C in the terminal to stop it.)
You used repetition in the press-ups challenge in Chapter 02. Real programs use loops to go through every item in a list, draw every frame of an animation, ask the user again until they type a valid answer.
You will write this in code in Chapter 18. A loop in Lua looks like this:
for i = 1, 10 do
print("Push-up " .. i)
end
Ten lines of output from three lines of code. Loops are how programs do work too big to write out by hand.
Mixing the three
A real program is the three patterns nested inside each other: sequences holding decisions, decisions holding loops, loops holding other loops. The Number Guessing Game from Part 3 has all three:
- The whole program is a sequence: pick a number, then play, then announce the winner.
- The play part is a loop: ask for guesses until one is correct.
- Each round of the loop has a decision: was the guess too high, too low, or correct?
Once you can spot the three patterns, you can read any program by
asking, at each line, which one is this?. Most lines are
sequence. A line with if is the top of a decision. A line
with while or for is the top of a loop. That
is most of programming.
On paper, sketch a one-page flowchart for play one round of rock, paper, scissors against the computer. Try to use at least one of each pattern: a sequence of steps to set the game up, a decision to compare the two choices, and (optionally) a loop if you want to play more than one round.
Homework
Still paper — the last paper homework before you install Lua.
Problem 1 — Spot the pattern
For each scenario, write down which of the three patterns is the main one. (Some use more than one — name the most important.)
- Counting the marbles in a jar.
- Deciding whether you can afford a new game.
- Following a recipe with no choices in it.
- Asking the user to enter a number until they type a valid one.
- Printing each name on a class list.
Problem 2 — Sequence only
Draw a flowchart for make a piece of toast using only sequence (no diamonds, no loops). Five to seven boxes.
Problem 3 — Sequence and decision
Draw a flowchart for go to bed with exactly one decision. The question is is it a school night?. If yes, add an extra step (set the alarm). If no, skip it. Both paths end at turn out the light.
Challenge — All three patterns
Draw a flowchart for feed the pets. The program must contain all three patterns:
- Sequence: fill the food bowl, then fill the water bowl, then call the pet.
- Loop: keep calling until the pet shows up or until you have called five times (give up after five).
- Decision: if the pet did show up, give it a treat. If not, put the food away.
Label each pattern on your flowchart so a reader can see where it starts.
Stuck or finished? Open the homework solutions page.