Iteration

 Zoom Name:


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

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

When it runs correctly (without errors or warnings) click the yellow Done button.
Then come back here and click this Next link to skip down to the next video.

If it's not a link yet, you might need to refresh your browser, or else click this button to call a mentor:

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 technical 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:

"Make PBJ"
  get stuff
  put it 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 near the front of the program:

       get bag from drawer

...and this line at the end:

       put sandwich into bag

...like this:

"Make PBJ"
  get stuff
  get bag from drawer
  put it together
  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 two lines (including the new last line you added) three times ... except it doesn't work. Some of the "stuff" that you'd think it got ready in the "get stuff" line is actually what we call "Just-In-Time" (JIT) processing, where it didn't open the loaf of bread until it needed a slice, and it didn't open the jam until it was ready to spread it. When a big retail chain does JIT restocking, Bad Things Happen, like when they didn't know about the winter storm coming, so you get to the store and they're all out. Programming has the same problem: we need to plan ahead. It's called "Design" and it's what people do and machines cannot.

When you got rid of assumptions in your PBJ, maybe you did it like we did, with a subroutine named "put it together" in which you dumped all the extra code that was necessary to get rid of the assumptions, like this:

"put it together"
    Open PB
    Open bread.
    Open jam.
    Get piece of bread.
    Get piece of bread.
    get jam on knife
    Spread jam on bread.
    get pb on knife
    Spread PB on bread.
    Put bread pieces together.
    done


The first three lines are about opening the stuff you already got in a different subroutine. That kind of thing should only happen once for all three sandwiches. So I moved those three lines to the end of the "get ingredients" subroutine. Sequence, getting things in the right order, is an important part of programming. Sometimes we don't know what the right order is until we try the wrong order and it doesn't work.

OK, now we can replicate the two lines that make the sandwich and (now also) put it in the bag:

"Make PBJ"
  get bag from drawer
  get stuff
  put it together
  put sandwich into bag {#1}
  put it together
  put sandwich into bag {#2}
  put it together
  put sandwich into bag {#3}


But if you are doing something in a database for a thousand customers, that would make a very big program. It's the way the computer does it, and the way a person does it, but we want to make the program itself shorter. It's called "Code reuse" and it's very important.

Think of telling your little sister how to make three PBJ sandwiches: "You open the cupboard and get out the bread and PB and jam, [and so on] ... and spread the jam and put the slices together and put the sandwich in the bag. Then you get two more pieces of bread and get PB on the knife and..." No, she can remember the steps she just went through to make the first sandwich, so you say simply "Do it three times," and she knows what you mean. That's what we tell the computer.

So let's go back to the 1-sandwich version (delete the duplicates), and insert just before you put the first sandwich together, 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
 
...like this:
"Make PBJ"
  get stuff
  get bag from drawer
  Repeat 3
    put it together
    put sandwich into bag
    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 in your program somewhere before the new Repeat line 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). I didn't see it stop early, maybe it recycles the bread.

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:

Congratulations! Your program ran correctly. Click this link to advance to Variables which has some exercises in preparation for the Calculator:

[2022 September 28]