Previously we had an overview of the COBOL language concept, this time we continue to go through the most basic data types and syntax to build a COBOL program.
1. The data type of COBOL
COBOL actually has only 2 data types: NUMBER and STRING .
The variables in COBOL are created based on these two types, often declared as Elementary Items and Group Items.
You can refer to the example variable declaration below:
1 2 3 4 5 6 7 8 9 10 11 | DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NAME PIC X(25). ---> ELEMENTARY ITEM 01 WS-CLASS PIC 9(2) VALUE '10'. ---> ELEMENTARY ITEM 01 WS-ADDRESS. ---> GROUP ITEM 05 WS-HOUSE-NUMBER PIC 9(3). ---> ELEMENTARY ITEM 05 WS-STREET PIC X(15). ---> ELEMENTARY ITEM 05 WS-CITY PIC X(15). ---> ELEMENTARY ITEM 05 WS-COUNTRY PIC X(15) VALUE 'INDIA'. ---> ELEMENTARY ITEM |
In the above example, you can see that the WS-NAME variable is declared as a string of 25 characters, and that WS-CLASS is declared as a two-digit number.
When declared, the value can always be assigned with the keyword VALUE as above: WS-CLASS has the value of ’10’, WS-COUNTRY has the value of ‘INDIA’.
Instead of writing PIC X (n) or PIC 9 (n) with n as the number of characters, you can declare it as follows:
1 2 3 | 01 WS-CLASSA PIC 9(2). 01 WS-CLASSB PIC 99. |
The two ways to declare WS-CLASSA and WS-CLASSB are the same.
When you want to perform a field declaration for both alphanumeric characters (such as Zip Code), we can declare a mix of letters and numbers as follows:
1 2 | 05 ZIP-PLUS-9 PIC 99999X9999 VALUE '01886-2001'. |
2. The basic syntax in COBOL programming:
Here I will list some basic commands commonly used in COBOL program and examples for your reference.
COMPUTE: command used to calculate the value. For example:
1 2 | COMPUTE NUMC = (NUM1 * NUM2). |
means NUMC is assigned a value by multiplying NUM1 and NUM2.
DIVIDE-BY-GIVING: 2-digit split command. For example:
1 2 | DIVIDE NUMA BY NUMB GIVING RES-DIV. |
means that REST-DIV is assigned a value equal to the result of dividing NUMA by NUMB.
The same goes for the MULTIPLY (multiply), SUBTRACT (minus), ADD (plus) statements.
MOVE: command used to assign values.
For example:
1 2 | MOVE NUMA TO RES-MOV. |
means assigning the RES-MOV value equal to the value of NUMA.
The MOVE command can be assigned to multiple variables as follows:
1 2 | MOVE 25 TO WS-NUM1 WS-NUM3. |
This means assigning the value 25 to both variables WS-NUM1 and WS-NUM3.
DISPLAY: command to print on screen (print the value of a variable or a string)
For example:
1 2 3 4 | MOVE 15 TO WS-NUM1. DISPLAY WS-NUM1. DISPLAY 'THIS IS A STRING'. |
IF statement: conditional statement, almost identical to other languages. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | MOVE 25 TO WS-NUM1 WS-NUM3. MOVE 15 TO WS-NUM2 WS-NUM4. IF WS-NUM1 > WS-NUM2 THEN DISPLAY 'IN LOOP 1 - IF BLOCK' IF WS-NUM3 = WS-NUM4 THEN DISPLAY 'IN LOOP 2 - IF BLOCK' ELSE DISPLAY 'IN LOOP 2 - ELSE BLOCK' END-IF ELSE DISPLAY 'IN LOOP 1 - ELSE BLOCK' END-IF. |
The output will look like this:
IN LOOP 1 – IF BLOCK
IN LOOP 2 – IF BLOCK
We can use negative conditions with the keyword IF NOT instead of IF.
EVALUATE command: this is the equivalent of the Switch command in C.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | MOVE 3 TO WS-A. EVALUATE TRUE WHEN WS-A > 2 DISPLAY 'WS-A GREATER THAN 2' WHEN WS-A < 0 DISPLAY 'WS-A LESS THAN 0' WHEN OTHER DISPLAY 'INVALID VALUE OF WS-A' END-EVALUATE. |
The result will be: WS-A GREATER THAN 2
Above are some basic syntax of COBOL, next time we will try to apply these commands to write a small program.
Reference links:
https://medium.com/@yvanscher/7-cobol-examples-with-explanations-ae1784b4d576