1 0 Cobol Funda

Embed Size (px)

Citation preview

  • 8/2/2019 1 0 Cobol Funda

    1/24

    Structure of COBOL Program:

    IDENTIFICATION DIVISION.PROGRAM-ID.

    AUTHOR.

    DATE-WRITTEN.

    DATE-COMPILED.

    ENVIRONMENT DIVISION.CONFIGURATION SECTION.

    SOURCE-COMPUTER.

    OBJECT-COMPUTER.

    INPUT-OUTPUT SECTION.

    FILE-CONTROL. -> the SELECT ASSIGN clause is used to define the

    files and relate them to the physical files in the JCL

    SELECT IN-FILE ASSIGN TO DD1

    I-O-CONTROL.

    DATA DIVISION.

    SIX SECTIONS

    1.FILE SECTION

    2.WORKING-STORAGE SECTION

    3. LOCAL-STORAGE SECTION

    4.SCREEN SECTION

    5.REPORT SECTION

    6. LINKAGE SECTION

    FILE SECTION.

    The FD entries will hold the record definition for the files in

    FILE-CONTROL section.

    FD IN-FILE.

    LABEL RECORD STANDARD.

    BLOCK CONTAINS 0 CHARACTERS.

    RECORD CONTAINS 80 CHARACTERS.

    01 IN-REC.

    05 FLD1 PIC X(05).

    05 FLD2 PIC X(75).

    FD File Descriptor level indicator

    SD Sort-Merge Descriptor level indicator

    WORKING-STORAGE SECTION.Local variables and records are defined here

    LOCAL-STORAGE SECTION. very rarely used.

    LINKAGE SECTION.

    Any variables that is passed to this program will be defined

    here. The linkage group should be used with the USING clause of

    PROCEDURE DIVISION.

  • 8/2/2019 1 0 Cobol Funda

    2/24

    PROCEDURE DIVISION.This is where the executable code goes in paragraphs and

    statements. For subprograms this division uses the USING clause.

    Scope Terminator:

    The implicit scope terminator is a period.

    The explicit scope terminator is END-IF, END-READ, END-EVALUATE,

    END-PERFORM etc.

    Levels in WSS:

    01 User defined levels. Can hav 02 thru 49 as sub items66 Level number for RENAMES clause. Cannot hav REDEFINES77 Elementary items. Cannot occur in a FD grp. Cannot hav REDEFINES.88 Conditon-Names / Switches / Flags. SET statement is used toinitialize or set a particular value

    Figurative Constants:ZERO, ZEROS, ZEROES, SPACE, SPACES, HIGH-VALUE, LOW-VALUE, QUOTE,

    NULL, and ALL.

    X00 null character for termination in the string.

    Numeric Data:9 to represent the numeric char

    P represents leading / trailing zeroes

    S represents +ve / -ve

    V or Period (.) decimal point (V doesnt require a storage

    space)

    Z suppress the leading / trailing zeros based on the Z in PIC

    clause.

    DISPLAY normal value

    COMP-3 packed decimal (storage reqd in bytes are no.of decimals

    / 2 for odd number of char and (no.of decimal /2 ) +1 for even)

    COMP and COMP-4 BINARY

    COMP-1 and COMP-2 Internal Floating Point

    PIC 9.99 is a FOUR-POSITION field that actually contains a

    decimal point where as PIC 9v99 is THREE-POSITION numeric field with

    implied or assumed decimal position.

    For Comp-3 var, the storage is (n/2) +1

    For Comp var, the storage is n/2

    Numeric max 18 ,

  • 8/2/2019 1 0 Cobol Funda

    3/24

    Character max 160

    Special char in a USER-DEFINED var is .

    Use Single quotes for Alphanumeric and alpha characters.

    EDIT MASK:

    USAGE:It can be

    DISPLAY default Normal display

    COMP Binary

    COMP-1 single precision floating point

  • 8/2/2019 1 0 Cobol Funda

    4/24

    COMP-2 double precision floating point

    COMP-3 packed decimal point

    JUSTIFIED:

    Can be used only on alpha or alphanumeric variables.

    INITIALIZE Statement:The variables / group items are initialied. Numeric to 0, alpha

    and alphanumeric to spaces, special characters with special characters.

    Cannot initialize a FD entry.

    MOVE / COMPUTE:With MOVE statement truncation in destination is possible. This

    can be avoided in numeric variables with COMPUTE.

    (1) MOVE ordinay move

    (2) MOVE ALL

    (3) MOVE CORRESPONDING

    OCCURS Clause:

  • 8/2/2019 1 0 Cobol Funda

    5/24

    01 table-name.

    05 element-name OCCURS n TIMES.

    . . . (subordinate items of the table element might follow)

    OCCURS clause cannot occur with a 01 level item.

    To refer the elements , use MAIN-GRP (2, 2, 1) or MAIN-GRP (2 2 1)

    01 GRP-1.

    05 GRP-2 OCCURS 2 TIMES.

    10 GRP-3 OCCURS 3 TIMES.

    15 GRP-4 PIC X(1).

    GRP-2 (1) = A,B,C

    GRP-2 (2) = D,E,F

    GRP-2 (1 3) = C

    GRP-2 (2,2) = E

    Subscripting starts with 1.

    SEARCH and SEARCH ALL:

    The conditions in the WHEN option are evaluated in the order in which

    they appear:

    -If none of the conditions is satisfied, the index is increased

    to correspond to the

    next table element, and the WHEN conditions are evaluated again.

    - If one of the WHEN conditions is satisfied, the search ends.

    The index remains

    pointing to the table element that satisfied the condition.

    - If the entire table has been searched and no conditions were

    met, the AT END

    imperative statement is executed if there is one. If you do not use AT

    END,control passes to the next statement in your program.

    SEARCH is used with indexing on a table and can be used at one level of

    table at a time. Used nested SEARCH for multiple dimensional search.

    SEARCH serial search

    SEARCH ALL binary search

  • 8/2/2019 1 0 Cobol Funda

    6/24

    SEARCH

    AT END

    WHEN

    END-SEARCH

    For SEARCH, set the index before the search command.

    For SEARCH ALL, no need to set the index. But the key should be in the

    ascending order.

    . If you want the search to be done on an array sorted in descending

    order, then while defining the array, you should give DESCENDING KEY

    clause. (You must load the table in the specified order).

    Subscript and INDEX:Subscript refers to the array occurrence while index is the

    displacement (in no of bytes) from the beginning of the array. An index

    can only be modified using PERFORM, SEARCH & SET. Need to have index

    for a table in order to use SEARCH, SEARCH ALL.

    An index is a register item that exists outside the program's working

    storage. You SET an index to a value and SET it UP BY value and DOWN

    BY value.

    Indexing uses binary displacement. Subscripts use the value of the

    occurrence.

    IF Statement:

    IF

  • 8/2/2019 1 0 Cobol Funda

    7/24

  • 8/2/2019 1 0 Cobol Funda

    8/24

    Evaluate statement can have even conditions in its WHEN clause. Use

    ALSO clause if more than one variable has to be checked. ALSO implies

    an AND condition.

    EVALUATE TRUE ALSO TRUE

    WHEN ALSO

    .

    .

    .

    END-EVALUATE.

    CONTINUE and NEXT SENTENCE:The CONTINUE is used to pass the control to the next available

    imperative statement.

    The NEXT SENTENCE is used to take the control only after the period (.)

    IF a > b THEN

    CONTINUE or NEXT SENTENCEELSE

    END-IF

    DISPLAY FIRST

    DISPLAY SECOND.

    DISPLAY THIRD.

    o/p for CONTINUE FIRST, SECOND , THIRD

    o/p for NEXT SENTENCE THIRD

    EXIT:

    EXIT stmt shud be the only stmt if written. It does nothing.

    SWITCHES /FLAGS:

    A Condition name can be used under level-item. If the level-item has

    only one 88 level, its a SWITCH. If it has two or mote 88 levels, its

    a FLAG. A 88 level item can be activated either by directly assigning

    the corresponding value to the level-item or by using SET

    TO TRUE. An 88 level item can be directly used in a IF / EVALUATE.

    88 flag-1 VALUE IS 8.

    88 flag-1 VALUES ARE 10,15,34.

    88 flag-1 VALUES 10 THRU/THROUGH 15.

    PERFORM statements:

    (1) PERFORM [THRU ]

    (2) PERFORM [THRU ] UNTIL

    (3) PERFORM VARYING from BY UNTIL

  • 8/2/2019 1 0 Cobol Funda

    9/24

    END-PERFORM.

    (4) PERFORM n TIMES.

  • 8/2/2019 1 0 Cobol Funda

    10/24

    The loop exists when condition become true. Loop can be used with WITH

    TEST AFTER / BEFORE. By default, a PERFORM is TEST BEFORE.

    STRING Statement:

    STRING

    DELIMITED BY SIZE

    INTO

    DELIMITED BY SIZE takes only the characters from variables to join into

    the output variable. The extra spaces in the variables are eliminated.

    UNSTRING statement:

    UNSTRING DELIMITED BY SPACE / X 00

    INTO

    COUNT cnt-var

    ON OVERFLOW

    END-UNSTRING.

  • 8/2/2019 1 0 Cobol Funda

    11/24

    INSPECT Statement:

    (1) INSPECT TALLYING FOR CHARACTERS BEFORE / AFTER

    (2) To get the number of actual chars stored in a str-var, use

    INSPECT WS-STR TALLYING STR-LEN FOR CHARACTERS BEFORE (i.e.)

    2 spaces.

    (3) INSPECT

    TALLYING

    FOR (CHARACTERS AFTER/BEFORE) / ALL / LEADING

    (4) INSPECT

    REPLACING [ALL / LEADING / FIRST CHARACTERS] BY

    [AFTER / BEFORE INITIAL ]

    (5) INSPECT

    CONVERTING abcdefghijklmnopqrstuvwxyz TO

    ABCDEFGHIJKLMNOPQRSTOVWXYZ

    [AFTER / BEFORE INITIAL ]

    REFERNCE MODIFICATION:

    Var-name ( starting position : length of chars)

    LENGTH OF var -> gives the entire length of the string.(not the numberof actual characters stored.

    WHEN-COMPILED -> gives the compilation date in the formatYYYYMMDDhhmmsshh

    STATIC CALL:

  • 8/2/2019 1 0 Cobol Funda

    12/24

    If the CALL statement has a specific program name, the call is

    static. If the LOAD module shows the display statement strings, then it

    is a STATIC CALL.

    DYNAMIC CALL:

    If the CALL statement has a variable name, then the call is

    dynamic. If the LOAD module holds the program name rather than its

    contents, its a dynamic call.

    IMPLICIT CALL:Calling a working storage variable, containing a program name,

    does not make a DYNAMIC call. This type of calling is known as

    IMPLICITE calling as the name of the module is implied by the contents

    of the working storage variable

    CALL BY CONTEXT:The parameters passed in a call by context are protected from

    modification by the called program. In a normal call they are able to

    be modified

    CALL:

    CALL pgm-name USING linkage-grp

    REDEFINES:Same storage but different names. Can b used for change in the

    PIC. The redefined item shud hav same level-num as tht of the

    redefining item. Cannot b done on 66 & 88 levels. Length of both items

    shud b same.

    Can redefine a higher PIC to lower PIC value and vice versa.

    Accordingly, the truncation or filling will occur.

    01 WS-DATE PIC X(10).

  • 8/2/2019 1 0 Cobol Funda

    13/24

    01 WS-DATE-GRP REDEFINES WS-DATE.

    05 WS-YR PIC 9(4).

    05 WS-F1 PIC X(1) VALUE -.

    05 WS-MM PIC 9(2).

    05 WS-F2 PIC X(1) VALUE -.

    05 WS-DD PIC 9(2).

    01 WS-STR PIC X(200).

    01 WS-STR-1 REDEFINES WS-STR PIC X(100).

    01 WS-STR PIC X(100).

    01 WS-STR-1 REDEFINES WS-STR PIC X(200).

    RENAMES:

    Renames one or a set of data items

    66 data-item-r RENAMES data-item-1 [THRU data-item-n]

    Cannot rename 01, 77, 88 or another 66 level item.

    SET Statement:

    This is for Condition names

    This is for index

  • 8/2/2019 1 0 Cobol Funda

    14/24

    Computational commands:

    ADD:

    SUBTRACT:

  • 8/2/2019 1 0 Cobol Funda

    15/24

    Multiply:

  • 8/2/2019 1 0 Cobol Funda

    16/24

    DIVIDE:

    COMPUTE:

  • 8/2/2019 1 0 Cobol Funda

    17/24

    FILE PROCESSING:

    ORGANIZATION:(1) SEQUENTIAL Mode of access is Sequential. Records are added asit comes. The only ACCESS mode is SEQUENTIAL. QSAM/PS or ESDS (VSAM

    Sequential)

    SELECT filename ASSIGN TO ddname

    ORGANIZATION IS SEQUENTIAL

    ACCESS MODE IS SEQUENTIAL

    FILE STATUS is file-st

    (2) LINE SEQUENTIAL similar to SEQUENTIAL

    (3) INDEXED records can be identified by a key / alt. key. ACCESSmode can be SEQUENTIAL,RANDOM or DYNAMIC. KSDS (VSAM Indexed) uses this.

    SELECT filename ASSIGN TO ddname

    ORGANIZATION IS INDEXED

    ACCESS MODE IS SEQUENTIAL / RANDOM / DYNAMICRECORD KEY IS rcrd-key

    ALTERNATE RECORD KEY IS alt-key

    FILE STATUS is file-st

    (4) RELATIVE records are identified by their relativepositionnumber. ACCESS mode can be SEQUENTIAL,RANDOM or DYNAMIC. RRDS

    (VSAM relative) uses this.

    SELECT filename ASSIGN TO ddname

    ORGANIZATION IS RELATIVE

    ACCESS MODE IS SEQUENTIAL / RANDOM / DYNAMIC

    RELATIVE KEY IS rltv-key

    FILE STATUS is file-st

    By default, ORGANIZATION seq & ACCESS is Seq and hence can be omitted

    for the PS files

    Q44) How do you reference the following file formats from COBOLprograms:

    Q44)

    Fixed Block File - Use ORGANISATION IS SEQUENTIAL.

    Use RECORDING MODE IS F,

    BLOCK CONTAINS 0 .

    Fixed Unblocked - Use ORGANISATION IS SEQUENTIAL.Use RECORDING MODE IS F,

    do not use BLOCK CONTAINS

    Variable Block File -Use ORGANISATION IS SEQUENTIAL.

    Use RECORDING MODE IS V,

    BLOCK CONTAINS 0. Do not code the 4 bytes for

    record length in FD ie JCL rec length will be max rec

    length in pgm + 4

  • 8/2/2019 1 0 Cobol Funda

    18/24

    Variable Unblocked - Use ORGANISATION IS SEQUENTIAL.

    Use RECORDING MODE IS V,

    do not use BLOCK CONTAINS. Do not code 4 bytes

    for record length in FD ie JCL rec length will

    be max rec length in pgm + 4.

    ESDS VSAM file - Use ORGANISATION IS SEQUENTIAL.

    KSDS VSAM file - Use ORGANISATION IS INDEXED,

    RECORD KEY IS,

    ALTERNATE RECORD KEY IS

    RRDS File - Use ORGANISATION IS RELATIVE,

    RELATIVE KEY IS

    Printer File - Use ORGANISATION IS SEQUENTIAL.

    Use RECORDING MODE IS F,

    BLOCK CONTAINS 0. (Use RECFM=FBA in JCL DCB).

    File Status:98 File locked by another user.

    99 Record locked

    92 Logic error

    39 Mismatch in rec len while open

    BLOCK CONTAINS n CHARACTERS if n is 0, it specifies the optimal block

    size the system offers. This is treated as comment under SD.

    READ Statement:

    READ file-name

    INTO record-grp

    AT END

    END-READ

    For indexed key read

    READ file-name INTO rcrd-grp

    KEY IS key-var

    INVALID KEY

    AT END

    END-READ

  • 8/2/2019 1 0 Cobol Funda

    19/24

    WRITE Statement:

    WRITE fd-rcrd-grp FROM ws-rcrd-grp

  • 8/2/2019 1 0 Cobol Funda

    20/24

    REWRITE statement:

    REWRITE fd-rcrd-grp FROM ws-rcrd-grp

    DELETE Statement:Delete can b performed only on Indexed and Relative files.

    DELETE file-name RECORD

    INVALID KEY /[ NOT INVALID KEY ]

  • 8/2/2019 1 0 Cobol Funda

    21/24

    In READ, WRITE, REWRITE, DELETE, use INVALID KEY clause for indexed

    files to handle invalid key condition.

    OPEN Statement:

    OPEN INPUT / OUTPUT / I-O / EXTEND file-name

    EXTEND to append. If existence of file is not sure, use SELECT

    OPTIONAL instead of SELECT in the ASSIGN clause.

    CLOSE Statement:CLOSE filename

    ACCEPT Statement

    ACCEPT iden-1 FROM DATE ( will b in YYMMDD)

    DATE YYYYMMDD

    DAY (will b in YYDDD)

    DAY YYYYDDD

    DAY-OF-WEEK

    TIME ( will b in hhmmssss)

    MERGE files:This merges the input files based on the gn. keyset into out-

    file. The order wil b ASC /DESC as specified. O/p proc specifies any

    additional logic tht is needed to perform the MERGE.

    MERGE

    ON ASCENDING / DESCENDING KEY

    USING

    OUTPUT PROCEDURE IS [THRU ]

    /GIVING

  • 8/2/2019 1 0 Cobol Funda

    22/24

    SORT files:

    This sorts the input files based on the keyset into out-file. Key

    shud b present in the same physical location in i/p files. The

    DUPLICATES is specified, the duplicates are handled by the INPUT PROC

    if specified. If i/p proc is not specified, they are written in the

    order they are retrieved from a particular file or one or more files.

    i/p proc applied to input recs before sorting

    o/p proc applied to output recs after sorting

    SORT sort-file

    ON ASCENDING / DESCENDING

    [WITH DUPLICATES [IN ORDER]]

    USING

    / INPUT PROCEDURE IS [THRU ]

    GIVING

    / OUTPUT PROCEDURE IS [THRU ]

    SORT file-1 ON ASCENDING/DESCENDING KEY key.... USING file-2

    GIVING file-3.

    USING can be substituted by INPUT PROCEDURE IS para-1 THRU para-2

    GIVING can be substituted by OUTPUT PROCEDURE IS para-1 THRU para-

    2.

  • 8/2/2019 1 0 Cobol Funda

    23/24

    file-1 is the sort (work) file and must be described using SD entry

    in FILE SECTION.

    file-2 is the input file for the SORT and must be described using

    an FD entry in FILE SECTION and SELECT

    clause in FILE CONTROL.

    file-3 is the out file from the SORT and must be described using an

    FD entry in FILE SECTION and SELECT

    clause in FILE CONTROL.

    file-1, file-2 & file-3 should not be opened explicitly.

    INPUT PROCEDURE is executed before the sort and records must be

    RELEASEd to the sort work file from the input procedure.

    OUTPUT PROCEDURE is executed after all records have been sorted.

    Records from the sort work file must be RETURNed one at a time to the

    output procedure

    Internal / External Sort:An external sort is not COBOL; it is performed through JCL and

    PGM=SORT. It is understandable without any code reference. An internal

    sort can use two different syntaxs: 1.) USING, GIVING sorts are

    comparable to external sorts with no extra file processing; 2) INPUTPROCEDURE, OUTPUT PROCEDURE sorts allow for data manipulation before

    and/or after the sort.

    Communication b/w COBOL and JCL:

    To pass values from JCL to COBOL, use PARM= in the EXEC , or use SYSIN

    DD *, or parmlib in case of PROC.

    To set a return code to the JCL from a COBOL program, Move a value to

    RETURN-CODE register. RETURN-CODE should not be declared in your

    program.

    GENERAL:

    Q14) My program has an array defined to have 10 items. Due to a bug, Ifind that even if the program access the11th item in this array, the program does not abend. What is wrong

    with it?A14) Must use compiler option SSRANGE if you want array bounds

    checking. Default is NOSSRANGE

    Q51) What is Static and Dynamic linking ?Q51) In static linking, the called subroutine is link-edited into the

    calling program , while in dynamic linking, the subroutine & the

    main program will exist as separate load modules. You choose

    static/dynamic linking by choosing either the DYNAM or NODYNAM link

    edit option. (Even if you choose NODYNAM, a CALL identifier (asopposed to a CALL literal), will translate to a DYNAMIC call).A

    statically called subroutine will not be in its initial state the

    next time it is called unless you explicitly use INITIAL or you do a

    CANCEL. A dynamically called routine will always be in its initial

    state.

    Q52) What is AMODE(24), AMODE(31), RMODE(24) and RMODE(ANY)?(applicable to only MVS/ESA

  • 8/2/2019 1 0 Cobol Funda

    24/24

    Enterprise Server).Q52) These are compile/link edit options. Basically AMODE stands for

    Addressing mode and RMODE for Residency

    mode.

    AMODE(24) - 24 bit addressing;

    AMODE(31) - 31 bit addressing

    AMODE(ANY) - Either 24 bit or 31 bit addressing depending on RMODE.

    RMODE(24) - Resides in virtual storage below 16 Meg line. Use this

    for 31 bit programs that call 24 bit programs.

    (OS/VS Cobol pgms use 24 bit addresses only).

    RMODE(ANY) - Can reside above or below 16 Meg line.

    Q54) What is SSRANGE, NOSSRANGE ?Q54) These are compiler options with respect to subscript out of range

    checking. NOSSRANGE is the default and if chosen,

    no run time error will be flagged if your index or subscript goes

    out of the permissible range.

    Q89) Read the following code. 01 ws-n pic 9(2) value zero. a-para move 5to ws-n. perform b-para ws-n times. b-para.

    move 10 to ws-n. how many times will b-para be executed ?A89) 5 times only. it will not take the value 10 that is initialized in

    the loop.

    Q112) What happens when we move a comp-3 field to an edited (say z (9).ZZ-)

    A112) the editing characters r to be used with data items with usage

    clause as display which is the default. When u tries displaying a

    data item with usage as computational it does not give the

    desired display format because the data item is stored as packed

    decimal. So if u want this particular data item to be edited u

    have to move it into a data item whose usage is display and then

    have that particular data item edited in the format desired.

    Q114) what is the difference between external and global variables?A114) Global variables are accessible only to the batch program whereas

    external variables can be referenced from any batch program

    residing in the same system library.

    Q123) What is the maximum length of a field you can define using COMP-3?A123) 10 Bytes (S9(18) COMP-3).