A program in COBOL is written as a sequence of lines. To facilitate writing of COBOL programs, printed forms known as COBOL CODING SHEET.
Page Line Area A Area B Identification
No No
The 80 positions of a line are divided into areas. Every position in a line is also known as Column.
The COBOL Coding sheet has 80 columns.
The first six columns are called “Sequence number”, column. The first three columns used for page numbers. The columns 4 – 6 are used for line numbers.
Column 7 is called the continuation column. If we put “*” or “/” in column7 that line will not be executed by the computer. The symbol “/ “may be put in column 7 to cause program printing from a fresh page.
If we put “- “ in this column, it means if the statement is exceed more than 72 columns, the continuation should be expanded with the hyphen in column 7.
Column 8 to 11 is known as “Area A“(or) “Margin A”. Here we write all headers for example, division headers, section headers and paragraph headers, Level numbers also printed in this column.
Column 12 to 72 is known as “Area B” or “Margin B”. Here we write all COBOL statements. Area entries can be continued into Area B, if required.
Column 73 to 80 may be used for identification purpose. These columns are non-executed in all times. The use of this field is optional.
-------| Area A
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. COMMENT.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500 PROCEDURE DIVISION.
000600
000700* This is a comment.
000800* This paragraph displays information about the program.
000900 PROGRAM-BEGIN.
----------| Area B
003700 DISPLAY "This program contains four DIVISIONS,".
003800 DISPLAY "three PARAGRAPHS".
001000 DISPLAY "and four SENTENCES".
-------| Area A
001100 PROGRAM-DONE.----------| Area B
001200 STOP RUN.
ANALYSIS: As a historical note, the very first COBOL programs were written using punch cards. Each card carried one line of code that had been carefully entered using a keypunch machine (a kind of typewriter that punches holes in cards). The stack of punched cards was carried to the computer and fed into it using a card reader. An "out of sequence" warning was used to let you know that you probably had dropped the punch card deck and hadn't put them back together in the correct sequence. Compiler error messages also referred to line numbers, and locating an error was difficult without line numbers on the cards. PC COBOL compilers rarely give warnings about sequence.
Lines 000700 and 000800 contain an asterisk in column 7, the indicator area. Everything beyond the asterisk is ignored and can be used as a comment, as in the example.
DIVISIONs and paragraphs start in Area A but can extend into Area B.
Sentences begin and end in Area B. In Listing 1.5, sentences appear at lines 003700, 003800, 001000, and 001200.
What Is a Shell Program?
Because COBOL has a certain minimum amount of code that is required for all programs, it is a good practice to maintain a COBOL program that contains the minimum requirements.
New Term: A COBOL program that contains the minimum requirements usually is called a shell program, a skeleton program, or a boilerplate program.
If you copy this shell program to a new file before you start editing the new program, you can save yourself extra typing. Listing 1.6, cobshl01.cbl, is your first version of the COBOL shell program. As you progress through the days, you'll gradually add more and more pieces to the shell; eventually, you'll have a complete shell to use in all projects.
TYPE: Listing 1.6. Your first version of a COBOL shell.
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. COBSHL01.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500 PROCEDURE DIVISION.
000600 PROGRAM-BEGIN.
000700
000800 PROGRAM-DONE.
000900 STOP RUN.
Type Listing 1.6 and compile it to ensure that everything in it is correct. You will use this shell, and versions of it, many times before you are through with these lessons.
As you will see in the next few days, PC-based COBOL compilers are much less stringent about following the rules described in today's lesson. However, COBOL is intended to be a portable language that allows code to be moved to other computers. Another computer probably will be using another COBOL compiler that might require compliance to all the strict rules of COBOL. When you start breaking the rules, you limit your code to a compiler that can handle the loose syntax, and this might make it extremely difficult to move your code to another machine or compiler.
Summary
Today's lesson introduced you to some computer and programming basics, including the following:
- A computer processes numbers and is made up of a central processing unit, input devices, output devices, main memory, and secondary storage.
- A computer can't do anything without a program.
- A program is a series of instructions that the central processing unit executes to process data, usually using some input data and providing some sort of output.
- A programming language is a method of writing a source code file in an English-like language that a human being can understand.
- A compiler translates the source code file into instructions that the central processing unit can understand and execute.
- A COBOL program contains four DIVISIONs: the IDENTIFICATION DIVISION, the ENVIRONMENT DIVISION, the DATA DIVISION, and the PROCEDURE DIVISION.
- A DIVISION can be broken down into paragraphs. Some divisions have required paragraphs, such as the PROGRAM-ID paragraph in the IDENTIFICATION DIVISION.
- The names of the paragraphs in the PROCEDURE DIVISION are assigned by the programmer.
- The work of a COBOL program is done in sentences that contain the commands of a program and appear within a paragraph.
- A COBOL program is written in 80-column format. The columns are divided into the following areas:
- Columns 1 through 6 are the sequence area and can be used by the programmer for line numbering. Line numbering is optional.
- Column 7 is the indicator area. An asterisk (*) in column 7 causes everything to the right of the asterisk to be treated as a comment.
- Columns 8 through 11 are Area A, and columns 12 through 72 are Area B. DIVISION names, SECTION names, and paragraph names must begin in Area A, but they can extend into Area B. Sentences begin and end in Area B.
- Columns 73 through 80 are undefined and are not processed by the COBOL compiler, but some COBOL editors use these columns to tag lines with modification codes.
- Columns 1 through 6 are the sequence area and can be used by the programmer for line numbering. Line numbering is optional.
Today, you also learned how to type, compile, and run several simple COBOL programs.
Q&A
- Q Why is so much of COBOL in uppercase?
A COBOL was developed in the days when computer terminals and keypunch machines used only uppercase letters. The entire language was defined in terms of this all-uppercase state of affairs. You can display uppercase and lowercase messages, such as Hello world, because terminals now have uppercase and lowercase capability. However, the actual elements of COBOL--the DIVISIONs, the paragraph names such as PROGRAM-BEGIN, and the verbs such as DISPLAY--originally were designed in uppercase only.
Q What does a blank line in a program do?
A Nothing. The compiler skips blank lines. You can put blank lines anywhere you want in the program to improve readability.
Q Does COBOL use line numbers?
A Yes, but they are optional. Most compilers ignore them or can be set to ignore them. Some compilers on larger computers will process the line numbers and provide a warning if the line numbers are out of sequence, but this is not an error.
Q Can I put anything in a comment?
A Yes. As long as the asterisk appears at column 7, everything after the asterisk is ignored and has no effect on the compiler. You can write English sentences or gobbledygook, although the usual practice is to provide some information that describes what the program is doing, or why it is doing it.
> You can do this on as many lines as necessary to complete the comment.
Q Will my comments appear in the program when it runs?
A No. Comments appear only in the source code file, and they will not be included in the compiled program. Comments are intended to document the source code. The computer can read only the compiled code, and the comments wouldn't mean anything to the computer, so they are not included in the resulting program.
Q Why do paragraph names have hyphens in them?
A Paragraph names in COBOL are limited to 30 characters and may only contain uppercase letters (A through Z), digits (0 through 9), and the hyphen (-). The hyphen is used to improve the readability of paragraphs and the names of data (which is covered in Day 2). Some modern compilers allow paragraph names, division names, and other elements of COBOL to be typed in lowercase.
Workshop
Quiz
- 1. What is the output of the following program?
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. BYEBYE.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500 PROCEDURE DIVISION.
000600
000700 PROGRAM-BEGIN.
000800 DISPLAY "Bye bye birdie".
000900 PROGRAM-DONE.
001000 STOP RUN.
- 2. How many DIVISIONs are in the following program, byebye.cbl?
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. BYEBYE.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500 PROCEDURE DIVISION.
000600
000700 PROGRAM-BEGIN.
000800 DISPLAY "Bye bye birdie".
000900 PROGRAM-DONE.
001000 STOP RUN.
- 3. How many paragraphs?
4. How many sentences?
5. What is wrong with the following, bad01.cbl?
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. BAD01.
000300 ENVIRONMENT DIVISION.
000400
000500 PROCEDURE DIVISION.
000600
000700 PROGRAM-BEGIN.
000800 DISPLAY "I'm bad!".
000900 PROGRAM-DONE.
001000 STOP RUN.
- 6. What is wrong with the following, bad02.cbl?
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. BAD02.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500 PROCEDURE DIVISION.
000600
000700 PROGRAM-BEGIN.
000800 DISPLAY "I'm bad!".
000900 PROGRAM-DONE.
001000 STOP RUN.
- Hint: Where are sentences supposed to begin?
Note: Some compilers might not give an error on compiling BAD02, but might only provide a warning.
7. What is wrong with the following, bad03.cbl?
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. BAD03.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500 PROCEDURE DIVISION.
000600 This program displays a message.
000700 PROGRAM-BEGIN.
000800 DISPLAY "I'm really bad!".
000900 PROGRAM-DONE.
001000 STOP RUN.
Exercises
- 1. Modify the hello.cbl program to display I am a COBOL programmer.
Hint: Copy the hello.cbl program to iam.cbl:
copy hello.cbl iam.cbl
- Then use your editor or pcobol to change the PROGRAM-ID and DISPLAY statements.
2. Use the computer to compile each of the bad examples (bad01.cbl, bad02.cbl, and bad03.cbl) to get a feel for the types of error messages that your compiler produces.
COBOL-85 compilers are much more relaxed than earlier standards. The ENVIRONMENT, DATA, and PROCEDURE DIVISIONs are optional under ANSI 85, although what a program would do without a PROCEDURE DIVISION is a bit of a mystery. The results of compiling these programs are interesting.
Both Micro Focus Personal COBOL and ACUCOBOL found nothing wrong with bad01.cbl. Apparently, if a program contains no data, it doesn't need to have a DATA DIVISION.
Micro Focus Personal COBOL handled bad02.cbl without a hiccup.
ACUCOBOL produced a warning that a sentence was starting in Area A.
Both Micro Focus Personal COBOL and ACUCOBOL generated errors on bad03.cbl and would not compile it.
3. Modify bad01.cbl so that it compiles without errors or warnings.
4. Modify bad02.cbl so that it compiles without errors or warnings.
5. Modify bad03.cbl so that it compiles without errors or warnings.
No comments:
Post a Comment