Chapter 1

18
Chapter 1 Your First COBOL program

description

Chapter 1. Your First COBOL program. What is COBOL?. COBOL is an acronym for Common Business Oriented Language. Programming language especially aimed at solving business problems . COBOL screens are 80 columns wide by 24 or 25 characters high and do not contain graphics. A COBOL program. - PowerPoint PPT Presentation

Transcript of Chapter 1

Page 1: Chapter 1

Chapter 1

Your First COBOL program

Page 2: Chapter 1

What is COBOL?

• COBOL is an acronym for Common Business Oriented Language.

• Programming language especially aimed at solving business problems.

• COBOL screens are 80 columns wide by 24 or 25 characters high and do not contain graphics.

Page 3: Chapter 1

A COBOL program

000100 IDENTIFICATION DIVISION.000200 PROGRAM-ID. HELLO.000300 ENVIRONMENT DIVISION.000400 DATA DIVISION.000500 PROCEDURE DIVISION.000600000700 PROGRAM-BEGIN.000800 DISPLAY "Hello world".000900001000 PROGRAM-DONE.001100 STOP RUN.

Page 4: Chapter 1

COBOL program parts -Divisions

• There are four divisions in COBOL.– Identification Division– Environment Division– Data Division – Procedure Division

• DON'T make typing errors. Some of the typing errors that can cause serious problems when you are compiling include misspelling the name of a DIVISION (for example, INDENTIFICATION DIVISION), adding an unnecessary hyphen (for example, DATA-DIVISION), or omitting any of the periods. Note that everything ends with a period (.)

Page 5: Chapter 1

COBOL program parts -Divisions

• The IDENTIFICATION DIVISION marks the beginning of a COBOL program. The name of the program, which you assign, will be entered as a statement in the IDENTIFICATION DIVISION.

• This division is used to identify basic information about the program. In this example, the IDENTIFICATION DIVISION contains only the PROGRAM-ID, HELLO.

Page 6: Chapter 1

COBOL program parts -Divisions

• ENVIRONMENT DIVISION, which is used to identify the environment in which the program is running. Remember that COBOL is intended to run on many different types of machines, and this section is used to handle the differences between various computers.

• Contains statements or commands to describe the physical environment in which the program is running. The main use of the ENVIRONMENT DIVISION is to describe the physical structure of files that will be used in the program.

Page 7: Chapter 1

COBOL program parts -Divisions

• The DATA DIVISION contains statements describing the data used by the program.

• Contain any data that the program operates on.

• All the data that the program uses will be defined in this division.

• The DATA DIVISION and the PROCEDURE DIVISION are the most important divisions in a COBOL program; they do 95 percent of the work.

Page 8: Chapter 1

COBOL program parts -Divisions

• PROCEDURE DIVISION. This is the meat of the program--the part that does the work intended by the programmer.

• The PROCEDURE DIVISION contains the COBOL statements that the program will execute after the program starts running.

• The PROCEDURE DIVISION is the real workhorse of a COBOL program.

• Without a PROCEDURE DIVISION, you wouldn't have a program, because all the other divisions are used to create the environment and data that are used by the PROCEDURE DIVISION to actually do something.

Page 9: Chapter 1

COBOL program partsProgram ID and Stop Run

• Most compilers require that only two things be present in a COBOL program--other than the four divisions--in order to compile it:– PROGRAM-ID– STOP RUN

Page 10: Chapter 1

COBOL program partsProgram ID and Stop Run

• The PROGRAM-ID is a paragraph that must appear in the IDENTIFICATION DIVISION and is used to give the program a name.

• There must also be one paragraph in the PROCEDURE DIVISION that contains the STOP RUN statement.

• Most versions of COBOL require this explicit command as a way of identifying the point in the program where the program terminates.

Page 11: Chapter 1

COBOL – Sample program000100 IDENTIFICATION DIVISION.000200 PROGRAM-ID. SENTNCES.000300 ENVIRONMENT DIVISION.000400 DATA DIVISION.000500 PROCEDURE DIVISION.000600000700 PROGRAM-BEGIN.000800 DISPLAY "This program contains four DIVISIONS,".000900 DISPLAY "three PARAGRAPHS".001000 DISPLAY "and four SENTENCES".001100 PROGRAM-DONE.001200 STOP RUN.

Page 12: Chapter 1

COBOL source code

• A COBOL source code file has five areas, extending from left to right across the page.

• The first six characters or columns of a line are called the sequence number area.– This area is not processed by the compiler, or if it

is processed, it provides you only with warnings that numbers are out of sequence (if they are).

.

Page 13: Chapter 1

COBOL source code

• Character position 7 is called the indicator area.– This seventh position is usually blank.– If an asterisk (*) is placed in this column,

everything else on that line is ignored by the compiler.

– This is used as a method to include comments in your source code file.

Page 14: Chapter 1

COBOL source code - Area A vs Area B

• The four character positions 8 through 11 are called Area A.

• DIVISIONs and paragraphs (and SECTIONs) must start in Area A.

• It is good coding practice to start DIVISIONs, SECTIONs, and paragraph names at column 8 rather than some random place in Area A.

Page 15: Chapter 1

COBOL source code - Area A vs Area B

• Character positions 12 through 72 are called Area B.

• Sentences must start and end within Area B. • It is good coding practice to start sentences at

column 12 rather than some random place in Area B.

Page 16: Chapter 1

COBOL source code - Area A vs Area B

• COBOL was designed as an 80-column language, but there is no formal definition of character positions 73 through 80. This is called the identification area (which has nothing to do with the IDENTIFICATIONDIVISION).

• Some special COBOL editors place a modification code automatically in positions 73 through 80.

• This method of marking lines as modified usually depends on a special editor set up for COBOL that inserts these codes automatically.

• You probably will never see modification codes using COBOL on a PC.

Page 17: Chapter 1

COBOL source code - Area A vs Area B

000100 IDENTIFICATION DIVISION.000200 PROGRAM-ID. COMMENT.000300 ENVIRONMENT DIVISION.

DATA DIVISION. MB072197000500 PROCEDURE DIVISION.000600000700* This is a comment. MB072197000800* This paragraph displays information about the program. MB072197000900 PROGRAM-BEGIN.003700 DISPLAY "This program contains four DIVISIONS,". MB072197003800 DISPLAY "three PARAGRAPHS". MB072197001000 DISPLAY "and four SENTENCES".001100 PROGRAM-DONE.001200 STOP RUN.

Page 18: Chapter 1

What is a Shell Program?

• A COBOL program that contains the minimum requirements usually is called a shell program, a skeleton program, or a boilerplate program.

IDENTIFICATION DIVISION.PROGRAM-ID. COBSHL01.ENVIRONMENT DIVISION.DATA DIVISION.PROCEDURE DIVISION.PROGRAM-BEGIN.

PROGRAM-DONE.STOP RUN.