Preface (Chapter 0.5)

(as edited by Tom)

(ToC)

[...Not many of us down the whole bowl of cereal in one gulp. Write the program then turn the page.]

Did you get something that looks sort of like this? It's doesn't matter if your program has more or less steps than mine. Neither is supposed to be complete.
 

Remove milk from fridge
Remove cereal from cabinet
Take out spoon from drawer
Remove bowl from cabinet
Pour cereal into bowl
Pour milk into bowl
Dip spoon into bowl
Lift spoon to mouth
Eat
See if bowl is empty
If not, do it again.


In computer speak we have a word for this. Just like the list of steps you go through moving forward is called a sequence and when you choose to execute one set of sequences or another based upon something like what day it is, is called a conditional, going back and repeating a sequence over and over is called iteration.

Whether it's hitting a nail with a hammer until it's all the way in, sawing until a board is cut all the way through, or doing the same steps over and over until a task is complete, you use iteration all the time in your life, and so do computer programs.

There's an important difference between how you experience iteration, as compared to how you write it into your program. When you eat your bowl of cereal, it's like a sequence: Dip,Lift,Eat, Dip,Lift,Eat, Dip,Lift,Eat, Dip,Lift,Eat, ..., but when you program it, you don't keep saying "Dip,Lift,Eat" over and over, you say it just once and then tell whoever is doing the program to "iterate" (or "repeat" or "do it again" or whatever). Here's what you are doing or thinking:

but here's how you write it into your program:
Basically you notice that you are doing the same thing over and over -- here the three steps, "Dip,Lift,Eat" -- over and over, so that short sequence is what gets repeated. It's like we took this long flat sequence, and rolled it up... (turn the page)...
 

<<Previous | ToC | Next >>

[2021 June 28]