Join Peggy Fisher for an in-depth discussion in this video Walking through a sample program, part of Learning COBOL.
- Every COBOL program has four divisions. Identification, environment, data, and the procedure division. We're going to review each one of those in our little sample program. At the very top, on line one, is the identification division. This division contains the program name, in this case, the program-ID. sample Below that, I have the author name. The identification division is the first and only mandatory division of every COBOL program.
The programmer and the compiler use this division to identify the program. In this division, program ID is the only mandatory paragraph, and the program ID specifies the program name, and it can consist of one to thirty characters. If the program requires any file processing, the environment division is used to associate the file name, referenced in the program, to the input and output devices recognized by the operating system. The environment division contains all the information about the physical characteristics of input output data sources used by the program.
As you can see on line six, right below the environment division, I have something called the configuration section. This section provides information about the system on which the program is written and executed. It consists of two paragraphs, the source computer and the object computer. These two paragraphs are optional. Next, is the input output section. This section provides information about the files to be used in the program, and it also consists of two paragraphs, file control and IO control.
Since I'm not using any files for this program, I don't have anything else listed under the IO section. The next division, in my case, is on line 13, is the data division. The data division describes the record layout of the incoming record or records and the location of the data in the generated report. Data division is used to define the variables used in the program. It consists of four sections. There's a file section, which is used to define the record structure of the file.
COBOL needs to know exactly what data starts and ends in what position in the file and what data type. Next is working storage. Working storage is used to declare temporary variables and file structures which are used in the program. In my case, I only have one working storage variable named WS-name. It has a pic clause which we'll talk more about later. But as you can guess, the pic x parentheses 10 means that the name can be up to ten characters.
The data division might also have a local storage section which is similar to the working storage section. The only difference is that the variables will be allocated and initialized every time the program starts a new execution. And, finally, the linkage section is used to describe the data names that are received from an external calling program. On line 20, you see the procedure division. The procedure division contains the program logic. It contains the instructions for the computer to execute to solve a problem It consists of executable statements using variables defined in the data division.
In this division, paragraph and section names are user defined. As you can see, on line 21, I have a paragraph named 0001-hello-world. There must be at least one statement in the procedure division. The last statement to end the execution in this division is either stop run, which is used in the calling programs, or exit program, which is used in the called programs. Since my sample program is very small, I just have stop run.
You're probably wondering what this little program does. Well let me show you how to run this program from my CygWin terminal environment. I already have the program compiled, so in order to run it, I type ./ and the name of the program 01_05_sample Notice, I don't place the extension. And when I hit enter, it goes right into the program, into the procedure division, which the first actionable item is on line 23, where it says display the sentence enter your first name.
So I'll enter my name, Peggy, and then when I hit enter, it's going to display hello peggy, and stop the program. I know this is a very short sample of a COBOL program. Most COBOL programs are hundreds of lines, at least. But I hope you got an idea, of what a COBOL program looks like.
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