Iteration


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

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

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

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 two thirds 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.

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. If you have difficulty understanding or getting it to do what you want, use the Mentor Alert button here or in the IDE:

[Memo: When the server is fully functional, it can detect that the program ran correctly without errors or warnings, and replace the Mentor Alert button above with the following paragraph...]

Congratulations! Your program ran correctly. Click this link to advance to the Calculator:

[2021 December 9]