COBOL Short Notes


Why COBOL?

COBOL is the best programming language that uses side-by-side with mainframe computing. COBOL is essential for the global economy because most financial applications run on a Mainframe System with COBOL as a back-end.

Does COBOL has Future?

COBOL has the future and it is the most wanted programming language for our day-to-day activities today. It's not dead. It's not dying. It will not die in the near future and will be with us for more than decades.

What is COBOL?

COBOL is an acronym for COmmon Business-Oriented Language, primarily developed for business, finance, and administrative system needs.

What is the Current version of COBOL?

Enterprise COBOL v6.4 (2022) is the current version and developed with COBOL/Java interoperability, interoperability between AMODE 31 (31-bit) and AMODE 64 (64-bit).

Different types of COBOL programs?

Based on the COBOL usage and program execution, the programs are divided into two types. Those are - batch programs and online programs.


Features?

  • Simple
  • Standard language
  • Stable
  • Business oriented
  • Structured
  • Robust
  • Easy to read and learn
  • Self-documenting

Abend -

An unexpected or abnormal termination of the program due to some problem in code or data.

Syntax -

It is a system-defined standard that should follow while coding the COBOL statements like MOVE, IF, PERFORM, etc.

Program -

A program contains the actual code to perform the specific task.

Physical File -

It is the actual file that contains the data in the form of records.

Logical File -

It is a name used in the program that refers the actual physical file.

Compiler -

It is a utility that coverts source code (high-level language) into object code (machine language).

Load modules -

It is an executable component of the program that is generated by the compiler.


Character Set -

It is a collection of valid characters(78). They define literals, variables, and other identifiers in a COBOL program. The valid characters contains alphabets (A-Z | a-z), digits (0-9), and special characters.

Words -

It is a combination of - A to Z, 0 to 9, - (hyphen), _ (underscore). Words are two type -

  • User-defined Words - Programmer defined words. For Example - MTHPROG1, STD-GENDER, ....
  • Reserved Words - System-defined words. For Example - ACCEPT, SKIP1, ZEROS, ....

Variable (data item) -

It is used to store and process the value. Its length is 1-30 characters. For Example - WS-A, WS-VAR, WS-TOTAL, WS-INPUT, WS_OUTPUT, etc.

Literal -

It is the value that is assigned to the variable. These are two types -

  • Non-numeric literals - For Example - "HELLO", "THIS ISN'T WRONG", etc.
  • Numeric literals - For Example - 1234, -1234, etc.

Constant -

If the variable value doesn't change during the program's execution, the variable is considered as a constant variable, and the value is considered as a constant value. Constants are three types -

  • Numeric constants – For Example - 3.14
  • Alphanumeric | non-numeric constants – For Example - "HI"
  • Figurative Constants - For Example - ZEROES, SPACES, HIGH-VALUES, etc.

Comments -

A comment is a non-executable statement that provides the information about code or business requirements. These are three types -

  • IDENTIFICATION DIVISION Comments - For Example - NameOfProgrammer is a comment.
           AUTHOR. NameOfProgrammer.
  • Full line comments (any division) - Line starting with an asterisk (*) in column 7 is a full-line comment. For Example -
          * FULL LINE COMMENT WITH * IN COLUMN-7.
  • Floating comment indicator (*>) - Comment starts in the middle of any line between 8-72 columns. For Example -
           01 WS-VAR  PIC X(12). *> INLINE COMMENT   

COBOL program structure is a top-to-bottom hierarchical design, consisting of Divisions, Sections, Paragraphs, Sentences, Statements, and Character strings.

Character Strings -

Sequences of characters that are formed by character set. They are also known as literals, variables, keywords, etc. For example - WS-VAR, ZEROES, MOVE, IF, DISPLAY, etc.

Statements -

A COBOL statement is a combination of COBOL keywords and operands to perform a task. All statements should code in Area-B. For Example - Two statements in a program.

           DISPLAY "Hello World".      --> Statement1
           DISPLAY "Welcome to MTH".   --> Statement2

Statements are 4 types -

  • Imperative Statement - It is a statement specifying an action to be taken during the program's execution without ERROR and EXCEPTION phases. For Example - ACCEPT, MOVE, INSPECT, STRING, UNSTRING, etc.
  • Conditional Statement - It tests a condition and then executes statements based on the result of that condition. For example - IF, EVALUATE, SEARCH, etc.
  • Delimited scope Statement - It marks the statement ending by specific keywords. These are two types -
    • Explicit Scope terminators - It mark the end of a statement and are specific only to the statement. For example - END-IF, END-CALL, END-COMPUTE, etc.
    • Implicit Scope terminators - It is used to end the statement implicitly and is used to terminate all statements. For example - Period (.).
  • Compiler directing Statement - It provide instructions to the compiler about how to compile the source program. For example - COPY, EJECT, SKIP, etc.

Sentences -

It is a sequence of two or more statements ended by a period (.). For example - A sentence with two statements.

           DISPLAY "Hello World"       --|
           DISPLAY "Welcome to MTH".   --|--> Sentence 

Paragraphs -

It is a block of code consisting of one or more sentences or statements. For example - A paragraph in the program.

       1000-PARA.                --> Paragraph
           DISPLAY "Hello World" 
           DISPLAY "Welcome to MTH".         
           STOP RUN.

Sections -

It is a collection of one or more related paragraphs within the PROCEDURE DIVISION. For example - Sections in the program.

       1000-SEC1 SECTION.     --> Section
       1000-PARA.
           DISPLAY "Hello World" 
           DISPLAY "Welcome to MTH".
           STOP RUN.

Divisions -

It is a collection of one or more sections and paragraphs. For example - Sections in the program.

       IDENTIFICATION DIVISION.  --> Division
       ...
       PROCEDURE DIVISION.       --> Division
       1000-SEC1 SECTION.
       1000-PARA.
           DISPLAY "Hello World" 
           DISPLAY "Welcome to MTH".
           ...


Divisions -

These are primary components of a program, that organizes its code and functionality. All divisions are system-defined and should begin in Area A.

COBOL has four divisions -

  • IDENTIFICATION DIVISION - It is mandatory first division in the program. It provides metadata about the program, such as its name, author, installation, and other descriptive information used to uniquely identify the program.
     IDENTIFICATION DIVISION.
     PROGRAM-ID. NameOfProgram.
     [AUTHOR. NameOfProgrammer.]
     [INSTALLATION. Installation-details.]
     [DATE-WRITTEN. program-written-date.]
     [DATE-COMPILED. program-compiled-date.]
     [SECURITY. security-status.]
    • PROGRAM-ID - Mandatory and provides the name of the COBOL program. This name is used to execute the program.
    • AUTHOR (optional) - Provides the name who written the program.
    • INSTALLATION (optional) - Offers installation details.
    • DATE-WRITTEN (optional) - Specifies program written date.
    • DATE-COMPILED (optional) - Provides the last compiled date.
    • SECURITY (optional) - Specifies the program's security level.

  • ENVIRONMENT DIVISION - It defines the input and output sources (files) required to run the program and interact with devices like printers, files, etc. It is optional. It has two sections: the CONFIGURATION SECTION and the INPUT-OUTPUT SECTION.

     ENVIRONMENT DIVISION. 
     [CONFIGURATION SECTION. 
     [SOURCE-COMPUTER. source-computer-name [WITH DEBUGGING MODE]
     [OBJECT-COMPUTER. object-computer-name]
     [SPECIAL-NAMES. special-names-enties]]
    
     [INPUT-OUTPUT SECTION.
     [FILE-CONTROL. file-control-entries]
     [I-O-CONTROL.  i-o-control-entries]]
    • SOURCE-COMPUTER - Specifies the computer on which the source program is compiled. WITH DEBUGGING MODE is used to enable the debugging switch in the program.
    • OBJECT-COMPUTER - Specifies the computer where the object program is to be executed.
    • SPECIAL-NAMES - Specifies overrides symbolic characters to system-specific characters.
    • FILE-CONTROL - Used to map logical files to physical files on the system.
    • I-O-CONTROL - Provides specifications for the computer's input-output operations.

  • DATA DIVISION - It defines the variables that are used for data processing. It is optional when no variables are declared. It has three sections: FILE SECTION, WORKING-STORAGE SECTION, and LINKAGE SECTION.
    DATA DIVISION.
        [FILE SECTION.
         FD|SD file-description-entry...
             [data-description-entry...]]
    
        [WORKING-STORAGE SECTION.
             data-description-entry...]
    
        [LINKAGE SECTION.
             data-description-entry...]
    • FILE SECTION - Used to declare the files layout that are read from or written to.
    • WORKING-STORAGE SECTION - Defines variables for data processing.
    • LINKAGE SECTION - Used to declare the variables to pass the data into or out of the program.

  • PROCEDURE DIVISION - The actual logic will be written in PROCEDURE DIVISION, which is responsible for carrying out the data processing tasks.
    PROCEDURE DIVISION [USING parameter...].
        [section-name SECTION.]
        paragraph-name.
            [statements...]
        [additional-paragraphs...]
        [... more sections and paragraphs ...]

Coding Sheet specifies from where each statement coding should start and how it differentiates from other statements.

Coding Sheet Split -

The coding sheet contains 80 columns for each line/row. A row is divided into multiple parts. Those are -

  • 1-6 Columns (Sequence Number Area) - It automatically increases with every addition of a new line.
    • Page Number (1-3 Columns).
    • Line Number (4-6 Columns) on the page.
  • 7th Column (Indicator Area) - Reserved for special characters.
    • Comment indicator (*) If "*" is coded, the COBOL compiler considers that line as a comment.
    • Continuation indicator (-) If "-" is coded, the COBOL compiler understands that the current line is a continuation of the previous line.
    • Print stopper indicator (/) If "/" is coded, it specifies that the printing should start from the new page.
    • Debugging indicator (D) If the "D" is coded, the COBOL compiler understands that line is used for DEBUGGING purposes.
  • 8-11 Columns (Area-A) - All DIVISION names, SECTION names, system-defined paragraph names, user-defined PARAGRAPH names, Level numbers (01, 77), DECLARATIVES, END DECLARATIVES, and FD/SD entries coding should start in Area-A.
  • 12-72 Columns (Area B) - All entries, sentences, statements, clauses, and continuous lines should start in Area-B.
  • 73-80 Columns (System Generated Number) - Reserved for System Generated Numbers and used for identification purposes.

Declaring variables is important and is required to process the data within the program. The variable declaration is as follows -

Level Number -

It specifies the hierarchy of data items or variables in the declaration. The valid level numbers are 01 to 49, 66, 77, and 88. Level numbers are of two types based on their usage -

  • General purpose level numbers (01 to 49) - It declares regular variables. The hierarchy of level numbers should be in ascending order. For example, 01 is the highest level number, and 49 is the lowest level number.
  • Special purpose level numbers (66, 77 and 88) - Declares the variables for special purposes.
    • 66 level number - Creates a logical group by regrouping the elementary variables of a group using RENAMES clause.
    • 77 level number - Declares the individual variables. It's not part of any hierarchical structure and doesn't subordinate other variables.
    • 88 level number - Defines a condition name for a set of values under the variable and provides a descriptive name for a condition.

Data item (Variable) -

It is a name used to hold the value for processing in the program. It should declare in DATA DIVISION. They are three types -

  • Individual variable - Declares variable individually but not under a group variable or as a group variable. For example - Declaring individual variable.
          * Individual variable
           77 LEVEL-1       PIC 9(03) VALUE 256.
  • Group variable - Declares a variable without the picture clause and has elementary variables under it. It does not have a picture clause.
  • Elementary variable - Declares a variable that is declared under the group variable. It should always have a PIC clause. For example - Declaring a group.
          * Group variable
           01 LEVEL-GROUP.
          * Elementary variables
              05 LEVEL-21       PIC 9(03) VALUE 256.
              05 LEVEL-22       PIC 9(03) VALUE 128.

PICTURE Clause -

It specify the characteristics of the variable while declaring it. i.e., variable type, length, etc. PICTURE clause is a letter used to specify the type of the variable. For example - A, B, E, G, N, P, S, V, X, Z, CR, DB.

Data Types -

It specifies the kind of data a variable can hold. Data types are 5 types -

Data Type Description
Numeric Allows to declare the variables to store the numeric values that are the combination of 0 to 9 numbers.
Declaration Symbol - 9
Allowed Characters - 0 to 9.
For Example -
 01 WS-NUM     PIC 9(5).
Alphabet Allows to declare the variables to store the alphabetic strings that are the combination of A to Z or a to z characters.
Declaration Symbol - A
Allowed Characters - Space + - * / = $ Comma(,) ; Decimal point(.) " ' ( ) > < : _ A-Z, a-z.
For Example -
 01 WS-NUM     PIC A(15).
Alpha-numeric Allows to declare the variables to store the strings that are combination of alphabets and numbers.
Declaration Symbol - X
Allowed Characters - Space + - * / = $ Comma(,) ; Decimal point(.) " ' ( ) > < : _ A-Z, a-z 0-9.
For Example -
 01 WS-NUM     PIC X(15).
Sign Allows to declare the numeric variable with sign to capture the negative values. Sign can specify for numeric values.
Declaration Symbol - S
Allowed Characters - - (minus) or + (plus)
For Example -
 01 WS-SIGNED-NUMBER     PIC S9(5).
Decimal point When a data comes as a decimal to the program, a variable should declare with decimal point to handle the decimal value.
Declaration Symbol - P/V
Allowed Characters - . (Dot)
For Example -
 01 WS-TOTAL     PIC 9(5)V9(2).
 01 WS-SUM       PIC 9(5)P.

VALUE Clause -

It is used to assign an initial value to a variable at the time of declaration. For example - Alphanumeric Initialization.

 WORKING-STORAGE SECTION.
 01 WS-NAME      PIC X(15) VALUE 'Mainframes'.

BLANK WHEN ZERO Clause -

It is used to define a variable to display spaces when its value is zero. For example - Declaring a variable.

 WORKING-STORAGE SECTION. 
 01 WS-VAR. 
* Declaring a variable with BLANK WHEN ZERO
    05 WS-BWZ-VAR    PIC ZZ,ZZ9.9(2) BLANK WHEN ZEROES.

JUSTIFIED | JUST Clause -

It overrides the default data alignment and aligns it to the right while displaying it. It applies only to alphabetic and alphanumeric variables. For example - Declaring a variable.

WORKING-STORAGE SECTION.
01 WS-VAR        PIC X(15).
01 WS-RJE-VAR    PIC X(15) JUSTIFIED RIGHT.

SYNCHRONIZED Clause -

System allocates the variables at their respective natural memory boundaries and introduces the slack bytes (unused bytes between the boundary start and the previous allocation ending).

Input-output statements manage data transfer between the program's internal storage and external input or output devices. These are two types -

ACCEPT Statement -

It receives the data from external sources like JCL or the system runtime environment during the program execution. For example - Receiving data from run JCL.

* Receiving name from run JCL
     ACCEPT WS-NAME.

DISPLAY Statement -

It is used to print the variable contents to the output device. For example - DISPLAY with multiple variables.

* Simple DISPLAY
     DISPLAY "MESSAGE: " WS-VAR1 " " WS-VAR2.

Data movement statements are responsible for transferring data between data items. These are –

INITIALIZE Statement -

It sets all elementary variables under group variable with system-defined initial values based on their data types.

MOVE Statement -

It is used to transfer data from the source data item to the target data item. MOVE statements are 4 types -

  • Simple MOVE - used to move the value of the sending item(s) to the receiving item.
  • Group MOVE - used to move data between two groups whose elementary items are in the same order.
  • MOVE Corresponding - used to move data between two groups whose elementary items are not in the same order.
  • MOVE Referencing Modification - is a special-purpose MOVE that moves a portion of data from the sending item to the receiving item.

String handling statements process textual data, allowing for modifications, evaluations, and extractions of character data within a COBOL program. They are -

STRING Statement -

It concatenates two or more strings into a single string and places them into a result variable. For example - Concatenate two strings separated by space.

       PROCEDURE DIVISION.
           STRING "Mainframestechhelp" DELIMITED BY SIZE, 
                  SPACE,
                  "is a Mainframe Community" DELIMITED BY SIZE
             INTO WS-OUTPUT
                  ON OVERFLOW DISPLAY "Error occured"
              NOT ON OVERFLOW DISPLAY "Result: " WS-OUTPUT
           END-STRING.

UNSTRING Statement -

It takes a single string, breaks it down into several separate strings, and places them into the variables. For example - Split the string into two strings.

       PROCEDURE DIVISION.
           UNSTRING WS-INPUT DELIMITED BY ","
               INTO WS-OUTPUT1, WS-OUTPUT2
                    ON OVERFLOW DISPLAY "ERROR OCCURED"
                NOT ON OVERFLOW
                        DISPLAY "WS-OUTPUT1:  ", WS-OUTPUT1 
                        DISPLAY "WS-OUTPUT2:  ", WS-OUTPUT2 
           END-UNSTRING.

INSPECT Statement -

It analyzes, counts, or replaces specific character(s) within a string. It has four formats -

  • INSPECT...TALLYING - It counts the occurrences of the specific characters in the input string. For example - Counting for ALL character "A".
           INSPECT "MAINFRAMES" 
                        TALLYING WS-CNT FOR ALL "A".

  • INSPECT...REPLACING - It replace all the instances of certain characters with other characters. For example - Replace all "-" with "/".
             INSPECT "DD-MM-YYYY"
                          REPLACING ALL "-" BY "/". 

  • INSPECT...TALLYING...REPLACING - It counts the occurrences of the specific characters and replaces them with new characters. For example - Count for no of characters and Replace them with "&".
             INSPECT   "DD-MM-YYYY" 
                  TALLYING  WS-CNT FOR CHARACTERS
                  REPLACING CHARACTERS BY "&".

  • INSPECT...CONVERTING - It performs character conversion in a variable. For example - Conveting all uppercase characters to lowercase.
             INSPECT "MAINFRAMES" CONVERTING 
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO
    			  "abcdefghijklmnopqrstuvwxyz". 

A conditional statement derives the truth value (TRUE or FALSE) of a condition, and decide the further program execution flow based on the truth value. The conditional statements are -

IF Statement -

It validates a condition and decides the program flow depending on the condition validation. IF statement has three formats -

  • Simple IF - It is used to execute the set of statements only if a condition is TRUE.
  • IF...ELSE - It is used to validate a condition and handles both (either TRUE or FALSE) outcomes of the condition.
  • Nested IF - IF statement coded within another IF statement is called as NESTED IF statement.

EVALUATE Statement -

It is used to validate multiple conditions at a time and controls the program flow based on the first condition match found. It is a shorter form for the nested IF...ELSE statements. EVALUATE has five formats -

  • Simple EVALUATE - It has only one condition and validates a single variable against multiple values.
  • EVALUATE TRUE - Validates multiple conditions instead of checking a variable's value. EVALUATE has the boolean value (TRUE or FALSE), and WHENs have the logical expressions.
  • EVALUATE with THRU - It is used to validate the variable with values range. The values range should be in ascending order.
  • EVALUATE using multiple WHEN - EVALUATE with multiple WHEN used to validate the variable with a set of values (different values).
  • EVALUATE with Multiple Conditions - EVALUATE with multiple conditions used to validate the set of conditions combined with ALSO.

CONTINUE -

It transfers the control to the immediate COBOL statement that comes next in the program flow. For example - Salary is greater than 5000, do nothing.

Input - WS-SALARY = 6000.

	 ...
   IF WS-SALARY GREATER THAN 5000
	  CONTINUE
   ELSE
	  COMPUTE WS-SALARY = WS-SALARY + 2000
   END-IF.
* Displaying Salary
   DISPLAY "SALARY: " WS-SALARY.

NEXT SENTENCE -

It transfers the control to the next COBOL statement immediately after the next period ('.'). For example - Salary is greater than 5000, do nothing.

Input - WS-SALARY = 7000.

	 ...
   IF WS-SALARY GREATER THAN 5000
	  NEXT SENTENCE
   END-IF
   COMPUTE WS-SALARY = WS-SALARY + 2000.
* Displaying Salary
   DISPLAY "SALARY: " WS-SALARY.

Iterative programming involves making the set of statements run in a repetitive, iterative, or looping manner. PERFORM statement is part of the iterative programming in COBOL and it is also called as looping statement.

PERFORM Statement -

It executes the statement block repeatedly either conditional or unconditional. PERFORM statement is two types -

  • Inline PERFORM - It executes the statements block that is coded inline (between PERFORM and END-PERFORM). Scope terminator END-PERFORM is mandatory.
  • Outline PERFORM - It executes statements block that are coded outline (in a separate paragraph or section outside of PERFORM). Scope terminator END-PERFORM is not required.

PERFORM statement has 5 formats -

  • Simple PERFORM - It is a way to execute a given paragraph or section once, and control is passed to the next statement in the flow.
  • PERFORM...THRU - It is used to execute a specific paragraph or a range of paragraphs.
  • PERFORM...TIMES - It is used to execute a specific paragraph or section a certain number of times.
  • PERFORM...UNTIL - It is used to execute a statement block repetitively until a certain condition is true.
  • PERFORM...VARYING - It is used to execute a specific paragraph or section repetitively while varying the value of one or more control variables.

Arithmetic operations are performed using specific statements rather than inline operators (+, -, *, /). The Arithmetic statements are -

ADD Statement -

It sums two or more numeric values and stores the result in the output variable. For example - Adding number to a variable.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-A      PIC 9(03) VALUE 20.
 PROCEDURE DIVISION.
	 ADD 10 TO WS-A.
	 DISPLAY "Result: " WS-A.

SUBTRACT Statement -

It can subtract one or more numeric items from another numeric item, or it can subtract two numeric items and store the result in a third. For example - Subtract one variable from other.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-A      PIC 9(03) VALUE 20.
    05 WS-B      PIC 9(03) VALUE 30.
 PROCEDURE DIVISION.
	 SUBTRACT WS-A FROM WS-B.
	 DISPLAY "Result: " WS-B.

MULTIPLY Statement -

It allows multiplying one or more numeric items and storing the result in one or more receiving items. For example - Multiplying two variables.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-A      PIC 9(03) VALUE 20.
    05 WS-B      PIC 9(03) VALUE 30.
 PROCEDURE DIVISION.
	 MULTIPLY WS-A BY WS-B.
	 DISPLAY "Result: " WS-B.

DIVIDE Statement -

It divides one number by another. For example - Multiplying two variables.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-A      PIC 9(03) VALUE 20.
    05 WS-B      PIC 9(03) VALUE 30.
 PROCEDURE DIVISION.
	 MULTIPLY WS-A BY WS-B.
	 DISPLAY "Result: " WS-B.

COMPUTE Statement -

It is used to perform all arithmetic operations. These operators used in COMPUTE statements are + (ADD), - (SUBTRACT), * (MULTIPLY), / (DIVIDE), and ** (EXPONENT) statements. For example - Adding one variable with other.

 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-VAR1      PIC 9(03) VALUE 50.
    05 WS-VAR2      PIC 9(03) VALUE 5.
    05 WS-VAR3      PIC 9(03).
 PROCEDURE DIVISION.
	 COMPUTE WS-VAR3 = WS-VAR1 + WS-VAR2.
	 DISPLAY "Result: " WS-VAR2.

Array is a collection of individual data items of same type and length. COBOL arrays are also known as tables.

Table Declaration -

Tables are declared in the DATA DIVISION. OCCURS clause is used to declare the table in program. For example - Let us declare a table to store two student details. WS-CLASS is the group variable and WS-STUDENT is a variable with all student information OCCURS 2 times to capture the two students information.

       DATA DIVISION.
       WORKING-STORAGE SECTION. 
      * Single Dimensional table
       01 WS-CLASS.
          03 WS-STUDENT  OCCURS 2 TIMES.
             05 WS-ROLL-NO      PIC X(03) VALUE "001".
             05 WS-NAME         PIC X(10) VALUE "STUDENT1".

       PROCEDURE DIVISION. 
           DISPLAY "CLASS INFO: " WS-CLASS. 

Accessing Table -

Table data can be accessed in two ways -

  • Subscript - It represents the number of table occurrences. Subscript needs to be declared, initialized and incremented/decremented separately.
  • Index - It refers the table element as the number of displacement positions from the table starting position. Index doesn't need separate declaration. SET statement is used to initialize, increment or decrement index.

Searching Table -

Table search can be done using two statements -

  • SEARCH - It is used to perform a linear search on the table (also known as an array) for a specific item.
  • SEARCH ALL - is used to perform binary search on sorted tables (or arrays). Array should be in sorted order.

Memory management techniques refers to how programs allocate, use, and release memory during the program execution. The memory management techniques are -

REDEFINES -

It allows one variable to share the storage area of another variable. For example - Redefining a variable with same and different lengths.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
	      05 WS-VAR1                      PIC X(20). 
          05 WS-RLE-VAR2 REDEFINES WS-VAR PIC X(10).

Computational Items (USAGE Clause) -

Computational items refer to data items defined with the USAGE clause to specify their internal representation. The list of USAGE modes in COBOL are -

  • DISPLAY Computation - It uses the character form where one character equals one byte (8 bits) of storage. It is default and applicable to all data types.

  • BINARY | COMP | COMPUTATION - It is used to store signed decimal numbers in pure binary format and applicable to numeric data items.
    Digits in PICTURE clauseStorage occupied
    1 through 42 bytes (halfword)
    5 through 94 bytes (full word)
    10 through 188 bytes (doubleword)

  • COMP-1 | COMPUTATION-1 - It is used to store single-precision (32 bit) floating-point numbers and applicable to numeric data items. It has no PICTURE clause, which is 4 bytes long (FULL WORD).
    Digits in PICTURE clauseStorage occupied
    1 through 164 bytes (FULL WORD)

  • COMP-2 | COMPUTATION-2 - It is used to store internal double-precision (64 bit) floating-point numbers and applicable to numeric data items. It has no PICTURE clause, and it is 8 bytes long (DOUBLE WORD).
    Digits in PICTURE clauseStorage occupied
    1 through 328 bytes (DOUBLE WORD)

  • COMP-3 | COMPUTATION-3 | Packed Decimal | Packed Numeric - It is used to represent decimal numbers in a compact binary-coded decimal (BCD) format and applicable to numeric data items. The formula for memory calculation of the COMP-3 with n digits in the declaration is -
    • No. of bytes = Round ((n + 1)/2) - Where n is an odd number.
    • No. of bytes = Round (n/2) - Where n is an even number.

Mainframe environment uses the PS (Physical sequential) and VSAM files. Basic terminology of the files are -

Information -

Information is the meaningful data that is stored in the file.

Field or Data item -

The field is the name for the meaningful information in the record. The name refers to variables in a record structure.

Record -

A record is a collection of related fields representing as a single entity. One or more fields together form a record. These are two types -

  • Physical record - One line in the physical file is one physical record.
  • Logical record - It is the record layout that is used by the COBOL program. Only one record can be processed at a time by the program.

File -

A file is a collection of records given a name and permanently stores the data on secondary memory (e.g., DISKs).

A file should be declared with all its characteristics for processing in the application program. The file declaration has two parts -

File Definition (FILE-CONTROL paragraph Entries) -

This definition becomes known to the program and should contain complete information about the file and assign a logical name to the physical file. These definitions are declared in FILE-CONTROL paragraph in INPUT-OUTPUT SECTION of ENVIRONMENT DIVISION. For example - Logical file definition for a fixed-length(80) sequential file(empps) to access sequentially.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT empps ASSIGN TO emppsdd
    ORGANIZATION IS SEQUENTIAL
    ACCESS MODE IS SEQUENTIAL 
    FILE STATUS IS ws-fs1. 

File Structure Definition (File & Record description entries) -

It has the definition of the file logical structure that is needed to process the data in the program. The logical file structure definition should be in the FILE SECTION of DATA DIVISION. For example - Logical file definition and structure definition for a fixed-length(80) sequential file(empps) to access sequentially.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT empps ASSIGN TO emppsdd
    ORGANIZATION IS SEQUENTIAL
    ACCESS MODE IS SEQUENTIAL 
    FILE STATUS IS ws-fs1. 
	
DATA DIVISION.
FILE SECTION.
FD empps
    RECORD CONTAINS 80 CHARACTERS
    BLOCK CONTAINS 800 CHARACTERS
    LABEL RECORD IS OMITTED
	DATA RECORD IS emp-rec
	RECORDING MODE IS F.

01 emp-rec    PIC X(80).

File organization specifies how information is physically stored in the memory and impacts how efficiently data can be accessed and managed. COBOL supports three file organizations -

  • Sequential File Organization - It applies to the sequential files (PS - Physical sequential file) that use the QSAM access method. A sequential file consists of records that are stored and accessed sequentially. Record deletion is not possible. For example - Declaring a sequential file in program.
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
        SELECT empfile ASSIGN TO input01
        ORGANIZATION IS SEQUENTIAL

  • Indexed File Organization - It applies to the KSDS file that uses the Virtual storage access method (VSAM). A KSDS file consists of records that are stored and accessed using the key value. Record deletion is possible. For example - Declaring indexed file in program with key emp-no and alternate key emp-dept.
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
        SELECT empfile ASSIGN TO input01
        ORGANIZATION IS INDEXED
    	RECORD KEY IS emp-no
    	ALTERNATE KEY IS emp-dept

  • Relative File Organization - It applies to the RRDS file that uses the Virtual storage access method (VSAM). An RRDS file consists of records that are stored and accessed using the RRN (Relative Record Number) value. Physical deletion of the record is allowed, but the memory is not reused. For example - Declaring Relative file in program with key emp-rrn.
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
        SELECT empfile ASSIGN TO input01
        ORGANIZATION IS RELATIVE
    	RELATIVE KEY IS emp-rrn

The file access modes specify how data is read from or written to files. COBOL supports three access modes -

  • Sequential access mode - Records are accessed one after another in the order they appear in the file. It applies to - sequential, indexed and relative files. For example - Declaring a sequential file to read sequentially.
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
        SELECT empfile ASSIGN TO input01
        ORGANIZATION IS SEQUENTIAL
    	ACCESS MODE IS SEQUENTIAL

  • Random access mode - Records are accessed based on a specific key value, allowing for the direct retrieval of any record without knowing its position or reading the preceding ones. It applies to - indexed and relative files. For example - Declaring indexed file to read randomly.
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
        SELECT empfile ASSIGN TO input01
        ORGANIZATION IS INDEXED
    	ACCESS MODE IS RANDOM

  • Dynamic access mode - A combination of both SEQUENTIAL and RANDOM access modes. It applies to - indexed and relative files. For example - Declaring indexed file to read dynamically.
    ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
    FILE-CONTROL.
        SELECT empfile ASSIGN TO input01
        ORGANIZATION IS INDEXED
    	ACCESS MODE IS DYNAMIC

COBOL file processing refers to the operations carried out on the files, like reading, writing, updating, and deleting records. Below are the file processing statements -

  • OPEN File - It opens the file for subsequent processing (e.g., reading, writing, updating). The opening modes are -
    ModeDescription
    INPUTFor reading operations only.
    OUTPUT For writing operations only. If a file already exists, ts contents overwritten.
    I-O For both reading and writing. Useful for updating files.
    EXTEND For appending records to an existing file. It applies for sequential access files only.
  • READ Statement - It is used to retrieve a record from the file. At a time, only one record is retrieved from the file. File should be opened in INPUT or I-O mode.
  • WRITE Statement - It is used to add a record to the file.
  • REWRITE Statement - It is used to replace the content of a previously read record with new data.
  • DELETE Statement - It deletes a record from an indexed or relative file.
  • START & READ NEXT – START is used to set the file pointer to read the record. READ NEXT statement is used to read the next record from the current reading position of the file.
  • CLOSE File - It is used to terminate the file processing and release the resources of the file.

An internal sort is a method to sort file directly within the program without relying on external utilities. The statements are -

  • SORT Statement - It is used for sorting files internally within the program.
  • MERGE Statement - It is used to combine two or more sequentially ordered files into a single, merged, and sequentially ordered output file. The files should be in sorted order according to the key that is common for both files.
  • RELEASE Statement - It is used inside the INPUT PROCEDURE of a SORT operation to send records from the input procedure to a sort work file.
  • RETURN Statement - It is used inside the OUTPUT PROCEDURE of a SORT operation to transfer records from the final phase of a sorting or merging operation.

Program communication statements manage interactions between program components, different programs, or external entities. The statements are -

CALL Statement -

It is used to invoke a subprogram to complete a task. CALL statement is always coded in the calling or main program and the program name in the CALL statement is called or subprogram. CALL is two types -

  • Static call - CALL statement with SUBPROG name in quotes makes. For example -
    CALL "SUBPROG" USING WS-INPUT1, WS-INPUT2.

  • Dynamic call - CALL statement with a variable that contains the SUBPROG name. For example -
    MOVE "SUBPROG"  TO WS-SUBPROG.
    CALL WS-SUBPROG USING WS-INPUT1, WS-INPUT2.

CALL with Passing Parameters -

MAINPROG can pass the data to SUBPROG in three ways -

  • BY REFERENCE - Default option. It is used to pass the memory location of the actual data, not a copy of the data. For example - Dynamic call with BY REFERENCE.
    CALL WS-SUBPROG USING BY REFERENCE WS-INP1, WS-PARM2, ...

  • BY VALUE - It is used to pass the copy of the data to the subprogram. For example - Dynamic call with BY VALUE.
    CALL WS-SUBPROG USING BY VALUE WS-INP1, WS-PARM2, ...

CANCEL Statement -

It is used to release the resources associated with a previously called subprogram.

STOP RUN -

is used to terminate the execution of a program and control returns to the operating system.

EXIT PROGRAM -

It marks the end of a subprogram processing and returns control to the main program. It's mainly used in subprograms.

GOBACK -

It defines the logical end of a program and gives the control back from where it was received. It can code in both the main program and subprogram.

GO TO -

It transfers control to another part of the program, allowing the program to "jump" to a different paragraph or section. It has two formats -

  • Unconditional GO TO - It is used to transfer control unconditionally to another part of the program.
  • Conditional GO TO - It is advised to use with conditional statements like IF and EVALUATE.

It is a code block that is not compiled or executed independently. These subroutines can exist within or outside of the application program. There are two types of subroutines -

  • Internal subroutines - These are paragraphs or sections that are coded within the program and can be called by using the PERFORM statement.
  • External subroutines -
    • Subprograms - These are separate modules coded to perform a specific task and can be called by multiple programs.
    • PROCEDURE DIVISION copybooks - These are paragraphs or sections that are added during the program compilation. For example - Using ACCINPUT (PROCEDURE DIVISION copybook) in the COBOL Program.
             ...
             PROCEDURE DIVISION.
                 PERFORM 1000-RECEIVE-INPUTS.
                 ...
                 STOP RUN.
      		   
                 ++INCLUDE ACCINPUT.

      ACCINPUT code copybook -

             1000-RECEIVE-INPUTS.
                 ACCEPT WS-NAME.
                 ACCEPT WS-GENDER.

Error handling is a mechanism used to handle the possible runtime errors by notifying the user with a message or routing the program flow to skip the program abend. COBOL has specific categories to handle the error conditions -

  • Handling errors in STRING and UNSTRING operations - ON OVERFLOW phase is used to handle the overflow condition and notify the end user about it.
  • Handling errors in Arithmetic operations - ON SIZE ERROR phrase can handle the scenarios if they are coded with arithmetic statements.
  • Handling errors in File operations - Techniques used for handling specific input or output conditions or errors -
    • FILE STATUS clause and file status key.
    • End-of-file condition (AT END Phrase) for sequential files.
    • INVALID KEY Phrase for indexed and relative files.
  • Handling errors in CALL statements - ON EXCEPTION phrase handles the CALL errors, and it also handles the errors that occur when the subprogram is unavailable on its initial load.

Intrinsic functions are built-in functions that facilitate various operations without the need for extensive procedural logic. Below list specifies the intrinsic functions -

  • ACOS
  • ADD-DURATION
  • ANNUITY
  • ASIN
  • ATAN
  • CHAR
  • CONVERT-DATE-TIME
  • COS
  • CURRENT-DATE
  • DATE-OF-INTEGER
  • DAY-OF-INTEGER
  • DAY-TO-YYYYDDD
  • EXTRACT-DATE-TIME
  • DATE-TO-YYYYMMDD
  • DISPLAY-OF
  • FACTORIAL
  • FIND-DURATION
  • INTEGER
  • INTEGER-OF-DATE
  • INTEGER-OF-DAY
  • INTEGER-PART
  • LENGTH
  • LOCALE-DATE
  • LOCALE-TIME
  • LOG
  • LOG10
  • LOWER-CASE
  • MAX
  • MEAN
  • MEDIAN
  • MIDRANGE
  • MIN
  • MOD
  • NATIONAL-OF
  • NUMVAL
  • NUMVAL-C
  • ORD
  • ORD-MAX
  • ORD-MIN
  • PRESENT-VALUE
  • RANDOM
  • RANGE
  • REM
  • REVERSE
  • SIN
  • SQRT
  • STANDARD-DEVIATION
  • SUBTRACT-DURATION
  • SUM
  • TAN
  • TEST-DATE-TIME
  • TRIM
  • TRIML
  • TRIMR
  • UPPER-CASE
  • VARIANCE
  • UTF8STRING
  • WHEN-COMPILED
  • YEAR-TO-YYYY

For example - Converts a numeric edited string to a numeric value.

Code-  COMPUTE WS-NUM = FUNCTION NUMVAL('123.45')

Result-     WS-NUM = 123.45

Compiler-directing Statements (often called "directives") provide instructions to the COBOL compiler about how the program should compile and produce the desired object code. The critical statements used in the COBOL program are -

COPY Statement -

It includes predefined copybooks (usually file record structures) from the library that is outside of the program. For example - Insert a copybook into the program.

       01 EMP-REC.
          COPY EMPREC. 

EXIT Statement -

It is a "do-nothing" statement mainly used for readability and structure, giving a logical endpoint for a section or paragraph. For example - EXIT usage in paragraph.

      ...
       1000-DISPLAY.
           DISPLAY "PARAGRAPH EXIT EXAMPLE". 
       1000-EXIT.
           EXIT.

EJECT Statement -

It specifies that the next source statement after it should be printed as the first statement of the next page while printing. For example - Coding EJECT statement in COBOL program.

       01 EMP-REC.
          COPY EMPREC.
		  EJECT.

SKIP Statement -

SKIP1, SKIP2, or SKIP3 statements specify the number of blank lines that should replace the SKIP statement while listing the COBOL source code. For example - Coding SKIP1 statement in COBOL program.

       01 EMP-REC.
          COPY EMPREC.
		  SKIP1.