- If you're new to COBOL, meaning that you never took a COBOL programming class in college, it might seem quite different from other programming languages, such as Java, or C++, or C#. Remember, COBOL was written to be an English-like format for businesses to leverage the speed and capacity of a computer to perform complex and numerous transactions faster than humans. So when you first learn COBOL, it might seem a little wordy. The coding rules for COBOL, also known as the syntax, includes a period at the end of each statement.
Most of the language that you're probably familiar with use a semicolon instead. So you just have to remember, to end a statement in COBOL, use a period instead of a semicolon. I'm sure you've heard the stories of the programmer who had to use punch cards to write each statement of a COBOL program, and then take the cards to a special card reader, just to check the syntax before he ever tried to run the program. Well, these stories are very true, and I must say that you learn quickly to number your cards and carry heavy duty rubber bands with you at all times.
If you accidentally dropped your "code", meaning the cards, it was quite the job to get everything back in order when you have hundreds of cards for one program. Well, enough of the story telling. The next rule of COBOL is learning the specific columns where you need to write your code. A COBOL program must start with some of the commands in certain areas. Let's talk about these rules. We'll start with columns one to six. You'll notice, in the program that I have open in Notepad++, columns one to six have a sequential number.
It's optional to number your lines, but you can actually put in line numbers. Column seven, which is empty for most of the file, except for the green commented area, is used to start a comment. In order to start a comment, you put an asterisk in column seven. And you can see rows four, five, and six, all have asterisks in column seven. Columns eight to 11 are considered the A margin.
This is where you write your division headers, your section headers, your paragraph names, your FDs, or your file descriptions, and even start your zero-ones, which we'll talk more about later. Columns 12 to 72 are considered the B margin. All remaining entries begin in or further along than column 12. And finally, columns 73 to 80 are optional and can be used for program identification. In addition to the syntax rules mentioned before, there are many parts of a COBOL program that are required.
For example, the identification division is required, and it must have at least one item, the program ID. It might also include an author, the author's name, installation, which might be instructions, the date written, and the date compiled. Just like any other programming language, COBOL programs may contain syntax errors and logic errors. For example, a syntax error would be forgetting a period at the end of a statement or at the end of a paragraph title. So on line 23, if I took the period off of the paragraph, 0001-HELLO-WORLD, I would get an error when I complied the program.
Logic errors can be a little more difficult to find. For example, if you accidentally coded the program to expect the first string on a file to be the first name, but the file actually stored the last name first, to the computer, this is fine, since a string is just a series of characters. But when your report is displayed, having the first name as Smith might seem a little odd. It would really mess up a mailing. A common practice in COBOL is to add a comment at the top of the program, as I did here.
When I first learned COBOL, it was common practice to actually start the comment with a line of asterisks, the next line has an asterisk in line seven, and then your description can be one line or multiple lines, and then finally, closing your comment with another row of asterisks. So remember, COBOL programs require that certain commands start in certain columns. Column seven is your comment column. Column eight is considered the A margin, or margin A, and then columns 12 to 17 are the B margin.
And finally, don't forget that you put a period at the end of each statement. If you can remember those rules about COBOL, that'll really help when you first start to do your programming.
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