310 likes | 688 Views
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.
E N D
Using Loops in ProgrammingFlowcharts 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. 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. processing condition Y N
DO WHILE LOOP taxCt = 1 taxCt < 5 Y Print tax bill Add 1 to taxCt Set up tax bill 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 Print tax bill Add 1 to taxCt Set up 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
The loop shown here is executed until there are no more records on the file. Start Process Housekeeping Read a record Process Not EOF Loop Y Terminate End-Process The loop is shown on the next slide. Stop Terminate Housekeeping Setup data Close files Open files End-Terminate End-Housekeeping DO WHILE LOOP
DO WHILE LOOP Loop Set up tax bill information taxCt = 1 taxCt < 5 Y Print tax bill Add 1 to taxCt Set up tax bill N Read a record The loop shown on this page prints 4 tax bills for each property owner. End-Loop
Pseudocode Process() read a record while not end of file loop() end-loop return end-process Mainline housekeeping() process() terminate() stop end-mainline 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 Housekeeping() set up data open files return end-housekeeping Terminate() close files return end-terminate
Start Housekeeping Wrapup Housekeeping Setup variables May not have paid last bill- check Enter money you have to pay bills Process Wrapup Cannot pay any more bills - moneyAvail Accept amountAvail Stop End-Wrapup End-Housekeeping 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 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. amountBill < moneyAvail Loop Y N End-Process DO WHILE LOOP
Loop 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. Set up information to pay bill Pay bill Subtract amountBill from moneyAvail Ask for amountBill & payTo 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. Accept amountBill & payTo End-Loop
Mainline housekeeping() process() wrapup() stop end-mainline Process() enter amountBill enter payTo accept amountBill & toPay While amountBill < moneyAvail loop end-loop return end-process Housekeeping() set up data enter money you have to pay bills accept amountAvail return end-housekeeping 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 Wrapup() print msg about last bill print msg about moneyAvail return end-wrapup
IDENTIFICATION DIVISION. PROGRAM-ID. C17LOOP. *ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 INPUT-VARIABLES. 05 AMOUNT-AVAIL PIC 9(5)V99. 05 AMOUNT-BILL PIC 9(5)V99. 05 PAY-TO PIC X(20). 01 OUTPUT-VARIABLES. 05 AMOUNT-BILL-TO-SCR PIC $$$,$$$.99. 05 AMOUNT-AVAIL-TO-SCR PIC $$$,$$$.99. The numbers are input as pure numbers. The V remembers where the decimal point is if the user inputs decimal points. 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. 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.
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 Accept amountAvail Wrapup Stop End-Housekeeping
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. Process Enter amountBill & payTo Note that in the flowchart I said if amountBill is < moneyAvail execute the loop. In the code it is expressed differently, here I say do the loop until the amountBill is greater than the amountAvail. Essentially, I am saying the same thing but the language of COBOL led me to express it differently. Accept amountBill & payTo amountBill < moneyAvail Loop Y N End-Process
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. Loop Set up information to pay bill Pay bill Subtract amountBill from moneyAvail Ask for amountBill & payTo Accept amountBill & payTo End-Loop
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 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 am being prompted to enter the next bill.
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.
The loop shown here is executed until there are no more records on the file. Start Process Housekeeping Read a record Process Not EOF Loop Y Wrapup End-Process The loop is shown on the next slide. Stop Wrapup Set up total line Housekeeping Note that I am writing a total line when I Wrapup this program. I a set up and then I write. Setup data Write total line Open files Close files End-Housekeeping End-Wrapup DO WHILE LOOP
Loop JBCD=S N Y Add SAL to TOTAL-SAL Set up the screen line Write to the screen Read a record End-Loop
Process() read a record while not end of file loop() end-loop return end-process Mainline housekeeping() process() terminate() stop end-mainline Housekeeping() set up data open files return end-housekeeping 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 Terminate() set up total line write total line close files return end-terminate
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. 05 IDNO PIC XXXXX. 05 EMPNAME PIC X(20). 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 Layout of the input file I am reading. 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 Stop Setup data Open files End-Housekeeping
B-100-PROCESS. READ EMPLOYEE-FILE AT END MOVE "YES" TO END-OF-FILE-IND. PERFORMB-200-LOOP UNTIL END-OF-FILE-IND = “YES”. Process Read a record Not EOF Loop Y End-Process
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. Loop JBCD=S N 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