Introduction to COBOL. COBOL COBOL is an acronym which stands for Common Business Oriented...

18
Introduction Introduction to to COBOL COBOL

Transcript of Introduction to COBOL. COBOL COBOL is an acronym which stands for Common Business Oriented...

Page 1: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

Introduction to Introduction to COBOL COBOL

Page 2: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

COBOLCOBOL

COBOL is an acronym which stands forCOBOL is an acronym which stands forCoCommon mmon BBusiness usiness OOriented riented LLanguage.anguage.

The name indicates the target area of COBOL applications.The name indicates the target area of COBOL applications.

COBOL is used for developing business, typically file-oriented, COBOL is used for developing business, typically file-oriented, applications.applications.

It is not designed for writing systems programs. You would It is not designed for writing systems programs. You would not develop an operating system or a compiler using COBOL.not develop an operating system or a compiler using COBOL.

COBOL is one of the oldest computer languages in use (it was COBOL is one of the oldest computer languages in use (it was developed around the end of the 1950s). As a result it has developed around the end of the 1950s). As a result it has some idiosyncracies which programmers may find irritating.some idiosyncracies which programmers may find irritating.

Page 3: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

The COBOL Metalanguage - Syntax NotationThe COBOL Metalanguage - Syntax Notation

Words in uppercase are reserved words.Words in uppercase are reserved words.

Words in mixed case represent names which will be Words in mixed case represent names which will be devised by the programmer.devised by the programmer.

When material is enclosed in braces { } a choice must When material is enclosed in braces { } a choice must be made from the options within the braces.be made from the options within the braces.

Material is enclosed in square brackets [ ] indicates that Material is enclosed in square brackets [ ] indicates that the material is an option which may be included or the material is an option which may be included or omitted as required.omitted as required.

The ellipsis symbol ‘...’ indicates that the preceding The ellipsis symbol ‘...’ indicates that the preceding syntax element may be repeated at the programmer’s syntax element may be repeated at the programmer’s discretion.discretion.

Page 4: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

ProgramProgramProgramProgram

DivisionsDivisions

Section(s)Section(s)

Paragraph(s)Paragraph(s)

Sentence(s)Sentence(s)

Statement(s)Statement(s)

Structure of COBOL programs.

Page 5: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

The Four Divisions.The Four Divisions.

DIVISIONS are used to identify the principal components DIVISIONS are used to identify the principal components of the program text. There are four DIVISIONS in all.of the program text. There are four DIVISIONS in all.

IDENTIFICATION DIVISION.IDENTIFICATION DIVISION.

ENVIRONMENT DIVISION.ENVIRONMENT DIVISION.

DATA DIVISION.DATA DIVISION.

PROCEDURE DIVISION.PROCEDURE DIVISION.

Although some of the divisions may be omitted the Although some of the divisions may be omitted the sequence in which the DIVISIONS are specified is fixed sequence in which the DIVISIONS are specified is fixed and and mustmust follow the pattern shown above. follow the pattern shown above.

Page 6: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

Functions of the four divisions.Functions of the four divisions.

The The IDENTIFICATION DIVISIONIDENTIFICATION DIVISION is used to supply is used to supply information about the program to the programmer information about the program to the programmer and to the compiler. and to the compiler.

The The ENVIRONMENT DIVISIONENVIRONMENT DIVISION describes to the describes to the compiler the environment in which the program will compiler the environment in which the program will run. run.

As the name suggests, theAs the name suggests, the DATA DIVISIONDATA DIVISION is used is used to provide the descriptions of most of the data to be to provide the descriptions of most of the data to be processed by the program. processed by the program.

The The PROCEDURE DIVISIONPROCEDURE DIVISION contains the description contains the description of the algorithm which will manipulate the data of the algorithm which will manipulate the data previously described. Like other languages COBOL previously described. Like other languages COBOL provides a means for specifying sequence, provides a means for specifying sequence, selection and iteration constructs.selection and iteration constructs.

Page 7: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

COBOL Program Text Structure COBOL Program Text Structure

Data Descriptions

Algorithm Description

IDENTIFICATION DIVISION.IDENTIFICATION DIVISION.

DATA DIVISION.DATA DIVISION.

PROCEDURE DIVISION.PROCEDURE DIVISION.

Program Details

Page 8: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

IDENTIFICATION DIVISION.IDENTIFICATION DIVISION.

The purpose of theThe purpose of the IDENTIFICATION DIVISION is to IDENTIFICATION DIVISION is to provide information about the program to the provide information about the program to the programmerprogrammer and to the and to the compilercompiler. .

Most of the entries in the IDENTIFICATION Most of the entries in the IDENTIFICATION DIVISION are directed at theDIVISION are directed at the programmerprogrammer and are and are treated by the compiler as treated by the compiler as commentscomments..

An exception to this is theAn exception to this is the PROGRAM-ID PROGRAM-ID clause. clause. Every COBOL program must have a PROGRAM-ID. Every COBOL program must have a PROGRAM-ID. It is used to enable the compiler to identify the It is used to enable the compiler to identify the program.program.

There are several other informational paragraphs There are several other informational paragraphs in the IDENTIFICATION DIVISION but we will ignore in the IDENTIFICATION DIVISION but we will ignore them for the moment.them for the moment.

Page 9: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

The IDENTIFICATION DIVISION Syntax.The IDENTIFICATION DIVISION Syntax.

The IDENTIFICATION DIVISION has the following The IDENTIFICATION DIVISION has the following structurestructureIDENTIFICATIONIDENTIFICATION DIVISIONDIVISION..PROGRAM-IDPROGRAM-ID. NameOfProgram.. NameOfProgram.[[AUTHORAUTHOR. YourName.]. YourName.]

The keywords The keywords IDENTIFICATION DIVISIONIDENTIFICATION DIVISION represent the represent the division header and signal the commencement of the division header and signal the commencement of the program text.program text.

The paragraph name The paragraph name PROGRAM-IDPROGRAM-ID is a keyword. It must is a keyword. It must be specified immediately after the division header.be specified immediately after the division header.

The program name can be up to 30 characters long.The program name can be up to 30 characters long.

Page 10: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

The IDENTIFICATION DIVISION Syntax.The IDENTIFICATION DIVISION Syntax.

The IDENTIFICATION DIVISION has the following The IDENTIFICATION DIVISION has the following structurestructureIDENTIFICATIONIDENTIFICATION DIVISIONDIVISION..PROGRAM-IDPROGRAM-ID. NameOfProgram.. NameOfProgram.[[AUTHORAUTHOR. YourName.]. YourName.]

The keywords IDENTIFICATION DIVISION represent the The keywords IDENTIFICATION DIVISION represent the division header and signal the commencement of the division header and signal the commencement of the program text.program text.

The paragraph name PROGRAM-ID is a keyword. It must The paragraph name PROGRAM-ID is a keyword. It must be specified immediately after the division header.be specified immediately after the division header.

The program name can be up to 30 characters long.The program name can be up to 30 characters long.

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. SequenceProgram.AUTHOR. Team Sarmag.

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. SequenceProgram.AUTHOR. Team Sarmag.

Page 11: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

The DATA DIVISION.The DATA DIVISION.

The The DATA DIVISIONDATA DIVISION is used to describe most of is used to describe most of the data that a program processes.the data that a program processes.

The DATA DIVISION is divided into two main The DATA DIVISION is divided into two main sections;sections;

FILE SECTION. WORKING-STORAGE SECTION.

The The FILE SECTIONFILE SECTION is used to describe most of is used to describe most of the data that is sent to, or comes from, the the data that is sent to, or comes from, the computer’s peripherals. computer’s peripherals.

The The WORKING-STORAGE SECTION WORKING-STORAGE SECTION is used to is used to describe the general variables used in the describe the general variables used in the program.program.

Page 12: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

The DATA DIVISION has the following structureThe DATA DIVISION has the following structure

DATA DIVISION SyntaxDATA DIVISION Syntax

DATA DIVISION.FILE SECTION. File Section entries.WORKING-STORAGE SECTION. WS entries.

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. Sequence-Program.AUTHOR. Team SarMag.

DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. Sequence-Program.AUTHOR. Team SarMag.

DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.

Page 13: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

The PROCEDURE DIVISION.The PROCEDURE DIVISION.

The PROCEDURE DIVISIONThe PROCEDURE DIVISION is where all the is where all the data data described in the DATA DIVISION is processed and described in the DATA DIVISION is processed and produced. It is here that the programmer describes produced. It is here that the programmer describes his algorithm.his algorithm.

The PROCEDURE DIVISION is hierarchical in structure The PROCEDURE DIVISION is hierarchical in structure and consists of Sections, Paragraphs, Sentences and and consists of Sections, Paragraphs, Sentences and Statements.Statements.

Only the Section is optional. There must be Only the Section is optional. There must be at least at least oneone paragraph, sentence and statement in the paragraph, sentence and statement in the PROCEDURE DIVISION.PROCEDURE DIVISION.

In the PROCEDURE DIVISION paragraph and section In the PROCEDURE DIVISION paragraph and section names are chosen by the programmer. The names names are chosen by the programmer. The names used should reflect the processing being done in the used should reflect the processing being done in the paragraph or section.paragraph or section.

Page 14: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

SectionsSections

A A sectionsection is a block of code made up of one or more is a block of code made up of one or more paragraphsparagraphs. .

A section begins with the section name and ends A section begins with the section name and ends where the next section name is encountered or where the next section name is encountered or where the program text ends.where the program text ends.

A section name consists of a name devised by the A section name consists of a name devised by the programmer or defined by the language followed by programmer or defined by the language followed by the word SECTION followed by a full stop.the word SECTION followed by a full stop.

SelectUlsterRecords SECTION. FILE SECTION.

Page 15: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

ParagraphsParagraphs

Each section consists of one or more paragraphs. Each section consists of one or more paragraphs.

A A paragraphparagraph is a block of code made up of one or is a block of code made up of one or more more sentencessentences..

A paragraph begins with the paragraph name and A paragraph begins with the paragraph name and ends with the next paragraph or section name or the ends with the next paragraph or section name or the end of the program text.end of the program text.

The paragraph name consists of a name devised by The paragraph name consists of a name devised by the programmer or defined by the language the programmer or defined by the language followed by a full stop. followed by a full stop.

PrintFinalTotals. PROGRAM-ID.

Page 16: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

Sentences and Statements.Sentences and Statements.

A paragraph consists of one or more sentences.A paragraph consists of one or more sentences.

A A sentence sentence consists of one or more consists of one or more statementsstatements and is and is terminated by a full stop.terminated by a full stop.

MOVE .21 TO VatRateCOMPUTE VatAmount = ProductCost * VatRate.

DISPLAY "Enter name " WITH NO ADVANCINGACCEPT StudentNameDISPLAY "Name entered was " StudentName.

A A statementstatement consists of a COBOL consists of a COBOL verbverb and an and an operandoperand or operands.or operands.

SUBTRACT Tax FROM GrossPay GIVING NetPay READ StudentFile

AT END SET EndOfFile TO TRUEEND-READ

Page 17: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

A Full COBOL program.A Full COBOL program.

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. SequenceProgram.AUTHOR. Team SarMag.

DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.

PROCEDURE DIVISION.CalculateResult. ACCEPT Num1. ACCEPT Num2. MULTIPLY Num1 BY Num2 GIVING Result. DISPLAY "Result is = ", Result. STOP RUN.

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. SequenceProgram.AUTHOR. Team SarMag.

DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.

PROCEDURE DIVISION.CalculateResult. ACCEPT Num1. ACCEPT Num2. MULTIPLY Num1 BY Num2 GIVING Result. DISPLAY "Result is = ", Result. STOP RUN.

Page 18: Introduction to COBOL. COBOL  COBOL is an acronym which stands for Common Business Oriented Language.  The name indicates the target area of COBOL applications.

The minimum COBOL program.The minimum COBOL program.

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. SmallestProgram.

PROCEDURE DIVISION.DisplayPrompt. DISPLAY "I did it". STOP RUN.

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. SmallestProgram.

PROCEDURE DIVISION.DisplayPrompt. DISPLAY "I did it". STOP RUN.