Sunday, December 1, 2013

COBOL Tutorial Class 2 : Constants and Variable


In any programing language the data falls into two categories called constants and variables. Constants are those which cannot be modified during the program execution. Variables are data that can be modified during the program execution. In the hello word program the phrase "Hello world" is a constant. As in constants there is nothing much to learn let us directly jump into variables.

In COBOL every variable to be used in the program must be declared first. Variable declaration is done in COBOL in Data Division under WORKING-STORAGE SECTION.
Below are the three types variable used in COBOL.
  • Alphabetic
  • Alphanumeric
  • Numeric
Variables are declared using PICTURE / PIC keyword. Variables can be grouped together to form a group level variable under which multiple sub variable can be defined. A level number is used to differentiate COBOL group level variables with sub variable.Below are sample examples of variable declarations.

Example 1 :  01 WS-NAME        PICTURE   IS   A(20).
Example 2:   77 WS-POLICY-NO                      PIC X(8).
Example 3:
01  WS-DATE.                                                    
    05  WS-DATE-CC                       PIC 99.  
    05  WS-DATE-YY                       PIC 99.             
    05  WS-DATE-MM                      PIC 99.   
    05  WS-DATE-DD                       PIC 99.             
Example 4:
01  PROGRAM-SWITCHES.                                  
    05 SW-END-OF-FILE                    PIC X(01)  
       88  END-OF-FILE                       VALUE 'Y'.    
       88  NOT-END-OF-FILE                VALUE 'N'.    
Example 5:
01 WS-AMOUNT                       PIC  S9(05)V99.
01 WS-AMOUNT-DISP               PIC  ZZ,ZZ9.99+.
Example 6:
01  FILE-REC.
    05 RECORD-IND                               PIC X(01).
    05 FILLER                                        PIC X(01).
    05 RECORD-DATA-1.
        10 STATE-CODE                           PIC X(02).
        10 FILLER                                     PIC X(76).
    05 RECORD-DATA-2 REDEFINES RECORD-DATA-2.
        10 COUNTY-NAME                        PIC X(10).
        10 FILLER                                     PIC X(68).

Let us talk about above examples one by one.

Example 1 : Here we declared a variable using PICTURE IS keyword. We can use PIC as well as shown in other variables. ‘01’ is level number and must be typed in area A.  It is a single variable so it should be either 01 or 77 level. PIC keyword is used to define the type and size of the variable. WS-NAME has been declared as a alphabetic variable ( character string ) with size 20. Alphabetic variable is declared using character ‘A’ with PIC keyword.  For alphanumeric and numeric we use ‘X’ and ‘9’.

Example 2 : In this example we used level number 77. Level number 77 is used for single elementary variable and not a group variable or structure.  It uses a same syntax of 01 and begins in area A.  Here WS-POLICY-NO is defined as alphanumeric meaning it can hold character 0-9 , A-Z or a-z or any special character.

Example 3 : This example shows how a group level variable or structure is declared. Level ‘05’ is used for second level or child level variable. The program can use the structure WS-DATE as a whole or can separately access each individual say WS-DATE-DD. In this example I also showed that we can use PIC 99 instead of PIC 9(02). Both are same.

Example 4 : Here it is shown how switches or indicators can be defined in COBOL. level ‘88’ is used for defining switch.  ‘SET’ statement is used to modify the value of the indictor. In other way moving ‘Y’ to SW-END-OF-FILE will set END-OF-FILE  to TRUE. See the code snippet below to get an idea how switches are to be used in program.
SET END-OF-FILE TO TRUE
or
MOVE 'Y' TO SW-END-OF-FILE
IF END-OF-FILE
Then
   statement 1
   statement 2
  
Example 5 : In this example WS-AMOUNT has been declared as signed numeric variable that can hold a floating point value. S is for signed. WS-AMOUNT can hold values ranging from  –99999.99 to +99999.99.WS-AMOUNT-DISP has been declared as edited variable. These types of variables are used for displaying to output or writing to a file in defined format for increasing the readability.  ‘Z’ is used for suppressing zeroes.  I will discuss more about edited variable later in details.

Example 6 : Here showed an example how REDEFINES keyword is used to define separate variables that will utilize same memory location. Say we are creating an output file ( record length 80 byte) where we will have first byte as an record indicator ( RECORD-IND ) which will be either ‘S’ or ‘C’ ( ‘S’ is for state , ‘C’ is for county).  Next there will be either state code or county name.  STATE-CODE under RECORD-DATA-1 will be used when we want to store a state code and then write to the file.  COUNTY-NAME under RECORD-DATA-2 will be used when we want store county name. A common memory in RAM will be allocated for RECORD-DATA-1 and RECORD-DATA-2.

No comments:

Post a Comment