- Remember, COBOL is a sequential programming language, so it starts with the first paragraph in the procedure division, and continues to execute each command until it reaches a stop-run. There are situations where you need to redirect the execution of the steps in the program. One option is to use the go to command. I have to admit, there are not that many situations where it is useful to use the go to command, but let's go back to our car sales program. In this program, if the data in the file that we're reading is corrupted, it makes sense to end the program, rather than continue to process the rest of the records.
So let's change this program, so that is stops, if it encounters invalid data. I'm gonna scroll down in my program to the procedure division. On like 115 is the first paragraph to open the file. And then on line 21, I perform paragraph 200 to process each record in the sales file. Let me scroll down a little bit more, and you can see on line 150, is where I have a check to see if the data field, CARTOTAL, is numeric. If I switch over to my data file, carsales2.dat, you can see, I actually put in ABC as the number of cars sold by Matt Thomas.
So now, I want to change my program, so that if it encounters invalid data, it stops the program. Let's go back to our program. Again, on line 150, it says if car total is numeric, then, I go ahead and compute a running total of all the cars sold at this dealership. Then I have an else command, and I'm displaying invalid car sales, and I'm displaying the invalid data. At that point, I want to stop the program. So I'm gonna use the go to command.
So on like 156, I'm gonna add GO TO and I'll add another paragraph to the bottom of the program, which will exit the program, so I'll make it 0300. And let's call it STOP-RUN. Okay, now let's scroll down. And right above the stop run, I want to add 0300 STOP-RUN, the name of my paragraph, and inside the STOP-RUN paragraph, I want to stop run, but I also want to close the file.
So I'm gonna move CLOSE CARSALESFILE into this paragraph here. And now, the last thing I wanna do, is I still want to go to the STOP-RUN paragraph if everything end successfully. I'm gonna scroll back up to my OPENFILE paragraph, and after I process the records, I wanna say, PERFORM 0300 STOP-RUN. That way, even if the program runs successfully, it'll execute the stop run, where it will close the file, and end the program, and, the situation where there's an error will cause it to go to the STOP-RUN paragraph.
All right, let's save our changes. I'm gonna open my terminal window. Now I'm gonna compile my program. My new program is 03_04_car_sales. I'll hit Enter. I didn't get any error messages, so now I can run the program. ./03_04_ car_sales with no extension, and I hit Enter. Here's my report of my salespeople, and their quarterly sales, but notice, it immediately says, invalid car sales, ABC, and then the program ended.
So this is an example of a situation where the go to command, for branching, might come in handy.
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