cobol basics 1. cobol coding rules almost all cobol compilers treat a line of cobol code as if it...

20
COBOL Basics COBOL Basics 1 1

Upload: calvin-robinson

Post on 27-Dec-2015

216 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

COBOL Basics 1 COBOL Basics 1

Page 2: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

COBOL coding COBOL coding rulesrules

Almost all COBOL compilers treat a line of COBOL code Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;as if it contained two distinct areas. These are known as;

Area AArea A and and Area BArea B When a COBOL compiler recognizes these two areas, all When a COBOL compiler recognizes these two areas, all

division, section, paragraph names, FD entries and 01 division, section, paragraph names, FD entries and 01 level numbers must start in level numbers must start in Area AArea A. All other sentences . All other sentences must start in must start in Area BArea B..

Area A is four characters wide and is followed by Area B.Area A is four characters wide and is followed by Area B.

In Microfocus COBOL the compiler directiveIn Microfocus COBOL the compiler directive$ SET SOURCEFORMAT"FREE" $ SET SOURCEFORMAT"FREE" frees us from all formatting frees us from all formatting

restrictions.restrictions.

Page 3: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

COBOL coding COBOL coding rulesrules

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. ProgramFragment.* This is a comment. It starts* with an asterisk in column 1

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. ProgramFragment.* This is a comment. It starts* with an asterisk in column 1

Almost all COBOL compilers treat a line of COBOL code Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;as if it contained two distinct areas. These are known as;

Area A Area A and and Area BArea B When a COBOL compiler recognizes these two areas, all When a COBOL compiler recognizes these two areas, all

division, section, paragraph names, FD entries and 01 division, section, paragraph names, FD entries and 01 level numbers must start in level numbers must start in Area AArea A. All other sentences . All other sentences must start in must start in Area BArea B..

Area A is four characters wide and is followed by Area B.Area A is four characters wide and is followed by Area B.

In Microfocus COBOL the compiler directiveIn Microfocus COBOL the compiler directive$ SET SOURCEFORMAT"FREE" $ SET SOURCEFORMAT"FREE" frees us from all formatting frees us from all formatting

restrictions.restrictions.

Page 4: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

Name Name Construction.Construction.

All user defined names, such as data names, paragraph All user defined names, such as data names, paragraph names, section names and mnemonic names, must names, section names and mnemonic names, must adhere to the following rules;adhere to the following rules;

They must contain at least one character and not more than 30 characters.

They must contain at least one alphabetic character and they must not begin or end with a hyphen.

They must be contructed from the characters A to Z, the number 0 to 9 and the hyphen.

e.g. TotalPay, Gross-Pay, PrintReportHeadings, Customer10-Rec

All data-names should describe the data they contain.All data-names should describe the data they contain. All paragraph and section names should describe the All paragraph and section names should describe the

function of the paragraph or section.function of the paragraph or section.

Page 5: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

Describing Describing DATA.DATA.

ŒŒVariables.Variables.

Literals.Literals.

ŽŽFigurative ConstantsFigurative Constants..

Unlike other programming languages, COBOL does not Unlike other programming languages, COBOL does not support user defined constants.support user defined constants.

There are basically three kinds of data used in COBOL programs;

Page 6: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

Data-Names / Data-Names / VariablesVariables

A A variablevariable is a named location in memory into which a is a named location in memory into which a program can put data and from which it can retrieve program can put data and from which it can retrieve data.data.

A A data-namedata-name or or identifieridentifier is the name used to identify is the name used to identify the area of memory reserved for the variable.the area of memory reserved for the variable.

Variables must be described in terms of their type and Variables must be described in terms of their type and size.size.

Every variable used in a COBOL program must have a Every variable used in a COBOL program must have a description in the DATA DIVISION. description in the DATA DIVISION.

Page 7: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

StudentName

Using Using VariablesVariables

MOVE "JOHN" TO StudentName.DISPLAY "My name is ", StudentName.

01 StudentName01 StudentName PIC X(6) VALUE SPACESPIC X(6) VALUE SPACES..

Page 8: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

StudentName

MOVE "JOHN" TO StudentName.MOVE "JOHN" TO StudentName.DISPLAY "My name is ", StudentName.

01 StudentName PIC X(6) VALUE SPACES.

J O H J O H NN

Using Using VariablesVariables

Page 9: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

StudentName

MOVE "JOHN" TO StudentName.DISPLAY "My name is ", StudentName.DISPLAY "My name is ", StudentName.

01 StudentName PIC X(6) VALUE SPACES.

My name is JOHN

Using Using VariablesVariables

J O H NJ O H N

Page 10: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

COBOL Data COBOL Data TypesTypes

COBOL is not a “typed” language and the distinction COBOL is not a “typed” language and the distinction between some of the data types available in the between some of the data types available in the language is a little blurred.language is a little blurred.

For the time being we will focus on just two data types,For the time being we will focus on just two data types, numeric text or string

Data type is important because it determines the Data type is important because it determines the operations which are valid on the type.operations which are valid on the type.

COBOL is not as rigorous in the application of typing COBOL is not as rigorous in the application of typing rules as other languages.rules as other languages.

For example, some COBOL “numeric” data items may, from time to time, have values which are not “numeric”!

Page 11: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

Quick Review of “Data Quick Review of “Data Typing”Typing”

In “typed” languages simply specifying the type of a In “typed” languages simply specifying the type of a data item provides quite a lot of information about it.data item provides quite a lot of information about it.

The type usually determines the range of values the The type usually determines the range of values the data item can store.data item can store.

For instance a CARDINAL item can store values between 0..65,535 and an INTEGER between -32,768..32,767

From the type of the item the compiler can establish From the type of the item the compiler can establish how much memory to set aside for storing its values.how much memory to set aside for storing its values.

If the type is “REAL” the number of decimal places is If the type is “REAL” the number of decimal places is allowed to vary dynamically with each calculation but allowed to vary dynamically with each calculation but the amount of the memory used to store a real number the amount of the memory used to store a real number is fixed.is fixed.

Page 12: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

COBOL data COBOL data description.description.

Because Because COBOL is not typedCOBOL is not typed it employs a different it employs a different mechanism for describing the characteristics of the mechanism for describing the characteristics of the data items in the program.data items in the program.

COBOL uses what could be described as a “COBOL uses what could be described as a “declaration declaration by exampleby example” strategy.” strategy.

In effect, the programmer provides the system with an In effect, the programmer provides the system with an example, or template, or example, or template, or PICPICTURE of what the data item TURE of what the data item looks like.looks like.

From the “picture” the system derives the information From the “picture” the system derives the information necessary to allocate it.necessary to allocate it.

Page 13: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

COBOL ‘PICTURE’ Clause COBOL ‘PICTURE’ Clause symbolssymbols

To create the required ‘picture’ the programmer uses a To create the required ‘picture’ the programmer uses a set of symbols.set of symbols.

The following symbols are used frequently in picture The following symbols are used frequently in picture clauses;clauses;

9 (the digit nine) is used to indicate the occurrence of a digit at the corresponding position in the picture.

X (the character X) is used to indicate the occurrence of any character from the character set at the corresponding position in the picture

V (the character V) is used to indicate position of the decimal point in a numeric value! It is often referred to as the “assumed decimal point” character.

S (the character S) indicates the presence of a sign and can only appear at the beginning of a picture.

Page 14: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

COBOL ‘PICTURE’ COBOL ‘PICTURE’ ClausesClauses

Some examplesSome examples PICTURE 999 a three digit (+ive only) integer PICTURE S999 a three digit (+ive/-ive) integer PICTURE XXXX a four character text item or string PICTURE 99V99 a +ive ‘real’ in the range 0 to 99.99 PICTURE S9V9 a +ive/-ive ‘real’ in the range ?

If you wish you can use the abbreviation If you wish you can use the abbreviation PICPIC..

Numeric values can have a maximum of 18 (eighteen) Numeric values can have a maximum of 18 (eighteen) digits (i.e. 9’s).digits (i.e. 9’s).

The limit on string values is usually system-dependent.The limit on string values is usually system-dependent.

Page 15: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

Abbreviating recurring Abbreviating recurring symbolssymbols

Recurring symbols can be specified using a ‘repeat’ Recurring symbols can be specified using a ‘repeat’ factor inside round bracketsfactor inside round brackets

PIC 9(6) is equivalent to PICTURE 999999 PIC 9(6)V99 is equivalent to PIC 999999V99 PICTURE X(10) is equivalent to PIC XXXXXXXXXX PIC S9(4)V9(4) is equivalent to PIC S9999V9999 PIC 9(18) is equivalent to PIC 999999999999999999

Page 16: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

Declaring DATA in Declaring DATA in COBOLCOBOL

In COBOL a variable declaration consists of a line In COBOL a variable declaration consists of a line containing the following items;containing the following items;

ŒŒA level number. A level number.

A data-name or identifier.A data-name or identifier.

ŽŽA PICTURE clause.A PICTURE clause.

We can give a starting value to variables by means of an We can give a starting value to variables by means of an extension to the picture clause called the extension to the picture clause called the value clausevalue clause..

DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 999 VALUE ZEROS.01 VatRate PIC V99 VALUE .18.01 StudentName PIC X(10) VALUE SPACES.

Num1 VatRate StudentNameNum1 VatRate StudentName

000000 .18.18

DDATAATA

Page 17: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

COBOL COBOL Literals.Literals.

String/Alphanumeric literalsString/Alphanumeric literals are enclosed in quotes and are enclosed in quotes and may consists of alphanumeric charactersmay consists of alphanumeric characters

e.g. "Michael Ryan", "-123", "123.45"

Numeric literalsNumeric literals may consist of numerals, the decimal may consist of numerals, the decimal point and the plus or minus sign. Numeric literals are point and the plus or minus sign. Numeric literals are not enclosed in quotes.not enclosed in quotes.

e.g. 123, 123.45, -256, +2987

Page 18: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

Figurative Figurative ConstantsConstants

COBOL provides its own, special constants called COBOL provides its own, special constants called Figurative Constants.Figurative Constants.

SPACE or SPACESSPACE or SPACES =

ZERO or ZEROS or ZEROSZERO or ZEROS or ZEROS = 0

QUOTE or QUOTESQUOTE or QUOTES = "

HIGH-VALUE or HIGH-VALUESHIGH-VALUE or HIGH-VALUES = Max Value

LOW-VALUE or LOW-VALUESLOW-VALUE or LOW-VALUES = Min Value

ALL ALL literalliteral = Fill With Literal

SPACE or SPACESSPACE or SPACES =

ZERO or ZEROS or ZEROSZERO or ZEROS or ZEROS = 0

QUOTE or QUOTESQUOTE or QUOTES = "

HIGH-VALUE or HIGH-VALUESHIGH-VALUE or HIGH-VALUES = Max Value

LOW-VALUE or LOW-VALUESLOW-VALUE or LOW-VALUES = Min Value

ALL ALL literalliteral = Fill With Literal

Page 19: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

Figurative Constants - Figurative Constants - ExamplesExamples

01 GrossPay PIC 9(5)V99 VALUE 13.5.

MOVE TO GrossPay.

01 GrossPay PIC 9(5)V99 VALUE 13.5.

MOVE TO GrossPay.ZEROZEROSZEROES

01 StudentName PIC X(10) VALUE "MIKE".

MOVE ALL "-" TO StudentName.

01 StudentName PIC X(10) VALUE "MIKE".

MOVE ALL "-" TO StudentName.

StudentName

M I K E M I K E

GrossPay

0 0 0 1 3 5 0 0 0 1 3 5 00

Page 20: COBOL Basics 1. COBOL coding rules  Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are known as;

Figurative Constants - Figurative Constants - ExamplesExamples

01 GrossPay PIC 9(5)V99 VALUE 13.5.

MOVE TO GrossPay.

01 GrossPay PIC 9(5)V99 VALUE 13.5.

MOVE TO GrossPay.ZEROZEROSZEROES

01 StudentName PIC X(10) VALUE "MIKE".

MOVE ALL "-" TO StudentName.

01 StudentName PIC X(10) VALUE "MIKE".

MOVE ALL "-" TO StudentName.

StudentName

- - - - - - - - - -- - - - - - - - - -

GrossPay

0 0 0 0 0 0 0 0 0 0 0 0 00