COBOL - Introduction

72
COBOL (Common Business Oriented Language)

Transcript of COBOL - Introduction

Page 1: COBOL - Introduction

COBOL (Common Business Oriented Language)

Page 2: COBOL - Introduction

Introduction

Page 3: COBOL - Introduction

History

1959 – Conference on Data System Languages (CODASYL)

created the first standardized business computer programming language

1960 – First proposal for COBOL – named COBOL-60

Page 4: COBOL - Introduction

History

1961 – First COBOL compilers are getting used

1965 – The usage of COBOL starts to increase a lot (COBOL edition

1965).

Page 5: COBOL - Introduction

History

1968 – The American National Standards Institute (ANSI) sets the first official COBOL: COBOL-68.

1970 – The International Organization for Standardization (ISO) makes ANSI’s COBOL-68 an

international standard.

Page 6: COBOL - Introduction

History

1973 – Machine Independent COBOL presented.

1974 – ANSI published a revised version of (ANSI) COBOL, COBOL-74.

1985 – The new COBOL-85 standard is set (ANSI X3.23-1985).

Page 7: COBOL - Introduction

History

1989 – Intrinsic functions are added to the standard (ANSI

X3.23a- 1989). 1997 – Object-oriented features are

added to Structured COBOL.

Page 8: COBOL - Introduction

History

2002 – The long awaited object oriented COBOL 2002 standard is set.

– Information on 2008 COBOL standard at http://www.ansi.org

Page 9: COBOL - Introduction

Features

As a programming language Oriented towards business applications Supports Procedural Programming (from

COBOL-60) Supports Structured Programming (from

COBOL-68)

Page 10: COBOL - Introduction

Features

As a programming language Supports Machine Independent

Programming (from COBOL-73) Supports Object oriented programming

(from COBOL-2002)

Page 11: COBOL - Introduction

Features

As a high-level language English like Problem oriented Self-documenting Multi-platform support Easy to learn/read/write and maintain

Page 12: COBOL - Introduction

Program Structure

PROGRAM

DIVISIONS

SECTIONS

PARAGRAPHS

ENTRY SENTENCES

STATEMENTCLAUSE

PHRASEWORDS CHARACTERS

Page 13: COBOL - Introduction

Character Set

Digits (10) 0, 1-9 Letters (26) A, B…Z Blank Space (01) b Symbols (12) + - * / , . ( ) = $ ; “ Conditional (02) < >

Total: 51 Characters in COBOL-74

Page 14: COBOL - Introduction

Character Set

Reference Modifier (01) :Total: 52 Characters in COBOL-85

Apostrophe (01) ‘Total: 53 Characters in Mainframe COBOL

Note: Mainframe COBOL also includes small letters (a to z), for nonnumeric literals.

Page 15: COBOL - Introduction

Reserved Words

Keywords The spelling of words are American

English and not British COBOL supports abbreviations in many

cases

Page 16: COBOL - Introduction

User Defined Words

Should not be a Reserved Word 1 - 30 characters Letters, digits, hyphens (-) only At least one Letter or Hyphen

(Recommended to use the first character as an alphabet)

Page 17: COBOL - Introduction

User Defined Words

Hyphen/s should be embedded. Spaces are not allowed. Must be unique within a set

Page 18: COBOL - Introduction

Literals

User defined constants Can be Numeric literal, Floating point

literal and Non-numeric literal

Note : A non-numeric literal can hold a character which is not available in COBOL character set.

Page 19: COBOL - Introduction

Literals

Numeric Literals May consist of numerals decimal point (.)

1 decimal character is allowed Should not be the rightmost character

plus or minus sign 1 sign character (+ or -) is allowed Leftmost character

Not enclosed in quotes

Page 20: COBOL - Introduction

Literals

Numeric Literals

123 123.45 -256 +2987

Examples

Page 21: COBOL - Introduction

Literals

Floating Point Literals Support a much wider range of values

than numeric literal

In Mathematics In COBOL12345000 1.2345*107 1.2345E07 or

+12.345E+06

Example

Page 22: COBOL - Introduction

Literals

Non-Numeric Literals Enclosed in quotes (“) May consists of alphanumeric characters 1 – 160 characters An embedded quotation mark must be

represented by a pair of quotation marks (“ “)

Page 23: COBOL - Introduction

Literals

Non-Numeric Literals The opening delimiter (“) cannot split

across lines.

Note: COBOL-74 compilers accept only 120 characters.

Page 24: COBOL - Introduction

Figurative Constants

Predefined constants, but reserved words

Except ZERO/ZEROES, others are treated as Non-Numeric Literals

Page 25: COBOL - Introduction

Figurative Constants

ZERO, ZEROS, ZEROES (0)QUOTE, QUOTES (“)SPACE, SPACESALL Represents 1 or more occurrences of

the string*Not used in Mainframe

Examples

Page 26: COBOL - Introduction

Figurative Constants

NULL, NULLS To indicate an invalid address for a POINTER variable;Has the value Zero in Mainframe COBOL

Examples

Page 27: COBOL - Introduction

Figurative Constants

HIGH-VALUE, HIGH-VALUES Highest value in the collating sequence

Examples

Page 28: COBOL - Introduction

Figurative Constants

LOW-VALUE, LOW-VALUES Lowest ordinal position in the collating sequence

Examples

Page 29: COBOL - Introduction

Figurative Constants

NOTES: The singular and plural forms of a figurative

constant can be used interchangeably.Do not use QUOTE/QUOTES to enclose a Non-numeric literal.ZERO, ZEROS, ZEROES are considered as Numeric Literals.

Page 30: COBOL - Introduction

Special Registers

Reserved words that name storage areas generated by the compiler

Page 31: COBOL - Introduction

Special Registers

RETURN-CODE Used to pass a return code to the calling

program or to the JCL when the current COBOL program ends

Examples

Page 32: COBOL - Introduction

Special Registers

WHEN-COMPILED Contains the date at the start of the program compilation

Examples

Page 33: COBOL - Introduction

Operators

Arithmetic Operators Relational Operators Logical Operators

Note: All COBOL operators must be preceded and followed by a space.

Page 34: COBOL - Introduction

Operators

Arithmetic Operators Binary Operators

Addition + Subtraction - Multiplication * Division / Exponentiation **

Page 35: COBOL - Introduction

Operators

Arithmetic Operators Unary Operators

Positive + (Equals to Multiplication by +1) Negative - (Equals to Multiplication by +1)

Page 36: COBOL - Introduction

Operators

Relational Operators Less Than < Greater Than > Equal To = Less Than or Equal To <= Greater Than or Equal To >=

Note: Each relational operators can be combined with the NOT logical operator

Page 37: COBOL - Introduction

Operators

Logical Operators Conjunction AND Inclusive OR Negation NOT

Page 38: COBOL - Introduction

Separator String

A word formed by 2 or more contiguous characters

1st character is a special character and the 2nd character is a space.

Page 39: COBOL - Introduction

Separator String

Separator comma ,bSeparator period .bSeparator Semicolon ;b

Note: b indicates a blank space.

Examples

Page 40: COBOL - Introduction

Separator String Rules

A COBOL statement may use Separator Period or

Separator Comma or Separator Semicolon.

Page 41: COBOL - Introduction

Separator String Rules

The following must end with a separator period: Sentence File (FD), sort/merge file (SD) and data description

entries in the data division Each paragraph in the Identification division

Page 42: COBOL - Introduction

Separator String Rules

The following must end with a separator period: SOURCE-COMPUTER, OBJECT-COMPUTER,

SPECIAL-NAMES and I-O-CONTROL paragraphs in the Environment division

Each file-control entry in the FILE-CONTROL paragraph

Page 43: COBOL - Introduction

Separator String Rules

The following must end with a separator period: All the 4 division headers All the section headers

Page 44: COBOL - Introduction

Phrase & Clause

Phrase – “meaningless” collection of COBOL words Clause – collection of words or phrases that itself has a meaning like

Statements

Page 45: COBOL - Introduction

Phrase & Clause

Phrase

AT END UNTIL A > B

Clause

OCCURS 5 TIMES LABEL RECORDS ARE STANDARDS

Examples

Examples

Page 46: COBOL - Introduction

Statement & Clause

Statements – group of words and/or phrases in PROCEDURE DIVISION

Clause – group of words and/or phrases in DATA, ENVIRONMENT &

IDENTIFICATION DIVISION

Page 47: COBOL - Introduction

Sentence & Entry

Sentence – collection of statements Entry – collection of clauses

Page 48: COBOL - Introduction

Sentence & Entry

Must be terminated by a Separator Period A COBOL Sentence can have only one

COBOL Statement

Page 49: COBOL - Introduction

Paragraphs

Subgroup of Sections

Page 50: COBOL - Introduction

Paragraphs

2 types: Predefined

IDENTIFICATION DIVISION & ENVIRONMENT DIVISION have only predefines paragraphs

User defined PROCEDURE DIVISION allows to define user defined paragraphs

Page 51: COBOL - Introduction

Paragraphs

Paragraph header has the para-name followed by a separator period.

At least one paragraph should be specified in PROCEDURE DIVISION, if it has any sentences.

Page 52: COBOL - Introduction

Sections

Subgroup of Divisions

Page 53: COBOL - Introduction

Sections

2 types: Predefined

ENVIRONMENT DIVISION & DATA DIVISION have only predefines sections User defined

PROCEDURE DIVISION allows to define user defined sections

Page 54: COBOL - Introduction

Sections

A section header has the: Section-name Followed by a space The word SECTION A separator period

Page 55: COBOL - Introduction

Sections

Optional, but must be specified in ENVIRONMENT DIVISION and DATA DIVISION, if they have appropriate entries

Page 56: COBOL - Introduction

COBOL Coding Sheet

Cols 1 – 6 : Sequence Number / Line Number Area, Margin L is Column 1 : Originally designed for entering line numbers on Punch Cards

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 57: COBOL - Introduction

COBOL Coding Sheet

Cols 1 – 6 : Used to label a source statement like revision number : Can consist of any characters : Nothing to do inside the program

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 58: COBOL - Introduction

COBOL Coding Sheet

Col 7 : Can have the ff: * - designates entire line as comment - - indicates continuation of nonnumeric literal / - forces page break when printing source listing : Also known as Margin C

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 59: COBOL - Introduction

COBOL Coding Sheet

Col 7 : A D or d indicates a debugging line : Should be left blank if this line is not a comment/ debugging line/ page break or a continuation.

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 60: COBOL - Introduction

COBOL Coding Sheet

Cols 8 – 72 : For COBOL program statements

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 61: COBOL - Introduction

COBOL Coding Sheet

Cols 8 – 11 : Area A / Margin A is column 8

: The following items must begin in Area A:- Division headers- Section headers

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 62: COBOL - Introduction

COBOL Coding Sheet

Cols 8 – 11 : The following items must begin in Area A:- Paragraph-names- Level indicators FD & SD- Level-numbers 01 and 77

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 63: COBOL - Introduction

COBOL Coding Sheet

Cols 8 – 11 : The following items must begin in Area A:

- DECLARATIVES and END DECLARATIVES

- End Program statement

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 64: COBOL - Introduction

COBOL Coding Sheet

Cols 12 – 72 : Area B / Margin B is column 12 and Margin R is

column 72

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 65: COBOL - Introduction

COBOL Coding Sheet

Cols 12 – 72 : The following items must begin in Area B:

- Entries & Sentences- Statements &

Clauses- Continuation lines

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 66: COBOL - Introduction

COBOL Coding Sheet

Note : Anything that begins anywhere in Area A

or B is considered to be in Area A or B respectively.

Level Numbers other than 01 & 77 can begin in Area A or Area B.

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 67: COBOL - Introduction

COBOL Coding Sheet

Cols 73 – 80 : Identification Field (used in punch cards, but no more!)

Note : 80 columns can COBOL accepts and only the first 72 columns are used by the COBOL compiler.

1 – 6 7 8 – 11 12 – 72Sequence No. Indicator No. Area A Area B

Page 68: COBOL - Introduction

Sample COBOL Program---1----+----2----+----3----+----4----+----5----+----6----+----7----+----8 IDENTIFICATION DIVISION. PROGRAM-ID. SequenceProgram. AUTHOR. Michael Coughlan.*This prgram lets the user to input 2 numbers to be multiplied and display*the product.

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 69: COBOL - Introduction

Reading the Syntax Diagrams Words in uppercase are reserved words.

When underlined they must be present when the operation of which they are a part is used. When they are not underlined the used for readability only and are optional. If used they must be spelt correctly.

Page 70: COBOL - Introduction

Reading the Syntax Diagrams Words in lower case represent names which will be devised by the programmer.

Page 71: COBOL - Introduction

Reading the Syntax Diagrams When enclosed in braces { }

A choice must be made from the options within the braces. When enclosed in square brackets [ ]

Indicates that the material is an option

Page 72: COBOL - Introduction

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