cobol BASICS2 (1)

download cobol BASICS2 (1)

of 20

Transcript of cobol BASICS2 (1)

  • 8/8/2019 cobol BASICS2 (1)

    1/20

    COBOL BasicsCOBOL Basics22

  • 8/8/2019 cobol BASICS2 (1)

    2/20

  • 8/8/2019 cobol BASICS2 (1)

    3/20

    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 FStudentDetails

    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.

  • 8/8/2019 cobol BASICS2 (1)

    4/20

    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 FStudentDetails

    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.

    StudentName StudentId CourseCode Grant Gender

    Group Items/RecordsGroup Items/Records

  • 8/8/2019 cobol BASICS2 (1)

    5/20

    LEVEL Numbers express DATA hierarchyLEVEL Numbers express DATA hierarchy

    In COBOL,In COBOL, level numberslevel numbers are used todecompose aare used todecompose astructure into its constituent parts.structure into its constituent parts.

    In this hierarchical structure the higherthe levelIn this hierarchical structure the higherthe levelnumber, the lowerthe item is in the hierarchy. At thenumber, the lowerthe item is in the hierarchy. At thelowest level the data is completely atomic.lowest level the data is completely atomic.

    The level numbersThe level numbers 0101 throughthrough 4949 are general levelare general levelnumbers but there are also special level numbersnumbers but there are also special level numberssuch as 66, 77 andsuch as 66, 77 and8888..

    In a hierarchical data description what is important isIn a hierarchical data description what is important isthethe relationshiprelationship ofthe level numbers toone another,ofthe level numbers toone another,

    not the actual level numbers used.not the actual level numbers used.

  • 8/8/2019 cobol BASICS2 (1)

    6/20

    LEVEL Numbers express DATA hierarchyLEVEL Numbers express DATA hierarchy

    In COBOL, level numbers are used todecompose aIn COBOL, level numbers are used todecompose astructure into its constituent parts.structure into its constituent parts.

    In this hierarchical structure the higherthe levelIn this hierarchical structure the higherthe levelnumber, the lowerthe item is in the hierarchy. At thenumber, the lowerthe item is in the hierarchy. At thelowest level the data is completely atomic.lowest level the data is completely atomic.

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

    In a hierarchical data description what is important isIn a hierarchical data description what is important isthe relationshipofthe level numbers toone another,the relationshipofthe level numbers toone 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.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.

    =

  • 8/8/2019 cobol BASICS2 (1)

    7/20

    Group and elementary items.Group and elementary items.

    In COBOL the term In COBOL the term group itemgroup item is used todescribe a is used todescribe adata item which has been furthersubdivided.data item which has been furthersubdivided.

    A Group item is declaredusing a level numberand 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 anduses the level number01.

    The term The term elementary itemelementary item is used todescribe data is used todescribe dataitems which are atomic; that is, not furthersubdivided.items which are atomic; that is, not furthersubdivided.

    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 grouporelementary item declarationEvery grouporelementary item declaration mustmust bebefollowed by a full stop.followed by a full stop.

  • 8/8/2019 cobol BASICS2 (1)

    8/20

    PICTUREs forGroup ItemsPICTUREs forGroup Items

    Picture clauses arePicture clauses are NOTNOT specifiedfor group dataspecifiedfor group data

    items because theitems because the sizesize a group item is the sum ofa group item is the sum ofthe sizes of its subordinate, elementary items and itsthe sizes of its subordinate, elementary items and itstypetype is always assumed to beis always assumed to be PIC XPIC X..

    The type ofa group items is always assumed to beThe type ofa group items is always assumed to be

    PIC X because group items may have severalPIC X because group items may have severaldifferent data items and types subordinate to them.different data items and types subordinate to them.

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

  • 8/8/2019 cobol BASICS2 (1)

    9/20

    Assignment in COBOLAssignment in COBOL

    In strongly typed languages like ModulaIn strongly typed languages like Modula--2,2,Pascal orADA the assignment operation is simplePascal orADA the assignment operation is simplebecause assignment is only allowed between databecause assignment is only allowed between dataitems with compatible types.items with compatible types.

    The simplicity ofassignment in these languages isThe simplicity ofassignment in these languages isachieved at the cost ofhaving a large numberofachieved at the cost ofhaving a large numberof

    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 achievedonly at the cost ofBut this simplicity is achievedonly at the cost ofhaving a very complex assignment statement.having a very complex assignment statement.

    In COBOL assignment is achievedusing theIn COBOL assignment is achievedusing the MOVEMOVE

    verb.verb.

  • 8/8/2019 cobol BASICS2 (1)

    10/20

    The MOVE VerbThe MOVE Verb

    TheThe MOVEMOVE copies data from the source identifierorcopies data from the source identifierorliteral toone ormore destination identifiers.literal toone ormore destination identifiers.

    The source anddestination identifiers can be groupThe source anddestination identifiers can be grouporelementary data items.orelementary data items.

    When the destination item is alphanumeric orWhen the destination item is alphanumeric oralphabetic (PIC X orA) data is copied into thealphabetic (PIC X orA) data is copied into thedestination area fromdestination area from leftleft toto rightright with space filling orwith space filling or

    truncation on the right.truncation on the right.

    When data is MOVEd into an item the contents oftheWhen data is MOVEd into an item the contents oftheitem are completelyitem are completely replacedreplaced. Ifthe source data is. Ifthe source data istoo small tofill the destination item entirely thetoo small tofill the destination item entirely theremaining area isremaining area is zerozeroororspace filledspace filled..

    _ a MOVE TO ...Identifier

    Literal

    Identifier

  • 8/8/2019 cobol BASICS2 (1)

    11/20

    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

  • 8/8/2019 cobol BASICS2 (1)

    12/20

    R Y A NR Y A N

    MOVE RYAN TO Surname.MOVE RYAN TO Surname.MOVE FITZPATRICK TO Surname.

    01 Surname PIC X(8).

    MOVEing DataMOVEing Data

  • 8/8/2019 cobol BASICS2 (1)

    13/20

    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 RF I T Z P A T RI C KI C K

  • 8/8/2019 cobol BASICS2 (1)

    14/20

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

    When the destination item is numeric, oreditedWhen the destination item is numeric, oreditednumeric, then data is aligned along thenumeric, then data is aligned along the decimaldecimalpointpoint with zerofilling ortruncation as necessary.with zerofilling ortruncation as necessary.

    When the decimal point is not explicitly specified inWhen the decimal point is not explicitly specified in

    eitherthe source ordestination items, the item iseitherthe source ordestination items, the item istreated as if it had an assumeddecimal pointtreated as if it had an assumeddecimal pointimmediately after its rightmost character.immediately after its rightmost character.

  • 8/8/2019 cobol BASICS2 (1)

    15/20

    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 00 0 0 0 0 0

    0 00 0 1 2 41 2 4 00

    00 1 2 3 4 51 2 3 4 5 66

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

    GrossPay

    GrossPay

    GrossPay

    GrossPay

  • 8/8/2019 cobol BASICS2 (1)

    16/20

    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.

    11 2 3 42 3 4

    00 1 21 2 44

    1 5 41 5 4 0 00 0

    33 5 5 2 7 55 5 2 7 5

    Price

    CountyPop

    CountyPop

    Price

  • 8/8/2019 cobol BASICS2 (1)

    17/20

    Legal MOVEsLegal MOVEs

    Certain combinations ofsending andreceiving dataCertain combinations ofsending andreceiving data

    types are not permitted (even by COBOL).types are not permitted (even by COBOL).

  • 8/8/2019 cobol BASICS2 (1)

    18/20

    The DISPLAY VerbThe DISPLAY Verb

    From time to time it may be useful todisplayFrom time to time it may be useful todisplay

    messages anddata values on the screen.messages anddata values on the screen. A simpleA simple DISPLAYDISPLAY statement can be used to achievestatement can be used to achieve

    this.this.

    A single DISPLAY can be used todisplay severalA single DISPLAY can be used todisplay several

    data items orliterals orany combination ofthese.data items orliterals orany combination ofthese. TheThe WITH NO ADVANCINGWITH NO ADVANCING clause suppresses theclause suppresses the

    carriage return/line feed.carriage return/line feed.

    ? A ? A

    DISPLAYIdentifier

    Literal

    Identifier

    Literal

    ...

    UPON WITH NO ADVANCING

    -

    Mnemonic - Name

  • 8/8/2019 cobol BASICS2 (1)

    19/20

  • 8/8/2019 cobol BASICS2 (1)

    20/20

    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).

    Enter student details using template below

    NNNNNNNNNNSSSSSSSCCCCGGGGSCOUGHLANMS9476532LM511245M

    Name is MS COUGHLAN

    Date is 24 01 94

    Today is day 024 of the year

    The time is 22:23

    Run ofAccept and Display programRun ofAccept and Display program