- So far, our programs have been relatively small, but most COBOL programs are quite lengthy. By breaking your program up into manageable pieces of code, or paragraphs, it helps make the program easier to debug and maintain. It is important to understand that the PERFORM statement is similar to the GO TO statement, but there is a key difference. The PERFORM statement transfers control to the paragraph and returns control back after the execution of the paragraph, whereas the GO TO transfers control and continues from that point to execute the remainder of the program sequentially.
Here is an updated version of my CARSALES program, which uses multiple paragraphs and PERFORM statements. Let's scroll down to the PROCEDURE DIVISION. Starting on line 121, I have my first PERFORM statement. I'm telling the program to PERFORM 0200-PROCESS-SALES UNTIL ENDOFSALESFILE. That's a flag that'll get triggered after the last record is read. This PERFORM statement transfers control to line 126, 0200-PROCESS-SALES.
It is also possible to have the program execute multiple paragraphs, using the PERFORM THRU syntax. So in my 0200-PROCESS-SALES paragraph, you'll notice on line 129, I have PERFORM 0250-MOVE-DETAILS THRU 0280-READ-RECORDS. The PERFORM THRU statement allows you to instruct the program to execute several paragraphs in order, and then return control to the next statement after the PERFORM THRU command.
So, in this case, my program will transfer control to line 132, which is the MOVE-DETAILS. It'll execute that paragraph, I'm gonna scroll down. It'll execute paragraph 260, which adds the sales to the working storage fields. It'll go to paragraph 270 and execute PROCESS-RECORDS, and finally it'll go to paragraph 280 where it reads the next record and checks for end of file. At that point, it's gonna go back up to paragraph 200 and if it's not ENDOFSALESFILE, it'll start all over again by reading the next record.
The PERFORM statement can be used to instruct the program to execute a paragraph of code, which I just showed you, or it can execute a block of code in the current paragraph. When it's used to execute a block of code in the current paragraph, we use the PERFORM statement followed by the code to be executed, and then we include an END-PERFORM to indicate the end of the statements to be executed. Again, in paragraph 200, you'll see we're using this type of statement to execute the paragraphs listed until the ENDOFSALESFILE is reached.
That's why on line 129, I don't have a period at the end of the statement, because that statement is part of the PERFORM paragraph on line 128, which is ended with the END-PERFORM on line 130. It might not be apparent at first, but the PERFORM statement is similar to a loop statement in other languages.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings