Using Loops in Programming Flowcharts and Pseudocode Please use speaker notes for additional information! condition Y processing N DO WHILE LOOP: The while loop shown here tests a condition to see if the processing should be done. If the answer to the condition is YES, the processing box shown is executed. If the answer to the condition is NO, the processing box shown is not executed. DO UNTIL LOOP: The while loop shown here executes the processing once and then tests a condition to see if the processing should be done. processing condition N Y This means that the loop will always be executed once since it is executed before checking is done. Once the processing has been done once, further processing is determined by the answer to the condition. If the condition gets a YES, then the processing is executed again. If the condition gets a NO, then the processing is not executed again. DO WHILE LOOP taxCt = 1 taxCt < 5 Y Set up tax bill Print tax bill Add 1 to taxCt N The looping structure where I want to use the while loop to do something a certain number of times, requires the following: • initialize the counter outside the loop • test the counter to determine whether or not to enter the loop • increment the counter inside the loop taxCt = 1 taxCt < 5 Y Set up tax bill Print tax bill N Pseudocode: taxCt = 1 while taxCt < 5 set up tax bil (move information to bill) print tax bill add 1 to taxCt end-loop Add 1 to taxCt Start Process Housekeeping Read a record Process Not EOF Y The loop shown here is executed until there are no more records on the file. Loop Terminate End-Process Stop Housekeeping Terminate Setup data Close files Open files End-Terminate End-Housekeeping The loop is shown on the next slide. DO WHILE LOOP DO WHILE LOOP Loop Set up tax bill information taxCt = 1 taxCt < 5 Y Set up tax bill Print tax bill N Read a record End-Loop The loop shown on this page prints 4 tax bills for each property owner. Add 1 to taxCt Pseudocode Mainline housekeeping() process() terminate() stop end-mainline Housekeeping() set up data open files return end-housekeeping Terminate() close files return end-terminate Process() read a record while not end of file loop() end-loop return end-process Loop() set up tax bill information taxCt = 1 while taxCt < 5 set up tax bill with additional data if needed print tax bill add 1 to taxCt end-loop read a record return end-loop Start Housekeeping Housekeeping Setup variables Process Enter money you have to pay bills Wrapup Stop Accept amountAvail End-Housekeeping Wrapup May not have paid last billcheck Cannot pay any more bills moneyAvail End-Wrapup Process is on the next page, Process Because I want to keep this processing simple, the user must enter the smallest bill first and move to the largest. So when a bill cannot be paid, processing is done. Enter amountBill & payTo Accept amountBill & payTo amountBill < moneyAvail N End-Process Y Loop Note that the only way out of this loop is to have a bill you cannot afford to pay. This means if you have no more bills to pay, entering a large number more than moneyAvail will cause an exit. DO WHILE LOOP Loop Set up information to pay bill Pay bill Because I want to keep this processing simple, the user must enter the smallest bill first and move to the largest. So when a bill cannot be paid, processing is done. Subtract amountBill from moneyAvail Ask for amountBill & payTo Accept amountBill & payTo End-Loop This is where I take in more data. I will then go back to process where I do the loop if I have the money to pay the bill. Mainline housekeeping() process() wrapup() stop end-mainline Housekeeping() set up data enter money you have to pay bills accept amountAvail return end-housekeeping Wrapup() print msg about last bill print msg about moneyAvail return end-wrapup Process() enter amountBill enter payTo accept amountBill & toPay While amountBill < moneyAvail loop end-loop return end-process Loop() set up information to pay bill print paid bill subtract amountBill from money Avail enter amountBill enter payTo accept amountBill & payTo return end-loop IDENTIFICATION DIVISION. PROGRAM-ID. C17LOOP. *ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 INPUT-VARIABLES. 05 AMOUNT-AVAIL 05 AMOUNT-BILL 05 PAY-TO 01 OUTPUT-VARIABLES. 05 AMOUNT-BILL-TO-SCR 05 AMOUNT-AVAIL-TO-SCR The numbers are input as pure numbers. The V remembers where the decimal point is if the user inputs decimal points. PIC 9(5)V99. PIC 9(5)V99. PIC X(20). PIC $$$,$$$.99. PIC $$$,$$$.99. Housekeeping Setup variables On this slide, I have shown the beginning of the COBOL program. The PROCEDURE DIVISION where the actual processing is done is shown on the next slide. This would correspond to setting up variables. The output wants to include decimal points and commas if needed. I setup these pictures to show the edited data I will display on the screen. PROCEDURE DIVISION. MAINLINE. PERFORM A-100-HOUSEKEEPING. PERFORM B-100-PROCESS. PERFORM C-100-WRAPUP. STOP RUN. A-100-HOUSEKEEPING. DISPLAY " " WITH BLANK SCREEN. DISPLAY " ". DISPLAY "ENTER THE AMOUNT OF MONEY AVAILABLE". ACCEPT AMOUNT-AVAIL. B-100-PROCESS. DISPLAY "ENTER THE AMOUNT OF THE FIRST BILL". ACCEPT AMOUNT-BILL. DISPLAY "ENTER WHO TO PAY THE BILL TO" ACCEPT PAY-TO. PERFORM B-200-LOOP UNTIL AMOUNT-BILL > AMOUNT-AVAIL. B-200-LOOP. DISPLAY "YOU HAVE PAID THE BILL TO " PAY-TO. MOVE AMOUNT-BILL TO AMOUNT-BILL-TO-SCR. DISPLAY "PAID THIS AMOUNT: "AMOUNT-BILL-TO-SCR. SUBTRACT AMOUNT-BILL FROM AMOUNT-AVAIL. DISPLAY " ". DISPLAY "ENTER THE AMOUNT OF THE NEXT BILL". DISPLAY "ENTER THE SMALLEST BILL LEFT TO PAY" ACCEPT AMOUNT-BILL. DISPLAY "ENTER WHO TO PAY THE BILL TO". ACCEPT PAY-TO. C-100-WRAPUP. DISPLAY "YOU MAY NOT HAVE PAID THE LAST BILL". DISPLAY "CHECK THE LISTINGS ON THE SCREEN". DISPLAY "CANNOT PAY ANY MORE BILLS". MOVE AMOUNT-AVAIL TO AMOUNT-AVAIL-TO-SCR DISPLAY "THIS IS WHAT YOU HAVE LEFT " AMOUNT-AVAIL-TO-SCR. PROCEDURE DIVISION. MAINLINE. PERFORM A-100-HOUSEKEEPING. PERFORM B-100-PROCESS. PERFORM C-100-WRAPUP. STOP RUN. A-100-HOUSEKEEPING. DISPLAY " " WITH BLANK SCREEN. DISPLAY " ". DISPLAY "ENTER THE AMOUNT OF MONEY AVAILABLE". ACCEPT AMOUNT-AVAIL. Housekeeping Start Setup variables Housekeeping Enter money you have to pay bills Process Wrapup Stop Accept amountAvail End-Housekeeping Process Enter amountBill & payTo B-100-PROCESS. DISPLAY "ENTER THE AMOUNT OF THE FIRST BILL". ACCEPT AMOUNT-BILL. DISPLAY "ENTER WHO TO PAY THE BILL TO" ACCEPT PAY-TO. PERFORM B-200-LOOP UNTIL AMOUNT-BILL > AMOUNT-AVAIL. Note that in the flowchart I said if amountBill is < moneyAvail execute the loop. Accept amountBill & payTo amountBill < moneyAvail N End-Process In the code it is expressed differently, here I say do the loop until the amountBill is greater than the amountAvail. Y Loop Essentially, I am saying the same thing but the language of COBOL led me to express it differently. Loop Set up information to pay bill Pay bill Subtract amountBill from moneyAvail Ask for amountBill & payTo Accept amountBill & payTo End-Loop B-200-LOOP. DISPLAY "YOU HAVE PAID THE BILL TO " PAY-TO. MOVE AMOUNT-BILL TO AMOUNT-BILL-TO-SCR. DISPLAY "PAID THIS AMOUNT: "AMOUNT-BILL-TO-SCR. SUBTRACT AMOUNT-BILL FROM AMOUNT-AVAIL. DISPLAY " ". DISPLAY "ENTER THE AMOUNT OF THE NEXT BILL". DISPLAY "ENTER THE SMALLEST BILL LEFT TO PAY" ACCEPT AMOUNT-BILL. DISPLAY "ENTER WHO TO PAY THE BILL TO". ACCEPT PAY-TO. C-100-WRAPUP. DISPLAY "YOU MAY NOT HAVE PAID THE LAST BILL". DISPLAY "CHECK THE LISTINGS ON THE SCREEN". DISPLAY "CANNOT PAY ANY MORE BILLS". MOVE AMOUNT-AVAIL TO AMOUNT-AVAIL-TO-SCR. DISPLAY "THIS IS WHAT YOU HAVE LEFT " AMOUNT-AVAIL-TO-SCR. Wrapup May not have paid last bill check Cannot pay any more bills - moneyAvail End-Wrapup I entered that the amount of money available was 10000 Now I am being prompted to enter the next bill. Then I entered the amount of the first bill for 1275.25 and that it was to be paid to JM Builders. The pament was confirmed. Now I have paid my second bill for 3000. This shows the next payment to Heights Roofers. I then attempted to pay H&J Builders 6000 but I did not have that amount left and the bill was not paid. I know this because I received no confirmation of payment and the message that the last bill may not have been paid. I also show the amount of money left. Start Process Housekeeping Read a record Process Not EOF Y The loop shown here is executed until there are no more records on the file. Loop Wrapup End-Process Stop Wrapup Housekeeping The loop is shown on the next slide. Set up total line Setup data Write total line Open files Close files End-Housekeeping End-Wrapup Note that I am writing a total line when I Wrapup this program. I a set up and then I write. DO WHILE LOOP Loop N JBCD=S Y Add SAL to TOTAL-SAL Set up the screen line Write to the screen Read a record End-Loop Mainline housekeeping() process() terminate() stop end-mainline Housekeeping() set up data open files return end-housekeeping Terminate() set up total line write total line close files return end-terminate Process() read a record while not end of file loop() end-loop return end-process Loop() if JBCD equals “S” add sal to total-sal set up the screen line write the screen line end-if read a record return end-loop IDENTIFICATION DIVISION. PROGRAM-ID. CIS17lab. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EMPLOYEE-FILE ASSIGN TO "A:\EMPLOYEE.DAT". DATA DIVISION. FILE SECTION. FD EMPLOYEE-FILE DATA RECORD IS EMPLOYEE-RECORD. 01 EMPLOYEE-RECORD. Layout of the input 05 IDNO PIC XXXXX. 05 EMPNAME PIC X(20). am reading. 05 YRBTH PIC 9(4). 05 GEN PIC X. 05 MARSTAT PIC X. 05 NUMDEP PIC 99. 05 ED PIC XXX. 05 JBCD PIC X. 05 PAYHR PIC 999V99. 05 SAL PIC 9(6)V99. 05 BONUS PIC 9(5). 05 DEPT PIC 99. 05 STATE PIC XX. WORKING-STORAGE SECTION. 01 PROG-INDICATORS. 05 END-OF-FILE-IND PIC XXX VALUE "NO ". 01 WORK-AREAS. 05 TOTAL-SAL PIC 9(8)V99 VALUE 0. 01 OUTPUT-TO-SCR. 05 SAL-FOR-SCR PIC $$$$,$$$.99. 05 TOTAL-SAL-FOR-SCR PIC $$$,$$$,$$$.99. Housekeeping Setup data file I Area to accumulate totals. Area to layout fields that will appear on the screen. PROCEDURE DIVISION. MAINLINE. PERFORM A-100-HOUSEKEEPING. PERFORM B-100-PROCESS. PERFORM C-100-WRAPUP. STOP RUN. A-100-HOUSEKEEPING. OPEN INPUT EMPLOYEE-FILE. DISPLAY " " WITH BLANK SCREEN. DISPLAY " ". B-100-PROCESS. READ EMPLOYEE-FILE AT END MOVE "YES" TO END-OF-FILE-IND. PERFORM B-200-LOOP UNTIL END-OF-FILE-IND = "YES". B-200-LOOP. IF JBCD = "S" ADD SAL TO TOTAL-SAL MOVE SAL TO SAL-FOR-SCR DISPLAY EMPNAME " " JBCD " " SAL-FOR-SCR END-IF READ EMPLOYEE-FILE AT END MOVE "YES" TO END-OF-FILE-IND. C-100-WRAPUP. DISPLAY " ". MOVE TOTAL-SAL TO TOTAL-SAL-FOR-SCR. DISPLAY "TOTAL SALARY FOR SALARIED EMPLOYEES " TOTAL-SAL-FOR-SCR. CLOSE EMPLOYEE-FILE. PROCEDURE DIVISION. MAINLINE. PERFORM A-100-HOUSEKEEPING. PERFORM B-100-PROCESS. PERFORM C-100-WRAPUP. STOP RUN. A-100-HOUSEKEEPING. OPEN INPUT EMPLOYEE-FILE. DISPLAY " " WITH BLANK SCREEN. DISPLAY " ". Start Housekeeping Process Wrapup Housekeeping Setup data Open files End-Housekeeping Stop B-100-PROCESS. READ EMPLOYEE-FILE AT END MOVE "YES" TO END-OF-FILE-IND. PERFORM B-200-LOOP UNTIL END-OF-FILE-IND = “YES”. Process Read a record Not EOF End-Process Y Loop B-200-LOOP. IF JBCD = "S" ADD SAL TO TOTAL-SAL MOVE SAL TO SAL-FOR-SCR DISPLAY EMPNAME " " JBCD " " SAL-FOR-SCR END-IF READ EMPLOYEE-FILE N AT END MOVE "YES" TO END-OF-FILE-IND. Loop JBCD=S Y Add SAL to TOTAL-SAL Set up the screen line Write to the screen Read a record End-Loop C-100-WRAPUP. DISPLAY " ". MOVE TOTAL-SAL TO TOTAL-SAL-FOR-SCR. DISPLAY "TOTAL SALARY FOR SALARIED EMPLOYEES " TOTAL-SAL-FOR-SCR. CLOSE EMPLOYEE-FILE. Wrapup Write total line Close files End-Wrapup