- COBAL uses the English grammar word "verb" to describe the commands available for performing tasks in COBAL. Let's take a look at a sample program that uses some of these verbs. In this program, on line 37, I use the INITIALIZE verb to initialize the variable WS-AGE. Later in the program, I use the current year and the birth year to calculate the age. The INITIALIZE verb is used to initialize a group or elementary data item.
When this is used, numeric date items are set to all zeroes, and alphanumeric or alphabetic date items are set to spaces. The next verb in this program is on line 38, the DISPLAY verb. The DISPLAY verb allows the program to display output from a COBOL program. This is really helpful when debugging your program. You can display intermediate data as the program steps through the code. This is not very useful in a fully implemented program since the end user does not usually interact directly with the console in a COBOL batch environment.
Below the display command, you'll see we have the ACCEPT verb, which allows the program to get data from the Operating System or directly from the user. An example of obtaining data from the Operating System is on line 42, ACCEPT WS-DATE FROM DATE. Note, when retrieving the system date, it is returned in the format of yy/mm/dd. So, as you can see in my program, lines 44 through 46 actually take the values from teh WS date and move them into the display month, the display day, and the display year.
As you can see, the MOVE verb is used to literally move data from one data field to another. It can be used with both group and elementary items. When it is used with group items, though, you must use the MOVE corresponding. Note that data will be truncated if the receiving variable of a MOVE statement is too small. If the destination is too large, it will be filled with zeroes or spaces, so that's not usually a problem. But this is something to keep in mind when working with COBOL.
Data overflow is often not captured as an error, so always make sure you allocate the correct amount of space for your variables. Just one last note about this program. As you can see, I used the verbs to display a message for the user to display their name, then I accept the name into the variable. WS-FIRST-NAME I ask the user what year they were born. I accept the year into the WS-BIRTH-YEAR variable. Then I get the date from the system, and I write back out the information.
On line 47, in order to calculate the birth year, I needed to calculate the current year using the year from the system date, and adding 2000. Then I compute the age by taking the new WS current year minus the birth year that the user entered. Then I print out what is today's date and the age. Let me run the program so you can see what it looks like. I've already compiled the program, so now I just need to run it. I'll enter in a name, Joe, and I'll say that Joe was born in 1985.
As you can see, the display commands now print out "Good job, Joe "Today's date is: 9/29/15" September 29th, 2015, and your age is 030. So, Joe is 30 years old. I hope this helps you to start to understand some of the verbs used in COBOL.
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