COBOL Reserved Words
Problem Description
Create a program demonstrating proper use of COBOL reserved words like MOVE, ADD, DISPLAY.
Expected Output
Correct usage of reserved words
Hints
Reserved words have special meaning and cannot be used as variable names.
Solution
IDENTIFICATION DIVISION.
PROGRAM-ID. RESERVED.
DATA DIVISION.
WORKING-STORAGE SECTION.
* Variable names must NOT be reserved words
* Wrong: 01 MOVE PIC X(10). (MOVE is reserved)
* Right: 01 WS-MOVE-DATA PIC X(10).
01 WS-NUM1 PIC 9(3) VALUE 100.
01 WS-NUM2 PIC 9(3) VALUE 50.
01 WS-RESULT PIC 9(4).
01 WS-MESSAGE PIC X(20).
PROCEDURE DIVISION.
* MOVE - copies data
MOVE "HELLO COBOL" TO WS-MESSAGE.
DISPLAY WS-MESSAGE.
* ADD - arithmetic addition
ADD WS-NUM1 TO WS-NUM2 GIVING WS-RESULT.
DISPLAY "SUM: " WS-RESULT.
* COMPUTE - complex calculations
COMPUTE WS-RESULT = WS-NUM1 - WS-NUM2.
DISPLAY "DIFF: " WS-RESULT.
STOP RUN.
Explanation:
Reserved words: MOVE, ADD, SUBTRACT, MULTIPLY, DIVIDE, COMPUTE, IF, PERFORM, etc.