Thursday, 22 November 2012

COBOL



COBOL stands for Common Business Oriented  Language.

It is maintained by CODASYL-Conference on Data Analysis and Systems Language.

Hierarchy of a COBOL PROGRAM.
  1. Divisions
  2. Sections
  3. Paragraphs
  4. Sentences
  5. Statements
  6. Clause/Word
  7. Characters


There are 4 Divisions in a COBOL Program.

  1. IDENTIFICATION DIVISION.
  2. ENVIRONMENT DIVISION.
  3. DATA DIVISION.
  4. PROCEDURE DIVISION.

.
IDENTIFICATION DIVISION.
It has no sections.The important and compulsory entry is PROGRAM-ID paragraph.

IDENTIFICATION DIVISION.
PROGRAM-ID. MY-FIRST-PROGRAM.

ENVIRONMENT DIVISION.

It has two sections.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.

CONFIGURATION SECTION has two paragraphs.
SOURCE-COMPUTER. IBM-PC.
OBJECT-COMPUTER. IBM-PC
This sections identifies the computer or hardware we are using for the COBOL Program.

INPUT-OUTPUT SECTION has one important paragraph.
FILE-CONTROL.
                            SELECT WS-FILE ASSIGN TO DISK
                             ORGANIZATION IS LINE SEQUENTIAL.
It specifies the data and printer files that are used in a COBOL Program.

DATA DIVISION.

It has 5 sections.

  1. FILE SECTION.
  2. WORKING-STORAGE SECTION.
  3. SCREEN SECTION.
  4. LINKAGE SECTION.
  5. REPORT SECTION.


In FILE SECTION we mention the format of the data files and the printer files used in the program.

FILE SECTION.
FD WS-FILE
                         LABEL RECORDS ARE STANDARD
                         VALUE OF FILE-ID "C:STUDENT.DAT".
01 STUDENT-REC.
                          02  S-NAME PIC A(30).
                          02  S-AGE     PIC 9(2).
                          02 ADDI-INFO PIX X(50).
                       

Here FD is a file identifier and its full form is FILE DESCRIPTION.
WS-FILE is the file name mentioned in the INPUT-OUTPUT SECTION of ENVIRONMENT DIVISION.

C:STUDENT.DAT is the name of the data file stored on c drive of the computer.

 LABEL RECORDS ARE STANDARD means it follows the standard convention of the operating system used.


01 STUDENT-REC.---------------------------------------------------(Record Name)
                          02  S-NAME PIC A(30).-------------------------(Field Name)
                          02  S-AGE     PIC 9(2).---------------------------(Field Name)
                          02 ADDI-INFO PIX X(50).---------------------(Field Name)


The above one is the structure of the record in the student.dat file. 01 and 02 are called level nos.



In WORKING-STORAGE SECTION we mention the data names or identifiers or variables used in the COBOL program.

WORKING-STORAGE SECTION.
77  ANSWER  PIC X VALUE " ".
77 SALARY    PIC 9(4)V99 VALUE 0.

77 is the level number where we can specify individual data names.

SCREEN SECTION is used for formatting the  display screen or the monitor or VDU.
LINKAGE SECTION is used when we use sub programming
REPORT SECTION is for formatting the reports.

Apart from the above sections we can also mention COMMUNICATION SECTION which can be used in NETWORKING environment.


PROCEDURE DIVISION.

It has user defined sections and paragraphs.Here we write the COBOL instructions


COBOL CODING SHEET



COBOL programs are written in coding sheet.The same format need to be followed while typing in the editor.

The coding sheet consists of 1-80 columns.

Columns 1-3   -   Page No.-(optional)
              4-6  -    Line Nos.(Optional)
                 7  -    Special mark like -,*,/
                                  - is for continuation
                                  * is for comment entry
                                  /  is for page skip
             8-11 - A -area
                                 All the division names,section names,paragraph names,level nos 01-49,77,66 , FD,SD                          
                                 will start here.
             12-72 - B-area
                                 All the COBOL instructions will start here.
             72-80 -  Remarks - has no relevance to the compiler.


PICTURE CLAUSE
         
PIC stands for PICTURE Clause. It is specified along with the data names/identifiers.

Example:

77   S-NAME       PIC A(30)  VALUE    SPACES.
77   S-AGE           PIC  9(2)     VALUE   0.
77   S-FEES          PIC   9(4)V99  VALUE    4567.54.

Here S-NAME,S-AGE,S-FEES are data names/identifiers. PIC indicates PICTURE clause.Along with the picture clause we can specify data type, width and initial value. The value clause indicates the initial value given to the identifier. Here, in the picture clause:

A - indicates the given data type is alphabetic- a-z and a blank values are allowed
X - indicates alphanumeric - all characters, numbers and special symbols including a blank are allowed
9 - indicates numeric values - only numbers 0-9 are allowed
V99 indicates decimal values


SPACE,SPACES, ZEROS, ZEROES,ZERO  are called as figurative constants.

META LANGUAGE


The syntax of the COBOL language is expressed using meta language.Meta language means, language about a language.

Capitals and Capital Underlined------- Reserved or Key words
Lower Case                             ------- User Defined entries
[  ]                                           ------- Optional entries
{ }                                           ------- Choice of entries
...                                             ------- prior entries will be repeated



INPUT and OUTPUT statements

ACCEPT and DISPLAY Statements are the input and output statements in COBOL.

Syntax:

ACCEPT  (LIN COL)  identifier-1...
DISPLAY  (LIN COL) identifier-1... or   message

Example 1.
Write a COBOL program to accept your name and age and display in the monitor.

SOLUTION-1.

                    IDENTIFICATION DIVISION.
                    PROGRAM-ID. EXAMPLE1.
                    ENVIRONMENT DIVISION.
                    DATA DIVISION.
                    WORKING-STORAGE SECTION.
                    77  S-NAME              PIC X(30) VALUE SPACES.
                    77 S-AGE                   PIC 9(2) VALUE 0.
                    PROCEDURE DIVISION.
                    PARA-1.
                                  DISPLAY "ENTER YOUR NAME".
                                  ACCEPT S-NAME.
                                  DISPLAY "ENTER YOUR AGE".
                                  ACCEPT S-AGE.
                     PARA-2.
                                  DISPLAY "MY NAME IS   : "   S-NAME.
                                  DISPLAY "MY AGE     IS   :"    S-AGE.
                                  STOP RUN.
                                 



SOLUTION-2. (with line and column number).

                    IDENTIFICATION DIVISION.
                    PROGRAM-ID. EXAMPLE1.
                    ENVIRONMENT DIVISION.
                    DATA DIVISION.
                    WORKING-STORAGE SECTION.
                    77  S-NAME              PIC X(30) VALUE SPACES.
                    77 S-AGE                   PIC 9(2) VALUE 0.
                    PROCEDURE DIVISION.
                    PARA-1.
                                  DISPLAY (5,  20)  "ENTER YOUR NAME".
                                  ACCEPT S-NAME.
                                  DISPLAY (7,  20)"ENTER YOUR AGE".
                                  ACCEPT S-AGE.
                     PARA-2.
                                  DISPLAY (9,  20)    "MY NAME IS   : "   S-NAME.
                                  DISPLAY (10,  20)  "MY AGE     IS   :"    S-AGE.
                                  STOP RUN.
                               
To enter the program

c>NE EXAMPL1.COB

To compile the program 

c>COBOL EXAMPL1.COB;
---Compile the program till NO Errors or Warnings

To execute the program(to see the output)

c>RUNCOB EXAMPL1


Arithmetic statements in COBOL
Operators
  • Arithmetic Operators
+    plus    sum

-     minus  difference
*     multiply product
/      divide   quotient
  • Relational Operators
> greater than
< less than
>= greater than or equal to
<= less than or equal to
= equal to
not =      not equal to
  • Arithmetic verbs
ADD,   SUBTRACT,   MULTIPLY,   DIVIDE,   COMPUTE are arithmetic verbs in COBOL.

Example:
                     ADD A B GIVING C.                           COMPUTE  C = A + B.
                     ADD A TO B.                                       COMPUTE   B = A + B.

                    SUBTRACT A FROM B.                      COMPUTE  B = B - A.
                    SUBTRACT A FROM B GIVING C.   COMPUTE  C = B - A.

                    MULTIPLY A BY B.                             COMPUTE B = A * B.
                    MULTIPLY A BY B GIVING C.           COMPUTE C = A* B.

                     DIVIDE A BY B.                                                    COMPUTE B = A / B.
                     DIVIDE A BY B GIVING C.                                  COMPUTE C = A / B.
                     DIVIDE A BY B GIVING C REMAINDER D.       COMPUTE  C = A / B.



Syntax:
ADD numeric value TO
    numeric value
    GIVING variable [ROUNDED]
    [ ON SIZE ERROR
      do something ]
ADD numeric value TO
    numeric variable [ROUNDED]
    [ ON SIZE ERROR
    do something ]

Examples:
ADD 17.5 TO THE-VALUE
    GIVING THE-SUM ROUNDED
    ON SIZE ERROR
    DISPLAY "Add - overflow"

ADD 1.17 TO THE-VALUE ROUNDED
    ON SIZE ERROR
    DISPLAY "Add - overflow"

Syntax:
COMPUTE numeric variable
    [ROUNDED] =
    arithmetic expression
    [ ON SIZE ERROR
      do something else ]
Examples:
COMPUTE THE-RESULT ROUNDED =
  (BASE-VALUE * 10) +
  (A-VALUE / 50)
  ON SIZE ERROR

  DISPLAY "Warning Size error."

Similarly for SUBTRACT,MULTIPLY and DIVIDE statements.

  1. Write a COBOL program to illustrate arithmetic statements.
Solution.
                     IDENTIFICATION DIVISION.
                     PROGRAM-ID. ARITHMETICS.
                     ENVIRONMENT DIVISION.
                     CONFIGURATION SECTION.
                     SOURCE-COMPUTER. IBM-PC.
                     OBJECT-COMPUTER. IBM-PC.
                     DATA DIVISION.
                     WORKING-STORAGE SECTION.
                      77  A PIC 9(2) VALUE 0.
                      77  B  PIC 9(2) VALUE 0.
                      77  C PIC 9(4) VALUE 0.
                      77  D PIC 9(2) VALUE 0.
                      PROCEDURE DIVISION.
                      PARA-INPUT.
                                                    ACCEPT A.
                                                    ACCEPT B.
                      PARA-ADD.
                                                   ADD A B GIVING C.
                                                   DISPLAY " SUM IS : ",   C.
                      PARA-SUBTRACT.
                                                  SUBTRACT  A FROM B GIVING C.
                                                  DISPLAY "DIFFERENCE IS : ",    C.
                      PARA-MULTIPLY.
                                                  MULTIPLY A BY B GIVING C.
                                                  DISPLAY "PRODUCT IS : ",   C.
                      PARA-DIVIDE.
                                                DIVIDE A BY B GIVING C REMAINDER D.
                                                DISPLAY " QUOTIENT IS :",  C.
                                                DISPLAY " REMAINDER IS : ",  D.
                                                STOP RUN.

Solution(with editing characters)

                     IDENTIFICATION DIVISION.
                     PROGRAM-ID. ARITHMETICS.
                     ENVIRONMENT DIVISION.
                     CONFIGURATION SECTION.
                     SOURCE-COMPUTER. IBM-PC.
                     OBJECT-COMPUTER. IBM-PC.
                     DATA DIVISION.
                     WORKING-STORAGE SECTION.
                      77  A PIC 9(2) VALUE 0.
                      77  B  PIC 9(2) VALUE 0.
                      77  C PIC  z(4)V99.
                      77  D PIC z(2).
                      PROCEDURE DIVISION.
                      PARA-INPUT.
                                                    ACCEPT A.
                                                    ACCEPT B.
                      PARA-ADD.
                                                   ADD A B GIVING C.
                                                   DISPLAY " SUM IS : ",   C.
                      PARA-SUBTRACT.
                                                  SUBTRACT  A FROM B GIVING C.
                                                  DISPLAY "DIFFERENCE IS : ",    C.
                      PARA-MULTIPLY.
                                                  MULTIPLY A BY B GIVING C.
                                                  DISPLAY "PRODUCT IS : ",   C.
                      PARA-DIVIDE.
                                                DIVIDE A BY B GIVING C REMAINDER D.
                                                DISPLAY " QUOTIENT IS :",  C.
                                                DISPLAY " REMAINDER IS : ",  D.
                                                STOP RUN.


IF Statement
Syntax:

1.            IF condition-1
                                 imperative statement-1......

2.            IF condition-1
                             statement-1.....
                      ELSE
                            statement-2......
3.           IF condition-1
                           IF condition-2
                                        statement-2
                             ELSE  statement-3
              ELSE
                           IF condition-3
                                        statement-4
                             ELSE  statement-5.

The syntax1 is simple IF condition,syntax2 is IF else and the syntax3 is nested if.Conditions can be either simple or complex condition.Complex condition generally contains Logical operators like AND OR NOT.
 
 
 
The Relational operator specifies the type of comparison to be made as shown in 
      below table.


      
Relational operator Can be written as Meaning of operator
IS GREATER THAN IS > Greater than
IS NOT GREATER THAN IS NOT > Not greater than
IS LESS THAN IS < Less than
IS NOT LESS THAN IS NOT < Not less than
IS EQUAL TO IS = Equal to
IS NOT EQUAL TO IS NOT = Not equal to
IS GREATER THAN OR EQUAL TO IS >= Is greater than or equal to
IS LESS THAN OR EQUAL TO IS <= Is less than or equal to

MOVE statement
syntax:
MOVE   identifier/literal TO identifier-1...




1. Write a COBOL Program to illustrate a simple Calculator.

IDENTIFICATION DIVISION.
PROGRAM-ID.  Iteration-If.
AUTHOR.  VVNK.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  Num1           PIC 9  VALUE ZEROS.
01  Num2           PIC 9  VALUE ZEROS.
01  Result         PIC 99 VALUE ZEROS.
01  Operator       PIC X  VALUE SPACE.

PROCEDURE DIVISION.
Calculator.
   
       DISPLAY "Enter First Number      : ".
       ACCEPT Num1.
       DISPLAY "Enter Second Number     : ".
       ACCEPT Num2.
       DISPLAY "Enter operator (+ or - or *) : ".
       ACCEPT Operator
       IF Operator = "+"
          ADD Num1, Num2 GIVING Result.
       IF Operator = "-"
          SUBTRACT Num1 FROM Num2 GIVING Result.
       IF Operator = "*"
MULTIPLY Num1 BY Num2 GIVING Result. DISPLAY "Result is = ", Result. STOP RUN.




 CONDITION NAMES
condition-name condition.   (important)  
 condition-name is a identifier defined with level number 88.  It has only VALUE 
 clause, it does not contain any picture clause, but it must always be associated 
 to a data name called the conditional variable. 


      Syntax

       88  condition-name   literal-1 [  literal-2 ] 
                          [, literal-3 [  literal-4] ... ]



      Example -

       01 WS-AGE    PIC   99.    <---  conditional variable
          88  INFANT  VALUE 0.
          88  BABY    VALUE 1,2.
          88  CHILD   VALUE 3 THRU 12.
          88  TEEN-AGER VALUE 13 THRU 19.

      In the above example  we have defined 4 condition-name's associated with one data
      name WS-NAME. if WS-AGE contains value 0 (ZERO).  INFANT will become true , since 
      we have defined this condition-name with value 0.  If WS-AGE contains values 1 OR 
      2, Second condition name BABY will become true,since we have defined BABY 
      condition-name with value 1 and 2.  Same rule applied to other condition-names.


      How we can use this in PROCEDURE DIVISION?
      See below example... IF INFANT is true, statement(s) after IF statement will be executed.
      i.e., WS-AGE contains value 0 (ZER0), then INFANT will become true.


      IF INFANT
         MOVE 20  TO X.
 
 1.Write a COBOL program using conditional Verbs
 
IDENTIFICATION DIVISION.
PROGRAM-ID.  Conditions.
AUTHOR.  VVNK.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  Char               PIC X.
    88 Vowel           VALUE "a", "e", "i", "o", "u".
    88 Consonant       VALUE "b", "c", "d", "f", "g", "h"
                             "j" THRU "n", "p" THRU "t", "v" THRU "z".
    88 Digit           VALUE "0" THRU "9".
    88 ValidCharacter  VALUE "a" THRU "z", "0" THRU "9".

PROCEDURE DIVISION.
Para-1.
    DISPLAY "Enter lower case character or digit. No data ends.".
    ACCEPT Char.
    IF Vowel DISPLAY "The letter " Char " is a vowel."
    IF Consonant DISPLAY "The letter " Char " is a consonant."
    IF Digit DISPLAY Char " is a digit."
    IF OTHER DISPLAY "problems found".
    STOP RUN.

GO TO statement :

GO TO para name
Eg:
GO TO para-1.
After the execution of this statement the control will jump the para mentioned along with the GO TO statement but will not return back.

This is the disadvantage with GO TO statement.

PERFORM statements:

The following are the different forms of perform statements:

  1. PERFORM paraname.
  2. PERFORM paraname-1 THRU paraname-n.
  3. PERFORM paraname integer TIMES.
  4. PERFORM paraname-1 THRU paraname-n integer TIMES.
  5. PERFORM paraname VARYING index FROM integer-1 BY integer-2 UNTIL condition.
  6. PERFORM paraname VARYING index-1 FROM integer-1 BY integer-2  AFTER index-2 FROM integer-3 BY integer-4 UNTIL condition.

eX:

Write a program to display multiplication table of a given number using different types of perform statements.
METHOD 1:

IDENTIFICATION DIVISION.
PROGRAM-ID. MULT-TABLE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 I PIC 9(2) VALUE 0.
77 PROD PIC 9(2) VALUE 0.
77 NUM PIC 9(2) VALUE 0.
PROCEDURE DIVISION.
PARA-MAIN.
                ACCEPT NUM.
                PERFORM PARA-MUL 10 TIMES.
                STOP RUN.

PARA-MUL.
                    COMPUTE I = I + 1.
                    COMPUTE PROD =  NUM * I.
                    DISPLAY NUM "*"  I "=" PROD.

METHOD 2:




IDENTIFICATION DIVISION.
PROGRAM-ID. MULT-TABLE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 I PIC 9(2) VALUE 0.
77 PROD PIC 9(2) VALUE 0.
77 NUM PIC 9(2) VALUE 0.
PROCEDURE DIVISION.
PARA-MAIN.
                ACCEPT NUM.
                PERFORM PARA-MUL VARYING  I FROM 1 BY 1 UNTIL I > 10.
                STOP RUN.

PARA-MUL.
                    COMPUTE PROD =  NUM * I.
                    DISPLAY NUM "*"  I "=" PROD.

METHOD 3:


IDENTIFICATION DIVISION.
PROGRAM-ID. MULT-TABLE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 I PIC 9(2) VALUE 1.
77 PROD PIC 9(2) VALUE 0.
77 NUM PIC 9(2) VALUE 0.
PROCEDURE DIVISION.
PARA-MAIN.
                ACCEPT NUM.
                PERFORM PARA-MUL 1 UNTIL I > 10.
                STOP RUN.

PARA-MUL.
                    COMPUTE PROD =  NUM * I.
                    DISPLAY NUM "*"  I "=" PROD.
                    COMPUTE I = I + 1.


OCCURS clause:
Occurs clause is used to store table entries in the data division.Whenever you refer an entry defined by occurs clause, it should be referred with a subscript.

Ex:

01 Marks-table.
                  02 s-marks pic 9(3) occurs 5 times.
In the above case we can store the marks of 5 subjects with one single data entry using occurs clause.
If we want to refer 3rd subject marks we should refer as s-marks(3) in procedure division.

EX:

Write a program to store 5 salesman sale value in a table and find its grand total.

IDENTIFICATION DIVISION.
PROGRAM-ID. SALE-VALUE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 I PIC 9(2) VALUE 0.
01 SALES-TABLE.
              02 SALES-VALUE OCCURS 5 TIMES PIC 9(3).
77 G-TOT    PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
PARA-MAIN.
                       PERFORM ACC-PARA VARYING I FROM 1 BY 1 UNTIL I > 5.
                       PERFORM TOT-PARA VARYING I FROM 1 BY 1 UNTIL I > 5.
                       DISPLAY "TOTAL SALE VALUE = "  G-TOT.
                       STOP RUN.
ACC-PARA.
                       MOVE 0 TO SALES-VALUE( I ).
                       DISPLAY " ENTER  "  I  "   SALE VALUE".
                       ACCEPT SALES-VALUE( I ).
TOT-PARA.
                     
                      COMPUTE G-TOT = G-TOT + SALES-VALUE ( I ).


2 DIMENSIONAL TABLE

ex:
To store marks of 60 students each having 5 subjects we use occurs clause as given below:

01 student-marks-table.
                   02 students occurs 60 times.
                           03 marks occurs 5 times pic 9(3).


ex:
Write a program to store sales value of 12 months for 5 salesman and store the total sale value of each salesman.Also display the grand total sale value of all salesman.


IDENTIFICATION DIVISION.
PROGRAM-ID. MULT-TABLE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SALES-TABLE.
                02 SALESMAN OCCURS 5 TIMES.
                       03 SALE-VALUE OCCURS 12 TIMES PIC 9(3).
01 SALE-TOT-TABLE.
           02 SALE-TOT OCCURS 5 TIMES PIC 9(5).
77 G-TOT   PIC 9(5) VALUE 0.
77 I PIC 9(2) VALUE 0.
77 J PIC 9(2) VALUE 0.

PROCEDURE DIVISION.
PARA-MAIN.
                     PERFORM ACC-SALE-VALUE VARYING I FROM 1 BY 1 UNTIL I > 5 AFTER
                     J FROM 1 BY 1 UNTIL J > 12.
                     PERFORM SALES-TOTAL VARYING I FROM 1 BY 1 UNTIL I > 5 AFTER
                     J FROM 1 BY 1 UNTIL J > 12.
                     PERFORM G-TOT-PARA VARYING I FROM 1 BY 1 UNTIL I > 5.
                     DISPLAY " GRAND TOTAL = " G-TOT.
                     STOP RUN.
ACC-SALE-VALUE.
                     MOVE 0 TO SALE-VALUE ( I  J).
                     MOVE  0 TO SALE-TOT( I ).
                     ACCEPT SALE-VALUE( I  J ).
SALES-TOTAL.
                   COMPUTE SALE-TOT ( I ) = SALE-VALUE( I ) + SALE-TOT( I ).
G-TOT-PARA.
                    DISPLAY SALE-TOT( I ).
                   COMPUTE G-TOT = G-TOT + SALE-TOT( I ).

                                                                                                                                                                                                                                                                                                                                                      INDEXED TABLES:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      example:

01 MARKS-TABLE.
               02 S-MARKS PIC 9(3) OCCURS 5 TIMES  INDEXED BY I.

To manipulate the index I we have to use SET Verb.

To initialize I
SET I TO 0.
It initializes value to 0.

To increase the value of I
SET I UP BY 1.
To decrease the value of I
SET I DOWN BY 1.

Note: I need not be separately declared in working storage section.


SERACH Verb:
The SEARCH verb is used to locate elements in 1-dimensional table.

SEARCH identifier-1
                          AT END imperative statement-1
WHEN condition-1  imperative statement.

Example:

Write a program to store ACC NO,ACC NAME and Balance in a table and display the balance for a given account number.

IDENTIFICATION DIVISION.
PROGRAM-ID. BANK-ACC.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 W-ACC-NO    PIC 9(3) VALUE 0.
01 SB-ACC-DETAILS.
               02 ACCOUNT OCCURS 5 TIMES INDEXED BY I.
                      03 ACC-NUM   PIC 9(3).
                      03 ACC-NAME  PIC X(15).
                      03  BALANCE PIC 9(5).
PROCEDURE DIVISION.
PARA-MAIN.
                      PERFORM ACC-PARA 5 TIMES.
                      PERFORM SERACH-PARA.
                      STOP RUN.
ACC-PARA.
                      MOVE 0 TO ACC-NUM(I).
                      MOVE SPACES TO ACC-NAME(I).
                      MOVE  0 TO BALANCE(I).
                      ACCEPT ACC-NUM(I).
                      ACCEPT ACC-NAME(I).
                      ACCEPT  BALANCE(I).
SEARCH-PARA.
                      DISPLAY "ENTER ACCOUNT NUMBER TO SEARCH".
                      ACCEPT W-ACC-NO.
                      SEARCH ACCOUNT
                                      AT END DISPLAY " ACCOUNT NOT FOUND"
                      WHEN W-ACC-NO = ACC-NAME(I)
                                      DISPLAY   ACC-NUM(I),
                                                         ACC-NAME(I),
                                                          BALANCE(I).


Binary Search:

For binary search,  search all statement is used.


SEARCH  ALL identifier-1
                          AT END imperative statement-1
WHEN condition-1  imperative statement.

We can also declare key elements in an indexed table using ASCENDING KEY / DESCENDING KEY phrase.

EX:

  Write a program to store ACC NO,ACC NAME and Balance in a table and display the balance for a given account number.Take ACCNO as primary key.Use binary search.

IDENTIFICATION DIVISION.
PROGRAM-ID. BANK-ACC.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 W-ACC-NO    PIC 9(3) VALUE 0.
01 SB-ACC-DETAILS.
               02 ACCOUNT OCCURS 5 TIMES
                         ASCENDING KEY IS ACC-NUM INDEXED BY I.
                      03 ACC-NUM   PIC 9(3).
                      03 ACC-NAME  PIC X(15).
                      03  BALANCE PIC 9(5).
PROCEDURE DIVISION.
PARA-MAIN.
                      PERFORM ACC-PARA 5 TIMES.
                      PERFORM SERACH-PARA.
                      STOP RUN.
ACC-PARA.
                      MOVE 0 TO ACC-NUM(I).
                      MOVE SPACES TO ACC-NAME(I).
                      MOVE  0 TO BALANCE(I).
                      ACCEPT ACC-NUM(I).
                      ACCEPT ACC-NAME(I).
                      ACCEPT  BALANCE(I).
SEARCH-PARA.
                      DISPLAY "ENTER ACCOUNT NUMBER TO SEARCH".
                      ACCEPT W-ACC-NO.
                      SEARCH  ALL ACCOUNT
                                      AT END DISPLAY " ACCOUNT NOT FOUND"
                      WHEN W-ACC-NO = ACC-NAME(I)
                                      DISPLAY   ACC-NUM(I),
                                                         ACC-NAME(I),
                                                          BALANCE(I).


FILES :
A file is a collection of logically related records.A record is a collection of logically related fields.
Fields contain data within them.Data is stored in the form of bits and bytes.

There are 3 types of files in COBOL.
  1. SEQUENTIAL
  2. INDEXED
  3. RELATIVE
There are 3 modes of of accessing the files:
  1. SEQUENTIAL
  2. RANDOM
  3. DYNAMIC
In sequential files records are stored in the manner in which there are entered or saved.Hence they can be accessed sequentially only.To write a record or read a record we have to access one after the other i,e., sequentially.Records can be added only at the end of the file and not anywhere in the middle.


In Indexed files records are stored based on a key field which are part of the the record also called as Index.
An index file is conceptually made up of two files i.e., an index file on which the file is accessed and a data file.The index file contains all the key fields.In case of Indexed files records can be accessed sequentially, or randomly or dynamically.


In Relative file the record is identified based on a relative record number.The records are accessed i.e., stored and retrieved based on the relative record number.In case of relative files records can be accessed sequentially, or randomly or dynamically.


While creating any  file generally we write the records in a sequential manner.

While creating the file the file should be opened in OUTPUT mode.In order to retrieve the records in the file it should be opened in INPUT mode in case of sequential,Index and Relative files.Indexed and Relative files can be accessed that is , retrieved by opening them in I-O mode(that is for updation purpose).In order to add records in a sequential file we can add only at the end of the file hence while adding records it should be opened in EXTEND mode.

Hence

ORGANIZATION IS  SEQUENTIAL
                                    INDEXED
                                    RELATIVE

ACCESS IS                SEQUENTIAL
                                    RANDOM
                                    DYNAMIC


OPEN mode file- name.

OPEN OUTPUT filename  -  TO CREATE A FILE
            INPUT                  -   TO READ A FILE
            I-O                        -   TO UPDATE INDEXED FILES
            EXTEND              -   TO ADD RECORDS AT THE END OF A SEQUENTIAL FILE.




SEQUENTIAL FILE CREATION:

EX:
Write a COBOL program to create a sequential file which contains the following fields.
sno,name,course,marks and dob.

IDENTIFICATION DIVISION.
PROGRAM-ID, CREA-SEQ.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
                        SELECT S-FILE ASSIGN TO DISK
                        ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD S-FILE
                        LABEL RECORDS ARE STANDARD
                        VALUE OF FILE-ID "STUDENT.DAT".
01 STU-REC.
                        02 SNO   PIC 9(2).
                        02 SNAME PIX X(15).
                        02 SCOURSE PIC X(5).
                        02 SDOB  PIC XX/XX/XX.
                        02 SMARKS PIC 9(3).
WORKING-STORAGE SECTION.
77 S-FLAG   PIC X VALUE SPACE.
PROCEDURE DIVISION.
PARA-MAIN.
                      OPEN OUTPUT S-FILE.
                      PERFORM ADD-RECORDS UNTIL S-FLAG = "Y".
                      CLOSE S-FILE.
                      STOP RUN.
ADD-RECORDS.
                       DISPLAY (5, 10) "ENTER STUDENT SNO :".
                       ACCEPT (5, 35) SNO.

                        DISPLAY (6, 10) "ENTER STUDENT NAME :".
                       ACCEPT (6, 35) SNAME.

                       DISPLAY (7, 10) "ENTER STUDENT COURSE :".
                       ACCEPT (7, 35) SCOURSE.

                      DISPLAY (8, 10) "ENTER STUDENT DOB :".
                      ACCEPT (8, 35) DOB.

                       DISPLAY (9, 10) "ENTER STUDENT MARKS :".
                       ACCEPT (9, 35) SMARKS.

                       WRITE STU-REC.

                       DISPLAY (10, 10)  "DO YOU WANT TO ADD MORE RECORDS ?(Y/N)".
                       ACCEPT (10, 55) S-FLAG.
                   


Ex: Write e program to read the above file and display its contents.


IDENTIFICATION DIVISION.
PROGRAM-ID, CREA-SEQ.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
                        SELECT S-FILE ASSIGN TO DISK
                        ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD S-FILE
                        LABEL RECORDS ARE STANDARD
                        VALUE OF FILE-ID "STUDENT.DAT".
01 STU-REC.
                        02 SNO   PIC 9(2).
                        02 SNAME PIX X(15).
                        02 SCOURSE PIC X(5).
                        02 SDOB  PIC XX/XX/XX.
                        02 SMARKS PIC 9(3).
WORKING-STORAGE SECTION.
77 EOF-FLAG   PIC X VALUE SPACE.
PROCEDURE DIVISION.
PARA-MAIN.
                      OPEN INPUT S-FILE.
                      READ S-FILE AT END MOVE "Y" TO EOF-FLAG.
                      PERFORM READ-RECORDS UNTIL EOF-FLAG = "Y".
                      CLOSE S-FILE.
                      STOP RUN.
READ-RECORDS.
                       DISPLAY (5, 10)  SNO.
                       DISPLAY (6, 10)  SNAME.
                       DISPLAY (7, 10)  SCOURSE.
                       DISPLAY (8, 10)  DOB.
                       DISPLAY (9, 10)  SMARKS.
                       READ S-FILE AT END MOVE "Y" TO EOF-FLAG.
                   
eX:
Write a program to enter 5 subjects marks of a student in a file.

IDENTIFICATION DIVISION.
PROGRAM-ID. MARKS-FILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
                           SELECT S-FILE ASSIGN TO DISK
                           ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD S-FILE
                             LABEL RECORDS ARE STANDARD
                             VALUE OF FILE-ID "MARKS.DAT".
01 MARKS-REC.
                           02 HTNO PIC 9(5).
                           02 SNAME  PIC X(20).
                           02 COURSE PIC X(5).
                           02 S-MARKS  OCCURS 5 TIMES.
                                       03 SUBJECT-NAME PIC X(10).
                                       03 SUBJECT-MARKS PIC 9(3).
WORKING-STORAGE SECTION.
77 EOF-FLAG  PIC X VALUE SPACE.
77 I PIC 9 VALUE 0.
PROCEDURE DIVISION.
PARA-MAIN.
                       OPEN OUTPUT S-FILE.
                       PERFORM STORE-MARKS UNTIL EOF-FLAG ="N".
                       CLOSE S-FILE.
                       STOP RUN.
STORE-MARKS.
                           ACCEPT HTNO.
                           ACCEPT SNAME.
                           ACCEPT COURSE.
                          PERFORM ADD-MARKS VARYING I FROM 1 BY 1 UNTIL I > 5.
                          WRITE MARKS-REC.
                          DISPLAY "DO YOU WANT TO CONTINUE(Y/N)?".
                          ACCEPT EOF-FLAG.
ADD-MARKS.
                        ACCEPT SUBJECT-NAME(I).
                        ACCEPT SUBJECT-MARKS(I).
                     

                   
READING A SEQUENTIAL FILE:

EX:
Write a program to read the above file and display its contents.


IDENTIFICATION DIVISION.
PROGRAM-ID. MARKS-FILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
                           SELECT S-FILE ASSIGN TO DISK
                           ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD S-FILE
                             LABEL RECORDS ARE STANDARD
                             VALUE OF FILE-ID "MARKS.DAT".
01 MARKS-REC.
                           02 HTNO PIC 9(5).
                           02 SNAME  PIC X(20).
                           02 COURSE PIC X(5).
                           02 S-MARKS  OCCURS 5 TIMES.
                                       03 SUBJECT-NAME PIC X(10).
                                       03 SUBJECT-MARKS PIC 9(3).
WORKING-STORAGE SECTION.
77 EOF-FLAG  PIC X VALUE SPACE.
77 I PIC 9 VALUE 0.
PROCEDURE DIVISION.
PARA-MAIN.
                       OPEN INPUT S-FILE.
                       READ S-FILE AT END MOVE "Y" TO EOF-FLAG.
                       PERFORM READ-MARKS UNTIL EOF-FLAG ="Y".
                       CLOSE S-FILE
                        STOP RUN.
READ-MARKS.
                          DISPLAY "HALL TICKET NO :",   HTNO.
                          DISPLAY "STUDENT NAME ::,   SNAME.
                          DISPLAY "STUDENT COURSE :"'  COURSE.
                          PERFORM DISP-MARKS VARYING I FROM 1 BY 1 UNTIL I > 5.
                          READ S-FILE AT END MOVE "Y" TO EOF-FLAG.
DISP-MARKS.
                        DISPLAY  SUBJECT-NAME( I )  ":"   SUBJECT-MARKS( I ).
                       

eX:
Write a program to read the above file and store HTNO, NAME, TOTMARKS and grade in another file.



IDENTIFICATION DIVISION.
PROGRAM-ID. MARKS-FILE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
                           SELECT S-FILE ASSIGN TO DISK
                           ORGANIZATION IS LINE SEQUENTIAL.
                           SELECT M-FILE ASSIGN TO DISK
                          ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD S-FILE
                             LABEL RECORDS ARE STANDARD
                             VALUE OF FILE-ID "MARKS.DAT".
01 MARKS-REC.
                           02 HTNO PIC 9(5).
                           02 SNAME  PIC X(20).
                           02 COURSE PIC X(5).
                           02 S-MARKS  OCCURS 5 TIMES.
                                       03 SUBJECT-NAME PIC X(10).
                                       03 SUBJECT-MARKS PIC 9(3).

FD M-FILE
                             LABEL RECORDS ARE STANDARD
                             VALUE OF FILE-ID "GRADE.DAT".
01 MARKS-REC.
                           02 MHTNO PIC 9(5).
                           02 MSNAME  PIC X(20).
                           02 MCOURSE PIC X(5).
                           02 MTOTAL PIC 9(3).
                           02 MAVG    PIC 9(3)V99.
                           02 MGRADE PIC X(8).

WORKING-STORAGE SECTION.
77 TOT  PIC 9(3) VALUE 0.
77 AVG PIC 9(3)V99 VALUE 0.
77 GRADE PIC X(8) VALUE SPACES.
77 EOF-FLAG  PIC X VALUE SPACE.
77 I PIC 9 VALUE 0.
PROCEDURE DIVISION.
PARA-MAIN.
                       OPEN INPUT S-FILE  OUTPUT M-FILE.
                       READ S-FILE AT END MOVE "Y" TO EOF-FLAG.
                       PERFORM READ-MARKS UNTIL EOF-FLAG ="Y".
                       CLOSE S-FILE M-FILE
                        STOP RUN.
READ-MARKS.

                          DISPLAY "HALL TICKET NO :",   HTNO.
                          DISPLAY "STUDENT NAME ::,   SNAME.
                          DISPLAY "STUDENT COURSE :"'  COURSE.
                          PERFORM DISP-MARKS VARYING I FROM 1 BY 1 UNTIL I > 5.
                          PERFORM WRITE-PARA.                          
                          READ S-FILE AT END MOVE "Y" TO EOF-FLAG.
DISP-MARKS.
                        DISPLAY  SUBJECT-NAME( I )  ":"   SUBJECT-MARKS( I ).
                        COMPUTE TOT = TOT + SUBJECT-MARKS(I).
                     
WRITE-PARA.
                        COMPUTE AVG = TOT / 5.

                         IF AVG > 70 MOVE "DISTINCTION" TO GRADE
                         ELSE IF AVG > 60 AND AVG <70 MOVE "FIRST" TO GRADE
                         ELSE IF AVG >50 AND AVG < 60 MOVE "SECOND" TO GRADE
                         ELSE IF AVG >40 AND AVG < 50 MOVE "PASS" TO GRADE
                         ELSE MOVE "FAIL" TO GRADE.

                        MOVE HTNO TO MHTNO.
                        MOVE SNAME TO MSNAME.
                        MOVE COURSE TO MCOURSE.
                        MOVE TOT TO MTOTAL.
                        MOVE AVG TO MAVG.
                        MOVE GRADE TO MGRADE.
                       WRITE MARKS-REC.

ex:
Write a COBOL program to add record  at the end of a sequential file which contains the following fields.
sno,name,course,marks and dob.

IDENTIFICATION DIVISION.
PROGRAM-ID, CREA-SEQ.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
                        SELECT S-FILE ASSIGN TO DISK
                        ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD S-FILE
                        LABEL RECORDS ARE STANDARD
                        VALUE OF FILE-ID "STUDENT.DAT".
01 STU-REC.
                        02 SNO   PIC 9(2).
                        02 SNAME PIX X(15).
                        02 SCOURSE PIC X(5).
                        02 SDOB  PIC XX/XX/XX.
                        02 SMARKS PIC 9(3).
WORKING-STORAGE SECTION.
77 S-FLAG   PIC X VALUE SPACE.
PROCEDURE DIVISION.
PARA-MAIN.
                      OPEN EXTEND S-FILE.
                      PERFORM ADD-RECORDS UNTIL S-FLAG = "Y".
                      CLOSE S-FILE.
                      STOP RUN.
ADD-RECORDS.
                       DISPLAY (5, 10) "ENTER STUDENT SNO :".
                       ACCEPT (5, 35) SNO.

                        DISPLAY (6, 10) "ENTER STUDENT NAME :".
                       ACCEPT (6, 35) SNAME.

                       DISPLAY (7, 10) "ENTER STUDENT COURSE :".
                       ACCEPT (7, 35) SCOURSE.

                      DISPLAY (8, 10) "ENTER STUDENT DOB :".
                      ACCEPT (8, 35) DOB.

                       DISPLAY (9, 10) "ENTER STUDENT MARKS :".
                       ACCEPT (9, 35) SMARKS.

                       WRITE STU-REC.

                       DISPLAY (10, 10)  "DO YOU WANT TO ADD MORE RECORDS ?(Y/N)".
                       ACCEPT (10, 55) S-FLAG.


Indexed Files:
Indexed files are accessed with a key field. The values in the key field should be unique and not null.The key field is a field within the file.

While executing the Index file programs, first we should run ISAM.exe file and then execute the programs.This is very important, since ISAM will enable us to create a key file with an extension filename.KEY which stores all the key fields on which the data file is accessed.


Ex. Write a program to create an INDEXED sequential file.

IDENTIFICATION DIVISION.
PROGRAM-ID. CREATEFILE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
                 SELECT EMP-FILE ASSIGN TO DISK
                 ORGANIZATION IS INDEXED
                 ACCESS IS SEQUENTIAL
                 RECORD KEY IS EMP-NO.
DATA DIVISION.
FILE SECTION.
FD EMP-FILE
                 LABEL RECORDS ARE STANDARD
                 VALUE OF FILE-ID "EMP.DAT".
01 EMP-REC.
                02 EMP-NO PIC 9(3).
                02 EMP-NAME PIC X(20).
                02 SALARY PIC 9(5).
PROCEDURE DIVISION.
WORKING-STORAGE SECTION.
77 EOF-FLAG PIC X.
PARA-M.
              OPEN OUTPUT EMP-FILE.
              PERFORM ADD-RECORDS UNTIL EOF-FLAG = "N".
             CLOSE EMP-FILE.
              STOP RUN.
ADD-RECORDS.
               ACCEPT EMP-NO.
               ACCEPT EMP-NAME.
              ACCEPT SALARY.
              WRITE EMP-REC INVALID KEY DISPLAY "ERROR IN CREATION".
              DISPLAY "DO U WANT TO CONTINUE?(Y/N)".
              ACCEPT EOF-FLAG.

Ex.
Write a program to read the above file.

IDENTIFICATION DIVISION.
PROGRAM-ID. READFILE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
                 SELECT EMP-FILE ASSIGN TO DISK
                 ORGANIZATION IS INDEXED
                 ACCESS IS RANDOM
                 RECORD KEY IS EMP-NO.
DATA DIVISION.
FILE SECTION.
FD EMP-FILE
                 LABEL RECORDS ARE STANDARD
                 VALUE OF FILE-ID "EMP.DAT".
01 EMP-REC.
                02 EMP-NO PIC 9(3).
                02 EMP-NAME PIC X(20).
                02 SALARY PIC 9(5).
PROCEDURE DIVISION.
WORKING-STORAGE SECTION.
77 EOF-FLAG PIC X.
77 W-EMP-NO PIC 9(3) VALUE 0.
PARA-M.
              OPEN INPUT EMP-FILE.
              PERFORM READ-RECORDS UNTIL EOF-FLAG = "N" OR W-EMP-NO=999..
             CLOSE EMP-FILE.
              STOP RUN.
READ-RECORDS.
             DISPLAY "ENTER EMP NO TO READ  :".
             ACCEPT W-EMP-NO.
              MOVE W-EMP-NO TO EMP-NO.
             READ EMP-FILE INVALID KEY PERFORM ERROR-PARA.
             DISPLAY EMP-NO.
             DISPLAY EMP-NAME.
             DISPLAY SALARY.
             DISPLAY " DO U WANT TO READ MORE (Y/N)?.
             ACCEPT EOF-FLAG.
ERROR-PARA.
              DISPLAY "ERROR IN READING".

SCREEN SECTION.
Screen section is meant to format the screen.Screen section is a part of data division.

Ex.Write a program to create a sequential file using screen section.

IDENTIFICATION DIVISION.
PROGRAM-ID. SCREEN-CREATE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE SECTION.
                     SELECT OUT-FILE ASSIGN TO DISK
                     ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD OUT-FILE
                       LABEL RECORDS ARE STANDARD
                       VALUE OF FILE-ID "MYFILE.DAT".
01 OUT-REC.
                     05 SNAME PIC X(20).
                     05 AGE PIC 9(3).
SCREEN SECTION.
01 CLEAR-SCREEN.
                    05  BLANK SCREEN.
01 FORM-FORMAT.
                   05 LINE 1 COLUMN 1 PIC X(30) FROM ALL "_".
                   05 LINE 2 COLUMN 10 VALUE "FILL THE FORM" HIGHLIGHT.
                   05 LINE 3 COLUMN 1 PIC X(30) FROM ALL "_".
                   05 LINE 5 COLUMN 1 VALUE  "NAME :".
                   05 LINE 5 COLUMN 10 PIC X(20) TO SNAME.
                   05 LINE 6 COLUMN 1 VALUE "AGE :".
                   05 LINE 6 COLUMN 10 PIC 9(3) TO AGE.
                   05 LINE PLUS 2 PIC X(30) FROM ALL "_".
                   05 LINE PLUS 2 COLUMN 1 "PRESS ESCAPE TO EXIT" BLINK.
PROCEDURE DIVISION.
DISPLAY-FORM.
                  OPEN OUTPUT OUT-FILE.
                   DISPLAY CLEAR-SCREEN.
                  DISPLAY FORM-FORMAT.
FILL-THE-FORM.
                  ACCEPT FORM-FORMAT
                               ON ESCAPE GO TO FINISH-ENTRY.
                  WRITE OUT-REC.
                   GO TO FILL-THE-FORM.
FINISH-ENTRY.
                  CLOSE OUT-FILE.
                   STOP RUN.