You are on page 1of 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.

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 items 66 Level number for RENAMES clause. Cannot hav REDEFINES 77 Elementary items. Cannot occur in a FD grp. Cannot hav REDEFINES. 88 Conditon-Names / Switches / Flags. SET statement is used to initialize 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 ,

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

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:

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

SEARCH <table name> AT END <imperative stmts> WHEN <cond> <imperative stmts to be executed when the WHEN is true> 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 <condn>

ELSE

<stmts>

IF <condn> <stmts> ELSE <stmts> END-IF END-IF. CONTINUE ctrl goes to the next available condition NEXT SENTENCE ctrl goes to the stmt tht is present after the immediate period. IF TIME < MOVE END-IF. 2 AND SLOW TIME > 1 TO SPEED THEN

Is equivalent to IF TIME < 2 AND > MOVE SLOW TO 1 THEN SPEED.

Conditions: IS [NOT] POSITIVE NEGATIVE ZERO NUMERIC ALPHABETIC ALPHABETIC-LOWER ALPHABETIC-UPPER

EVALUATE Statement: EVALUATE <var-name> WHEN <val 1> <stmts> . . . WHEN <val2> THRU <val3> <stmts> . . . WHEN <val4> WHEN <val5> <stmts executed when either of val4 or val5 is satisfied> . . . WHEN OTHER <stmts executed when no other WHEN condition is satisfied> END-EVALUATE.

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 <condn 1> ALSO <condn 2> <stmts> . . . 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 ELSE <STMTS> 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. or NEXT SENTENCE

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 <switch/flag> 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) (2) PERFORM <para-name> [THRU <para-name2>] PERFORM <para-name> [THRU <para-name2> ] UNTIL <condn> PERFORM VARYING <var-name> from <indx-val> BY <inc-val> UNTIL <condn>

(3)

END-PERFORM. (4)

<set of statements>

PERFORM <para-name> n TIMES.

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 <var-set> DELIMITED BY SIZE INTO <o/p var>

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 <srce-var> DELIMITED BY SPACE / X 00 INTO <vars set> COUNT cnt-var ON OVERFLOW <statements> END-UNSTRING.

INSPECT Statement: (1) INSPECT <var> TALLYING <num-var> FOR CHARACTERS BEFORE / AFTER <val> (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 <varname> TALLYING <cnt> FOR (CHARACTERS AFTER/BEFORE) / ALL / LEADING <char>

(4)

INSPECT <varname> REPLACING [ALL / LEADING / FIRST CHARACTERS] <char> BY <char> [AFTER / BEFORE INITIAL <char>] INSPECT <var> CONVERTING abcdefghijklmnopqrstuvwxyz TO ABCDEFGHIJKLMNOPQRSTOVWXYZ [AFTER / BEFORE INITIAL <char>]

(5)

REFERNCE MODIFICATION: Var-name ( starting position : length of chars) LENGTH OF var -> gives the entire length of the string.(not the number of actual characters stored. WHEN-COMPILED -> gives the compilation date in the format YYYYMMDDhhmmsshh STATIC CALL:

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

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

Computational commands: ADD:

SUBTRACT:

Multiply:

DIVIDE:

COMPUTE:

FILE PROCESSING: ORGANIZATION: (1) SEQUENTIAL Mode of access is Sequential. Records are added as it 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. ACCESS mode can be SEQUENTIAL,RANDOM or DYNAMIC. KSDS (VSAM Indexed) uses this. SELECT filename ASSIGN TO ddname ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL / RANDOM / DYNAMIC RECORD KEY IS rcrd-key ALTERNATE RECORD KEY IS alt-key FILE STATUS is file-st (4) RELATIVE records are identified by their relative positionnumber. 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 RELATIVE KEY IS rltv-key FILE STATUS is file-st

/ RANDOM / DYNAMIC

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 COBOL programs: 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

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. Use ORGANISATION IS SEQUENTIAL. Use ORGANISATION IS INDEXED, RECORD KEY IS, ALTERNATE RECORD KEY IS

ESDS VSAM file KSDS VSAM file -

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 98 99 92 39 Status: File locked by another user. Record locked Logic error 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 <stmts> END-READ For indexed key read READ file-name INTO rcrd-grp KEY IS key-var INVALID KEY <stmts> AT END <stmts> END-READ

WRITE Statement: WRITE fd-rcrd-grp FROM ws-rcrd-grp

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 <stmts> / [ NOT INVALID KEY <stmts> ]

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 outfile. The order wil b ASC /DESC as specified. O/p proc specifies any additional logic tht is needed to perform the MERGE. MERGE <sort-file> ON ASCENDING / DESCENDING KEY <key-set> USING <input-files> OUTPUT PROCEDURE IS <para1> [THRU <para-n>] /GIVING <out-file>

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 <keyset> [WITH DUPLICATES [IN ORDER]] USING <in-file-set> / INPUT PROCEDURE IS <para-i1> [THRU <para-in> ] GIVING <out-file> / OUTPUT PROCEDURE IS <para-o1> [THRU <para-on>] 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 para2.

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) INPUT PROCEDURE, 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, I find that even if the program access the 11th 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 (as opposed 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

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

You might also like