20
COBOL COBOL Basics Basics 2 2

COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

Embed Size (px)

Citation preview

Page 1: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

COBOL BasicsCOBOL Basics2 2

Page 2: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F

Group Items/RecordsGroup Items/Records

StudentDetails

WORKING-STORAGE SECTION.01 StudentDetails PIC X(26).

WORKING-STORAGE SECTION.01 StudentDetails PIC X(26).

Page 3: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F

StudentDetails

StudentName StudentId CourseCode Grant Gender

Group Items/RecordsGroup Items/Records

WORKING-STORAGE SECTION.01 StudentDetails.

0202 StudentNameStudentName PIC X(10).PIC X(10).0202 StudentIdStudentId PIC 9(7).PIC 9(7).0202 CourseCodeCourseCode PIC X(4).PIC X(4).0202 GrantGrant PIC 9(4).PIC 9(4).0202 GenderGender PIC X.PIC X.

WORKING-STORAGE SECTION.01 StudentDetails.

0202 StudentNameStudentName PIC X(10).PIC X(10).0202 StudentIdStudentId PIC 9(7).PIC 9(7).0202 CourseCodeCourseCode PIC X(4).PIC X(4).0202 GrantGrant PIC 9(4).PIC 9(4).0202 GenderGender PIC X.PIC X.

Page 4: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F

StudentDetails

Surname Initials

WORKING-STORAGE SECTION.01 StudentDetails.

0202 StudentName.StudentName.03 Surname03 Surname PIC X(8).PIC X(8).03 Initials03 Initials PIC XX.PIC XX.

0202 StudentIdStudentId PIC 9(7).PIC 9(7).0202 CourseCodeCourseCode PIC X(4).PIC X(4).0202 GrantGrant PIC 9(4).PIC 9(4).0202 GenderGender PIC X.PIC X.

WORKING-STORAGE SECTION.01 StudentDetails.

0202 StudentName.StudentName.03 Surname03 Surname PIC X(8).PIC X(8).03 Initials03 Initials PIC XX.PIC XX.

0202 StudentIdStudentId PIC 9(7).PIC 9(7).0202 CourseCodeCourseCode PIC X(4).PIC X(4).0202 GrantGrant PIC 9(4).PIC 9(4).0202 GenderGender PIC X.PIC X.

StudentName StudentId CourseCode Grant Gender

Group Items/RecordsGroup Items/Records

Page 5: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

LEVEL Numbers express DATA hierarchyLEVEL Numbers express DATA hierarchy In COBOL, In COBOL, level numberslevel numbers are used to decompose a are used to decompose a

structure into it’s constituent parts. structure into it’s constituent parts.

In this hierarchical structure the higher the level In this hierarchical structure the higher the level number, the lower the item is in the hierarchy. At the number, the lower the item is in the hierarchy. At the lowest level the data is completely atomic.lowest level the data is completely atomic.

The level numbers The level numbers 0101 through through 49 49 are general level are general level numbers but there are also special level numbers numbers but there are also special level numbers such as 66, 77 and such as 66, 77 and 8888..

In a hierarchical data description what is important is In a hierarchical data description what is important is the the relationshiprelationship of the level numbers to one another, of the level numbers to one another, not the actual level numbers used.not the actual level numbers used.

Page 6: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

LEVEL Numbers express DATA hierarchyLEVEL Numbers express DATA hierarchy In COBOL, level numbers are used to decompose a In COBOL, level numbers are used to decompose a

structure into it’s constituent parts. structure into it’s constituent parts.

In this hierarchical structure the higher the level In this hierarchical structure the higher the level number, the lower the item is in the hierarchy. At the number, the lower the item is in the hierarchy. At the lowest level the data is completely atomic.lowest level the data is completely atomic.

The level numbers 01 through 49 are general level The level numbers 01 through 49 are general level numbers but there are also special level numbers numbers but there are also special level numbers such as 66, 77 and 88.such as 66, 77 and 88.

In a hierarchical data description what is important is In a hierarchical data description what is important is the relationship of the level numbers to one another, the relationship of the level numbers to one another, not the actual level numbers used.not the actual level numbers used.

01 StudentDetails.02 StudentName.

03 Surname PIC X(8).03 Initials PIC XX.

02 StudentId PIC 9(7).02 CourseCode PIC X(4).02 Grant PIC 9(4).02 Gender PIC X.

01 StudentDetails.02 StudentName.

03 Surname PIC X(8).03 Initials PIC XX.

02 StudentId PIC 9(7).02 CourseCode PIC X(4).02 Grant PIC 9(4).02 Gender PIC X.

01 StudentDetails.05 StudentName.

10 Surname PIC X(8).10 Initials PIC XX.

05 StudentId PIC 9(7).05 CourseCode PIC X(4).05 Grant PIC 9(4).05 Gender PIC X.

01 StudentDetails.05 StudentName.

10 Surname PIC X(8).10 Initials PIC XX.

05 StudentId PIC 9(7).05 CourseCode PIC X(4).05 Grant PIC 9(4).05 Gender PIC X.

=

Page 7: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

Group and elementary items.Group and elementary items.

In COBOL the term “In COBOL the term “group itemgroup item” is used to describe a data ” is used to describe a data item which has been further subdivided.item which has been further subdivided.

A Group item is declared using a level number and a data name. It cannot have a picture clause.

Where a group item is the highest item in a data hierarchy it is referred to as a record and uses the level number 01.

The term “The term “elementary itemelementary item” is used to describe data items ” is used to describe data items which are atomic; that is, not further subdivided.which are atomic; that is, not further subdivided.

An elementary item declaration consists of; An elementary item declaration consists of; a level number, a data name picture clause.

An elementary item must have a picture clause. Every group or elementary item declaration Every group or elementary item declaration mustmust be followed be followed

by a full stop.by a full stop.

Page 8: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

PICTUREs for Group ItemsPICTUREs for Group Items

Picture clauses are Picture clauses are NOTNOT specified for ‘group’ data specified for ‘group’ data items because the items because the sizesize a group item is the sum of a group item is the sum of the sizes of its subordinate, elementary items and its the sizes of its subordinate, elementary items and its typetype is always assumed to be is always assumed to be PIC XPIC X..

The type of a group items is always assumed to be The type of a group items is always assumed to be PIC X because group items may have several PIC X because group items may have several different data items and types subordinate to them.different data items and types subordinate to them.

An X picture is the only one which could support An X picture is the only one which could support such collections.such collections.

Page 9: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

Assignment in COBOLAssignment in COBOL

In “strongly typed” languages like Modula-2, Pascal In “strongly typed” languages like Modula-2, Pascal or ADA the assignment operation is simple because or ADA the assignment operation is simple because assignment is only allowed between data items with assignment is only allowed between data items with compatible types.compatible types.

The simplicity of assignment in these languages is The simplicity of assignment in these languages is achieved at the “cost” of having a large number of achieved at the “cost” of having a large number of data types.data types.

In COBOL there are basically only three data types,In COBOL there are basically only three data types, Alphabetic (PIC A) Alphanumeric (PIC X) Numeric (PIC 9)

But this simplicity is achieved only at the cost of But this simplicity is achieved only at the cost of having a very complex assignment statement.having a very complex assignment statement.

In COBOL assignment is achieved using the In COBOL assignment is achieved using the MOVE MOVE verb. verb.

Page 10: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

The MOVE VerbThe MOVE Verb

TheThe MOVE MOVE copies data from the source identifier or copies data from the source identifier or literal to one or more destination identifiers.literal to one or more destination identifiers.

The source and destination identifiers can be group The source and destination identifiers can be group or elementary data items.or elementary data items.

When the destination item is alphanumeric or When the destination item is alphanumeric or alphabetic (PIC X or A) data is copied into the alphabetic (PIC X or A) data is copied into the destination area fromdestination area from leftleft toto right right with space filling or with space filling or truncation on the right.truncation on the right.

When data is MOVEd into an item the contents of the When data is MOVEd into an item the contents of the item are completely item are completely replacedreplaced. If the source data is too . If the source data is too small to fill the destination item entirely the remaining small to fill the destination item entirely the remaining area is area is zero zero oror space filledspace filled..

MOVE TO ...Identifier

LiteralIdentifier

Page 11: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

MOVE “RYAN” TO Surname.MOVE “FITZPATRICK” TO Surname.MOVE “RYAN” TO Surname.MOVE “FITZPATRICK” TO Surname.

01 Surname PIC X(8).

MOVEing DataMOVEing Data

C O U G H L A NC O U G H L A N

Page 12: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

R Y A N R Y A N

MOVE “RYAN” TO Surname.MOVE “RYAN” TO Surname.MOVE “FITZPATRICK” TO Surname.MOVE “RYAN” TO Surname.MOVE “RYAN” TO Surname.MOVE “FITZPATRICK” TO Surname.

01 Surname PIC X(8).

MOVEing DataMOVEing Data

Page 13: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

MOVE “RYAN” TO Surname.MOVE “FITZPATRICK” TO Surname.MOVE “FITZPATRICK” TO Surname.MOVE “RYAN” TO Surname.MOVE “FITZPATRICK” TO Surname.MOVE “FITZPATRICK” TO Surname.

01 Surname PIC X(8).

MOVEing DataMOVEing Data

F I T Z P A T R F I T Z P A T R I C KI C K

Page 14: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

MOVEing to a numeric item.MOVEing to a numeric item.

When the destination item is numeric, or edited When the destination item is numeric, or edited numeric, then data is aligned along the numeric, then data is aligned along the decimal decimal pointpoint with zero filling or truncation as necessary. with zero filling or truncation as necessary.

When the decimal point is not explicitly specified in When the decimal point is not explicitly specified in either the source or destination items, the item is either the source or destination items, the item is treated as if it had an assumed decimal point treated as if it had an assumed decimal point immediately after its rightmost character.immediately after its rightmost character.

Page 15: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

MOVE ZEROS TO GrossPay.

MOVE 12.4 TO GrossPay.

MOVE 123.456 TO GrossPay.

MOVE 12345.757 TO GrossPay.

01 GrossPay PIC 9(4)V99.

0 0 0 0 0 0 0 0 0 0 0 0

0 00 0 1 2 41 2 4 00

0 0 1 2 3 4 51 2 3 4 5 66

11 2 3 4 5 7 5 2 3 4 5 7 5 77

GrossPay

GrossPay

GrossPay

GrossPay

Page 16: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

MOVE 1234 TO CountyPop.

MOVE 12.4 TO CountyPop.

MOVE 154 TO Price.

MOVE 3552.75 TO Price.

01 CountyPop PIC 999.01 Price PIC 999V99.

1 1 2 3 42 3 4

00 1 21 2 44

1 5 41 5 4 0 00 0

33 5 5 2 7 5 5 5 2 7 5

Price

CountyPop

CountyPop

Price

Page 17: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

Legal MOVEsLegal MOVEs

Certain combinations of sending and receiving data Certain combinations of sending and receiving data types are not permitted (even by COBOL).types are not permitted (even by COBOL).

Page 18: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

The DISPLAY VerbThe DISPLAY Verb

From time to time it may be useful to display From time to time it may be useful to display messages and data values on the screen.messages and data values on the screen.

A simple A simple DISPLAYDISPLAY statement can be used to achieve statement can be used to achieve this.this.

A single DISPLAY can be used to display several A single DISPLAY can be used to display several data items or literals or any combination of these.data items or literals or any combination of these.

The The WITH NO ADVANCINGWITH NO ADVANCING clause suppresses the clause suppresses the carriage return/line feed.carriage return/line feed.

DISPLAY Identifier

Literal

Identifier

Literal ...

UPON WITH NO ADVANCING

Mnemonic - Name

Page 19: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

The ACCEPT verbThe ACCEPT verb

Format 1. ACCEPT Identifier FROM Mnemonic - name

Format 2. ACCEPT Identifier FROM

DATE

DAY

DAY - OF - WEEK

TIME

01 CurrentDate01 CurrentDate PIC 9(6).PIC 9(6).* YYMMDD

01 DayOfYear01 DayOfYear PIC 9(5).PIC 9(5).* YYDDD

01 Day0fWeek01 Day0fWeek PIC 9.PIC 9.* D (1=Monday)

01 CurrentTime 01 CurrentTime PIC 9(8).PIC 9(8).* HHMMSSss s = S/100

01 CurrentDate01 CurrentDate PIC 9(6).PIC 9(6).* YYMMDD

01 DayOfYear01 DayOfYear PIC 9(5).PIC 9(5).* YYDDD

01 Day0fWeek01 Day0fWeek PIC 9.PIC 9.* D (1=Monday)

01 CurrentTime 01 CurrentTime PIC 9(8).PIC 9(8).* HHMMSSss s = S/100

Page 20: COBOL Basics 2. H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F Group Items/Records StudentDetails WORKING-STORAGE SECTION. 01StudentDetailsPIC X(26)

PROCEDURE DIVISION.Begin. DISPLAY "Enter student details using template below". DISPLAY "NNNNNNNNNNSSSSSSSCCCCGGGGS ". ACCEPT StudentDetails. ACCEPT CurrentDate FROM DATE. ACCEPT DayOfYear FROM DAY. ACCEPT CurrentTime FROM TIME. DISPLAY "Name is ", Initials SPACE Surname. DISPLAY "Date is " CurrentDay SPACE CurrentMonth SPACE CurrentYear. DISPLAY "Today is day " YearDay " of the year". DISPLAY "The time is " CurrentHour ":" CurrentMinute. STOP RUN.

PROCEDURE DIVISION.Begin. DISPLAY "Enter student details using template below". DISPLAY "NNNNNNNNNNSSSSSSSCCCCGGGGS ". ACCEPT StudentDetails. ACCEPT CurrentDate FROM DATE. ACCEPT DayOfYear FROM DAY. ACCEPT CurrentTime FROM TIME. DISPLAY "Name is ", Initials SPACE Surname. DISPLAY "Date is " CurrentDay SPACE CurrentMonth SPACE CurrentYear. DISPLAY "Today is day " YearDay " of the year". DISPLAY "The time is " CurrentHour ":" CurrentMinute. STOP RUN.

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. AcceptAndDisplay.AUTHOR. Michael Coughlan.

DATA DIVISION.WORKING-STORAGE SECTION.01 StudentDetails. 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 StudentId PIC 9(7). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Gender PIC X.

01 CurrentDate. 02 CurrentYear PIC 99. 02 CurrentMonth PIC 99. 02 CurrentDay PIC 99.

01 DayOfYear. 02 FILLER PIC 99. 02 YearDay PIC 9(3).

01 CurrentTime. 02 CurrentHour PIC 99. 02 CurrentMinute PIC 99. 02 FILLER PIC 9(4).

$ SET SOURCEFORMAT"FREE"IDENTIFICATION DIVISION.PROGRAM-ID. AcceptAndDisplay.AUTHOR. Michael Coughlan.

DATA DIVISION.WORKING-STORAGE SECTION.01 StudentDetails. 02 StudentName. 03 Surname PIC X(8). 03 Initials PIC XX. 02 StudentId PIC 9(7). 02 CourseCode PIC X(4). 02 Grant PIC 9(4). 02 Gender PIC X.

01 CurrentDate. 02 CurrentYear PIC 99. 02 CurrentMonth PIC 99. 02 CurrentDay PIC 99.

01 DayOfYear. 02 FILLER PIC 99. 02 YearDay PIC 9(3).

01 CurrentTime. 02 CurrentHour PIC 99. 02 CurrentMinute PIC 99. 02 FILLER PIC 9(4).

Enter student details using template belowNNNNNNNNNNSSSSSSSCCCCGGGGSCOUGHLANMS9476532LM511245MName is MS COUGHLANDate is 24 01 94Today is day 024 of the yearThe time is 22:23

Enter student details using template belowNNNNNNNNNNSSSSSSSCCCCGGGGSCOUGHLANMS9476532LM511245MName is MS COUGHLANDate is 24 01 94Today is day 024 of the yearThe time is 22:23

Run of Accept and Display programRun of Accept and Display program