Breakfast for Champions

You Already Know How

Computer programs are big and complicated and scary.

But it's not so bad if you understand what is going on.

Anything that is "big and complicated" has parts. If you understand the parts and how they fit together, you understand the whole thing. That's probably the most important thing you can learn about programming.

A country is big and complicated, but it is made up of cities and countryside. Cities are made up of streets and buildings. Buildings have doors and windows and rooms. Rooms have furniture and fixtures (think: lights and plumbing and fireplaces) and sometimes people. You, a particular person, woke up this morning on a particular piece of furniture in a particular room in a particular building in a particular city or countryside in a particular country. It tries to be more complicated, but you can understand the parts, and then you can understand how they fit together into bigger parts.

Big fleas have little fleas
Upon their backs to bite 'em
And little fleas have littler fleas
And so, ad infinitum.

Take breakfast. You know all about breakfast, you do it every day. Breakfast is like a computer program. It has parts, and we all understand the parts.

Some parts are the things we do when we eat breakfast. At the bottom level of our understanding, the things we do happen in three directions: forward, backward, and sideways. OK, four: outside is a different kind of direction.

Some parts are the individual stuff we do things to, and the places we put stuff.

And finally, after we understand the little fleas, we have the "big fleas," a box we wrap it all up in and give it a name like "breakfast" so we can say "Let's get breakfast," and everybody knows what you mean. More or less.

I'm going to start with the "stuff" of breakfast, the cereal, the bowl, the milk, the spoon, and maybe some fruit, all on a table. You know what these are.

[image: cereal box, bowl, milk jug, spoon, fruit (apple+banana), all on a table]

Then we talk about what you do with these things. You put the bowl on the table, you pour some cereal into the bowl, then milk over the cereal. The order is important. If you pour the cereal before there is a bowl there, you get cereal all over the table. [image: cereal all over table] If you pour the milk into the bowl before the cereal is there, the cereal floats on the top and it doesn't mix in well. [image: cereal floating on top of bowl and spilling over onto table] The order is important. You know that. That's our First Principle, we call it Sequence, doing things in a particular order, stepping forward through the computer program. You already know how to do that.

[image: numbered frames, 1: empty bowl, 2: cereal being poured into bowl, 3: milk being poured over cereal]

Then you eat your cereal. First you put the spoon into the bowl under some of the cereal, then you lift the spoon to your mouth, and you open your mouth and put the spoon (with the cereal on it) into your open mouth, then you remove the spoon, leaving the cereal in your mouth. Then you put the spoon into the bowl under some of the cereal, then you lift the spoon... You see what happened? Getting the spoon of cereal into your mouth is a sequence of steps, but then we did the same steps again. And again. Always going back to the beginning and starting over. That's the "backwards" part, we call it Iteration.

[image, animated: spoon going up and down from bowl to mouth]

But what happens if the box of cereal is empty? If there's no CheeryOats, then you eat CornFlakes. If there's no corn flakes, then you eat fruit. You don't do all three, you decide, you choose to do one or the other, not both. Maybe you decide because because you feel like it, maybe because there isn't any cereal, but it's a decision somebody made. We call it Conditional, because we either go left or right, never both at the same time. You do it all the time, make choices.

[image: cereal box and fruit, with big question mark between them]

For breakfast we eat food, but computers eat numbers, chew them up, and spit them out. Your breakfast cereal is in a box, and the box is in the cupboard, as is the bowl; the fruit may be in a basket on the counter, the milk in a jug in the fridge. Computers have places to put their numbers, we call them Variables because the values of those numbers vary (change) over time -- mostly because you told them to. The milk in the jug gets used up, it gets poured from the jug into the bowl, and then (in the spoon) carried to your mouth. The cereal comes out of the box into the bowl, then also into your mouth. Numbers move around in the compouter too, but usually as copies. More on that later.

When you are done with breakfast, you put the bowl and spoon in the sink and wash the remains down the disposer, you put the fruit peels and/or cores into the trash and carry the trash out to the bin. Later that day somebody goes to the grocery store to buy more cereal and fruit. Things come in, things go out, we call it Input and Output, or I/O for short.

[image grocery bag with arrow down to table, apple core and banana peel with arrow up and out]

Those five principles -- Sequence, Iteration, Conditionals, Variables and I/O -- are what computer programs are made of. They are the littlest fleas of the flea stack.

And when all is said and done, you ate breakfast. Nothing more is said, just "breakfast," and everybody knows what you meant. It's the name of the whole collection of things you did in the morning. You have a routine for breakfast, another routine for when you get ready for bed. Some of these routines are like the fleas, they have littler fleas, we call them Subroutines, now as a general word meaning any collection of things that you can do that you refer to with a name. Subroutines are packages we put around the Five Things to make your life easier, and the packaging ability is so important that it counts as a sixth principle. The whole computer program is the big flea, and somewhere in between, we have middle-sized fleas that are called "objects" but they basically do the same packaging thing as subroutines (only bigger) so you don't need to know about them until your program gets big.

The point of all this is that these six things that you do with breakfast, you already know how to do them. Your breakfast is a computer program with you being the computer. All that is left is to tell you how to spell these six things so the computer understands you.

You still need to think about what you want to do, and how you want to do it. Then break it down to little fleas and littler fleas, until you are down to the level of the six things, then write that down in the language the computer understands, and you are a programmer. You already know how to think about it, you just need to learn how to spell it in Java (or Chomp or GameEngine). That's what we will do these next three weeks.

The Six Principles

One more time, because we consider this important. We have three ways to order the steps in programming breakfast, and in programming a computer:
1. Sequence, the steps in the proper (forward) order.
2. Iteration, going back to do the same steps again and again.
3. Conditionals, choosing to go either left or right, one or the other, but not both.
We have two things we do with data, the stuff of breakfast, the numbers and words in your computer program:
4. Variables, the containers in your kitchen and inside your program where you put the stuff, together with the stuff itself (cereal or fruit or numbers) that the steps those first three principles are organizing, what those steps do to make the world different.

5. Input/Output, getting stuff into and out of your kitchen or program.

And finally we have a way to put a name on a bunch of steps and data (like "Breakfast") so we can just mention it without going through all the low-level details, and we call it:
6. Subroutines, or sometimes Objects, a named box around some collection of the previous Five (or Six).
All we have done in this page is give strange new names to what you already know how to do. Computer programming is like that, we give new names, new ways to spell old names, to ideas we already understand. If you are Hispanic, or your parents came from another country and you speak the old language in your home, you already understand what it is like to have different ways to say the same thing. If you are not bilingual, you get to work a little harder. But you'll get it. Anybody can do it if they want to.

Next: Chomp
(or Skip Chomp, go directly to Java)

Tom Pittman
(images by Vivian Killian)
Revised 2020 October 10