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
Example 1 : 01 WS-NAME PICTURE IS A(20).
Example 2: 77 WS-POLICY-NO PIC X(8).
Example 3:
01 WS-DATE.Example 4:
05 WS-DATE-CC PIC 99.
05 WS-DATE-YY PIC 99.
05 WS-DATE-MM PIC 99.
05 WS-DATE-DD PIC 99.
01 PROGRAM-SWITCHES.Example 5:
05 SW-END-OF-FILE PIC X(01)
88 END-OF-FILE VALUE 'Y'.
88 NOT-END-OF-FILE VALUE 'N'.
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 TRUEorMOVE 'Y' TO SW-END-OF-FILE…IF END-OF-FILEThenstatement 1statement 2…
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