Perform vs Go To

21 Jun

Think of the PERFORM verb as a round-trip ticket, and the GO TO statement as a one-way ticket. The PERFORM verb causes execution of a paragraph elsewhere in the program and then immediately returns to its starting point. The GO TO statement moves to a new paragraph, executes that paragraph, and then continues on its merry way from that point. Be careful when combining a PERFORM and a GO TO statement, as this can potentially be disastrous. Check out this befuddled code:

This program starts with the PERFORM of the APPETIZER paragraph, displaying the words “EAT APPETIZER”, and then jumps to the DESSERT paragraph. The GO TO statement in the APPETIZER paragraph causes the flow of execution to jump out of the path intended in the PERFORM statement. Consequently, the program flow does not return to the statement immediately following the PERFORM,  the main course is never served, and the check is unpaid.  The only results of the execution of this program would be:
EAT APPETIZER
EAT DESSERT

A couple simple changes fix this problem. Change the PERFORM line from this:
PERFORM APPETIZER.
to this:
PERFORM APPETIZER THROUGH DESSERT
and remove “GO TO DESSERT” from the APPETIZER paragraph.

(many thanks to Bob, who pointed out the error in my original logic).

This change moves the return point from the end of the APPETIZER paragraph to the end of the DESSERT paragraph. The PERFORM returns to its starting point when it gets to the end of the DESSERT paragraph.   If we execute this program, we will see:

EAT APPETIZER
EAT MAIN COURSE
EAT DESSERT
PAY CHECK

Here’s yet another variation:  In the following program, one GO TO takes the program flow outside of the intended path of the PERFORM, and another one brings it back again.

The execution flow of this program goes like this:
1. The PERFORM statement, by using THRU, sets up a return position at the bottom of MAIN-COURSE.
2. The PERFORM statement then jumps to the beginning of APPETIZER.
3. APPETIZER, using a GO TO statement, jumps out of the path of the PERFORM to DESSERT.
4. DESSERT has its own GO TO that jumps back into the path of the PERFORM.
5. On reaching the end of MAIN-COURSE, the return position ( the one originally set up by the PERFORM in the ENTER-ROUTINE) is found and the flow returns to the statement right after the PERFORM in ENTER-ROUTINE.

Whew! That was tough! The output of this program would look like this:

EAT APPETIZER
EAT DESSERT
EAT MAIN COURSE
PAY CHECK

While I may enjoy eating a meal in this order, these are not the results we had expected. This program, my friends, shows how genuine spaghetti code is born.  This very simple program is extremely hard to follow.   A larger program can become downright impossible.  Now don’t get me wrong – I certainly use my share of GO TO statements in my programs every day, and I would never tell you not to use them.   My point here is just to caution you that combining PERFORM statements and GO TO statements can give you very unpredictable results, not to mention a killer headache.

0 Replies to “Perform vs Go To

  1. Why oh why do people use paragraphs and gotos in programs? A well structured program with sections is far easier to maintain and understand. Each section serves a specific function and can be called one or many times and reduce duplication of coding. COBOL85 has the EXIT PERFORM to remove the need for GOTO completely. Earlier versions of COBOL should ONLY use GOTO for conditional bypassing of code within the same section and NEVER between sections. This is FUNDAMENTAL COBOL guys 🙂

Leave a Reply to Bill Smith Cancel reply

Your email address will not be published.