cadence_learn
think_like_a_programmer / level 03 of 12 ▶ 13 min

then, if, again.

Every program ever written is built from three shapes and nothing else. Learn them as pictures now and the C++ keywords later will be spelling, not new ideas.

after this level you'll be able to
  • Name the only three shapes any program can be built from
  • Read a flowchart and say which path will run before you run it
  • Explain why a loop runs forever, and what an off-by-one error is
the whole idea

there are only three shapes

the one sentence

Every program ever written — your homework, your phone, the flight system on a plane — is built from only three shapes: do this then that, choose between paths, and do it again.

the entire vocabulary of programming
1 · THEN wake up brush teeth leave one after another 2 · IF raining? YES NO umbrella sunglasses leave only one path runs 3 · AGAIN count ≤ 3? YES do a push-up count = count + 1 ↺ back the green line is why it ends
That is the entire vocabulary. Every program you will ever read — yours, your lecturer’s, the one flying the plane — is these three nested inside each other. The green step is the one beginners forget, and forgetting it is how a loop runs forever.

That is not a simplification for beginners. It is a proven fact about computing, and it is genuinely all there is. Everything that looks more complicated than this is these three shapes nested inside each other, sometimes thousands deep.

Their proper names are sequence, selection and repetition. Your lecturer will use those words. They mean then, if, and again.

why this is worth ten minutes

When you eventually meet C++ you will see if, else, while and for and they will look like new things to memorise. They are not new. They are these three shapes with punctuation. If you know the shapes now, the syntax later is spelling, not thinking.

try it yourself

press play and watch the path light up

Each of the three below is a real program written as a picture. Run them. On the second one, change the weather first and watch the path go somewhere else.

▤ lab 03 · the three shapes

Pick a shape, then press play. Watch which box lights up next.

start
Wake up
Brush your teeth
Eat breakfast
leave the house
start
Look out of the window
Is it raining?
yes
Take an umbrella
no
Take sunglasses
leave the house
count = 1
Is count 3 or less?
yes
Do a push-up
Add 1 to count
↺ go back and ask again
no
stop
count = 1 · push-ups done: 0
Press play. Watch which box lights up, and which ones never light up at all.
◈ ask an ai about this

“What are sequence, selection and repetition, and why do people say every program is made of only those three?”

chatgpt ↗ claude ↗

the one that catches everyone

the third shape is where bugs live

Sequence is easy. Choice is easy. Repetition is where almost every beginner loses an afternoon, and it is always one of two things.

Either the thing that is supposed to change never changes — you forgot to add 1 to count, so it stays at 1 forever, the answer to “is count 3 or less?” is always yes, and the program runs until you kill it. That is an infinite loop, and you will write one this semester. Everyone does.

Or it runs one time too many or one time too few. Look at the loop above: it does three push-ups, not four, and getting that exactly right is called an off-by-one error. It has a name because it is that common.

try breaking it on purpose

Go back and imagine the “Add 1 to count” box is missing. Walk it yourself: count is 1, is 1 three or less? Yes. Push-up. Back to the question. Count is still 1… Feeling why it never stops is worth more than being told it does not.

◈ ask an ai about this

“What is an infinite loop, and what is an off-by-one error? Show me each with a tiny everyday example instead of code.”

chatgpt ↗ claude ↗

do this together

find the three shapes in real life

☶ two people · 10 minutes · anywhere

Everything you do today is these three shapes. Go and find them.

person a

Name an everyday routine out loud — making coffee, doing laundry, getting to campus, checking your phone in the morning.

person b

Call out each shape as it appears. “That is a then.” “That is an if.” “That is an again — and what makes it stop?”

The killer question is the last one: what makes it stop? Laundry repeats until the basket is empty. Scrolling repeats until… and that is a real conversation about infinite loops. Swap roles after two routines.

what to keep

three things worth remembering

01

Then, if, again

Sequence, selection, repetition. Every program you will ever read is these three, nested.

02

A loop needs an exit

Something inside must change, or the answer to the question never changes and it runs forever.

03

C++ is spelling

if, else, while, for are not new ideas. They are these shapes with brackets.

check yourself

3 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself3 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

How many shapes is every program built from?

02

A loop checks “is count 3 or less?” but nothing inside ever changes count. What happens?

03

In an if / else, how many of the two branches run?

answered: 0 of 3right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →