Separate speaker notes to accompany presentation on loops:


Slide #1:

This presentation will cover a variety of ways that looping can be used to problem solve in logic and programming.

Slide #2:

This slide shows the classic while loop.  This loop is frequently used to control the processing in the program.

We have seen it used where the condition tested for end of file on a data file or user deciding to stop with user input.  In these examples, the processing is the processing that should be done on the record that was just read or the input the user just entered.  When the condition did not get a YES, processing was ended.

Slide #3:

Note that the programmer chooses which loop is more appropriate for what they want to accomplish.  If they want to always do the processing once, then the DO UNTIL is appropriate.  If they want to always check before processing and not do the processing even once unless the condition is true then they should use the DO WHILE.

Slide #4:

In my town, the property taxes are due quarterly, but all four bills are sent out in one mailing to cut down on mailing costs.  This means that I need a loop that will print four tax coupons.

By initializing outside the loop, I do it once before the loop starts.  And more importantly, I do it before the loop starts - everytime.  So in the example of the property taxes, I do it before I start to print the bills for each property owner in the town.

I use the counter to determine when to leave the loop.  In this case, I am saying that as long as the taxCt is less than 5 I should redo the loop.  This means I will do it 4 times.

I need to do something to effect the condition inside the loop. If I do not change the value of the taxCt inside the loop then the loop will never be able to end and I will have an endless loop.

Slide #5:

Now I am going to put the tax loop within a program that will process all of the property owners in the town.

Note that there are many styles that can accomplish the same thing.  I do this a little differently then your book, but either approach works.

Note that I frequently eliminate setting up data from the flowchart as it is always required and is done differently in different programming languages.  I deal more with the processing of the data then the establishing of the data.

Note that processing can include the initializing read or in some cases the initializing read can be done in housekeeping.  In this example, I am doing it in processing.

Slide #6:

Since I have done the initializing read in processing (remember it could also have been done in housekeeping) before I go to the loop, I have a record to process.  I process the record by getting the bill information from the record that I just read and then setting the taxCt=1 before entering the processing loop and then I process the four tax bills for each property owner. Again note that I increment taxCt in the loop and I test for taxCt less than 5 before entering the loop.

After I complete 4 iterations of the loop, I drop down and read another record.  The loop is done and I return to process.  The first thing that is done in process is checking to make sure the read was successful.  If it was then I will go and do the loop for the new property owner.  If the read was not successful, I will end process.

Slide #7:

In this example, I am paying bills until I run out of money. At the beginning of processing, the user enters the amount of money they have to pay bills.  The processing stops when the user has used up their money.

On this slide, I am asking the user to enter the money they have to pay bills. I am doing this in housekeeping because this program works for one person.  If a second person wants to pay bills they must rerun the program.  Therefore entering the amount is a one time thing that I am doing before processing starts - in housekeeping.

In wrapup, I am going to show how much money is left as I exit the program.

Slide #8:

The user must enter the amount of the first bill.  Remember this code only works if they enter the smallest bill and work up to the highest bill.  Because as soon as their is not money available to pay a bill the processing stops.  This may not be reality, but it is easier to code.  If you are working from lowest to highest and you hit a bill you cannot pay, then obviously you cannot pay any higher bills.

Slide #9:

Now I will process their bills - subtracting the amount each time a bill is written.  At the end of the loop it returns to processing and checks to see if there is more money to pay another bill.  If there is, the user enters the loop.

Slide #10:

Note that the user can use decimal points when inputting data but they cannot use any other characters like commas or decimals or the data will not be stored as a pure number.

Note that in processing I must move the pure input number to the name I gave it in the output variables so that it will show up with decimal points and commas etc.

Slide #11:

This shows the entire procedure division.  On the following slides I am going to break things down and match to the flowchart.

Slide #12:

Note that the two lines that say DISPLAY " " WITH BLANK SCREEN and DISPLAY " " are there to clear the screen, everytime you run a program.  They do not relate to this specific programs logic.

Slide #13:

Note that in the code, I alternated asking for data and receiving it.  In the flowchart, I grouped the requests and grouped the accepts.  This is acceptable in that the flowchart is a guide to doing the program - not to be blindly followed.

Slide #14:

Again I have mixed the setting up and displaying of the information.  The move statement is truly a setup.

And then I have changed the order as I ask for and receive the new information.  Again, that is merely a matter of better use of the language.

Slide #15:

This warns the user that the last bill may not have been paid.  I could have checked this, but again I wanted to keep the processing simple.

I also could have added other features.  For example telling the user how much they had left after each payment.

Slide #16:

This slide shows the execution of the program

Slide #17:

This shows the additional bills that were paid and the processing when there was no money left.

Slide #18:

The processing here is very similar to our other programs, but this time I am writing a total line after end of file is reached.  During the loop, I accumulate totals and then I write the total line after the processing is complete.

Slide #19:

In this example, I am accumulating totals for employees that have an S in the JBCD.  I am not processing records that do not have the S.

Slide #20:

This shows the set up of the data.  I have the layout of the input file, the end of file indicator to help me detect when processing is complete.  A work area where I will accumulate the total and an edited area that will display the data on the screen with editing characters like dollar signs, commas and decimal points.  The string of dollar signs is called floating dollar signs.  That means that the $ will float over to where it is needed and everything before the $ will turn to spaces.

Slide #21:

The procedure division will be laid out piece by piece on the following slides.

Slide #22:

The remaining code in housekeeping is to handle presenting a clean screen each time the program is executed.

The setup data in housekeeping was done in the DATA DIVISION as shown in the previous slide.

Slide #23:

This slide shows the processing.  It includes the initializing read and the execution of the loop repetitively until end of file, on the file that is being read, is reached,

Slide #24:

This shows the processing that is done if the job code is S.  The processing is to add to the total salary work variable, set up the screen line and write the information to the screen.

Slide #25:

This shows the writing of the total line and then the closing of the file in wrapup.

Slide #26:

This shows the output from executing the program called  This lists all of the salaried workers and generates a total line. 

Remember only salaried employees are listed and the total is for salaried employees only.