Iteration & Variables

(ToC)


Video Introduction (9 minutes) <<--- Watch this video first, then

English IDE <<--- Click this link and again modify your program here.

When your latest version runs,Raise your hand in Zoom to continue.

If you didn't watch the video, or if you didn't pay much attention, and if you cannot figure out why your program does not run properly, you might reconsider: Watch the Video.

Or, if you are like me and prefer to read at your own pace instead of whatever pace set in some video, You can read the transcript (and probably come out ahead of the non-readers).

Video Transcript: 3. Iteration & Variables

By this time you should have mastered getting your steps in order -- we call that Sequence -- you can move things around on the screen and query the date -- that would be Input and Output -- and you can change what the computer does based on things not known when you wrote it -- the word is Conditional.

We have six concepts that cover all of computer programming, and you are halfway there.

One more thing we can do with this PBJ, which is we can repeat the same thing over and over, like if we want to make three sandwiches. Starting with the simplified program I had at the beginning, this makes one sandwich:

get bread from pantry
get plate from cabinet
Get knife from drawer.
get peanut butter
get jam
Open jam.
Open PB.
Open bread.
Get slice of bread.
Get another slice of bread.
spread jam
spread PB
Put bread pieces together.
We want to re-use the plate for the next sandwich, so we need a lunch bag to put the sandwiches in. Type this line at the front of the program:

       get bag from drawer

...and this line at the end:

       put sandwich into bag

You can run it and be sure that it works. Now to make three sandwiches, we could just replicate the last six lines (including the new last line you added) three times...

get bag from drawer
get bread from pantry
get plate from cabinet
Get knife from drawer.
get peanut butter
get jam
Open jam.
Open PB.
Open bread.
Get slice of bread.  {start #1}
Get another slice of bread.
spread jam
spread PB
Put bread pieces together.
put sandwich into bag {end #1}
Get slice of bread.  {start #2}
Get another slice of bread.
spread jam
spread PB
Put bread pieces together.
put sandwich into bag {end #2}
Get slice of bread.  {start #3}
Get another slice of bread.
spread jam
spread PB
Put bread pieces together.
put sandwich into bag {end #3}


But if you are doing something in a database for a thousand customers, that would make a very big program. It's what the computer does, but we want to make the program itself shorter. It's called "Code reuse" and it's very important. So let's go back to the 1-sandwich version, and insert just before you get the bread for the first sandwich, this one line:

Repeat 3


Then at the end of that sandwich, after the program puts it into the bag, insert a new last line:

Next


Now when you run it, the computer still makes all three sandwiches, but we only told it just this once.

This is called an Iteration (that's a fancy word meaning repeat) because it iterates or repeats some part of the program, in this case making a sandwich.

Experiment with making different numbers of sandwiches.

Bonus (not in the video): If you insert at the front of your program this line:

Input number
then change the Repeat line so it says:
Repeat number
The program will stop and wait for the user to type in a number, and then it will make that many sandwiches. Try it. What happens if you give it zero? How about ten? As I write this, the bread wrapper has only eight slices (enough for four sandwiches). Then it stops.
 

Variables

There is only one more concept left to go and you will have everything you need. Actually, you already had it in the very first program you wrote, PBJ.

People manipulate stuff -- peanut butter, jam, bread, plates, ideas, colors, numbers -- you name it, if you have a name for it, you can manipulate it, if not a physical object, then at least you can manipulate the name!

Computer programs manipulate information: numbers, names, groups of names and numbers representing physical objects or just ideas. In other words, stuff. The information can be very simple, or very complex but it has to go somewhere, in your hand, on the counter, on a plate, in your head, somewhere. The computer has millions of places to put numbers and names representing ideas and things and stuff. We call those places variables, because what you put there can vary from time to time.

Think of a variable as a container, like a glass of milk or orange juice. When you pour OJ into the glass from the jug, that's like Input:

If you drink the OJ (or pour it down the sink) from the computer's perspective, that would be Output.

Let's say we have three glasses, one is one quarter full of OJ (that would be 1), and another is two quarters (half) full of OJ (that would be 2). If you pour the first glass into the second (add it to the second), then it would be three quarters full (3). Computer variables are like that, when they have a quantity that can be added, you can add those numbers and get a sum (or subtract or multiply or divide), except pouring one variable into another doesn't empty the first, like this:

Also, computers don't do orange juice or milk, they do abstract numbers and text. Later you will learn about Objects, which are just collections of numbers and text, plus the programs to work on them.

There's a lot more to be said about variables, but it will be easier to understand when we do a real computer program with real numbers, not just a sandwich, but a four-function calculator like you can buy for a couple bucks at the Dollar store.

Normally you can mix and match these five things in any combination, whatever gets your job done, but the Kitchen computer doesn't seem to be able to do conditionals on the jam inside iterations. I tried, and it just didn't work. We can do other kinds of conditionals inside iterations with no problem, as we will see shortly.

OK, lets review what we know about computer programs -- about any program really:

A. A program is a sequence of steps to be executed in order, except

B. If we tell it to repeat (an iteration), it jumps back and does some part of the sequence over and over, or else

C. In a conditional we can tell the computer to omit some step based on some outside or computed condition. In the usual form you get a choice, left or right, red or green, yellow or blue, yes or no, sometimes a choice from several like our choice of jams, but never both or all at the same time.

And what kinds of steps do we do in (or out of) order? We can do

D. Input (so far reading the date, later we will accept information from a human user, like the bonus above) and output (printing a message on the screen, or changing some pixels in an image), and

E. Evaluating the value of a variable, which can then be used to control conditionals and iterations, and/or output to the human watching.

Those five things, every computer program, in its lowest detail, consists of those five things, and nothing else.

F. There is a sixth not-quite-primitive called subroutines, a sort of named box we can put around a bunch of steps so that you think of them as a unit, the way we call the sandwich we just made a "PBJ" which says nothing about the steps you went through to make it. You don't need to think about what's inside the box after you know it works correctly. More on that in another video.

So experiment some with making multiple sandwiches, and then we'll write a calculator program -- still in English, so you can understand what it does. And a couple of games. Then we'll do the same programs in Java, so you can see it's exactly the same.
 

[2021 August 26]