*Now let's write some code. Here is a simple, Hello World program.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 THE-MESSAGE PIC X(20).
PROCEDURE DIVSION.
DISPLAY "STARTING PROGRAM".
MOVE "HELLO WORLD" TO THE-MESSAGE.
DISPLAY THE-MESSAGE.
STOP RUN.
*The above code will output:
*STARTING PROGRAM
*HELLO WORLD
********COBOL can perform math***************
ADD 1 TO AGE GIVING NEW-AGE.
SUBTRACT 1 FROM COUNT.
DIVIDE VAR-1 INTO VAR-2 GIVING VAR-3.
COMPUTE TOTAL-COUNT = COUNT1 PLUS COUNT2.
*********PERFORM********************
*The PERFORM keyword allows you to jump to another specified section of the code, and then to return to the next executable
*statement once the specified section of code is completed. You must write the full word, PERFORM, you cannot abbreviate it.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLOCOBOL.
PROCEDURE DIVISION.
FIRST-PARA.
DISPLAY 'THIS IS IN FIRST-PARA'.
PERFORM THIRD-PARA THRU FOURTH-PARA. *>skip over second-para and perfrom third and fourth
*> then after performing third and fourth, return here and continue the program until STOP RUN.
SECOND-PARA.
DISPLAY 'THIS IS IN SECOND-PARA'.
STOP RUN.
THIRD-PARA.
DISPLAY 'THIS IS IN THIRD-PARA'.
FOURTH-PARA.
DISPLAY 'THIS IS IN FOURTH-PARA'.
*When you compile and execute the above program, it produces the following result:
THIS IS IN FIRST-PARA
THIS IS IN THIRD-PARA
THIS IS IN FOURTH-PARA
THIS IS IN SECOND-PARA
**********Combining variables together using STRING ***********
*Now it is time to learn about two related COBOL verbs: string and unstring.
*The string verb is used to concatenate, or put together, two or more stings. Unstring is used, not surprisingly, to separate a *string into two or more smaller strings. It is important that you remember to use ‘delimited by’ when you
*are using string or unstring in your program.
IDENTIFICATION DIVISION.
PROGRAM-ID. LEARNING.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FULL-NAME PIC X(20).
01 FIRST-NAME PIC X(13) VALUE "BOB GIBBERISH".
01 LAST-NAME PIC X(5) VALUE "COBB".
PROCEDURE DIVISION.
STRING FIRST-NAME DELIMITED BY SPACE
" "
LAST-NAME DELIMITED BY SIZE
INTO FULL-NAME
END-STRING.
DISPLAY "THE FULL NAME IS: "FULL-NAME.
STOP RUN.
*The above code will output:
THE FULL NAME IS: BOB COBB
*Let’s examine it to see why.
*First, we declared all of our variables, including the one that we are creating by the string command, in the DATA DIVISISION.
*The action takes place down in the PROCEDURE DIVISION. We start with the STRING keyword and end with END-STRING. In between we *list what we want to combine together into the larger, master variable.
*Here, we are combining FIRST-NAME, a space, and LAST-NAME.
*The DELIMITED BY phrase that follows FIRST-NAME and LAST-NAME tells the program how much of each variable we want to capture.
*DELIMITED BY SPACE tells the program to start at the beginning, and capture the variable until it runs into a space.
*DELIMITED BY SIZE tells the program to capture the full size of the variable.
*Since we have DELIMITED BY SPACE after FIRST-NAME, the GIBBERISH part is ignored.
*To make this clearer, change line 10 in the above code to:
STRING FIRST-NAME DELIMITED BY SIZE
*and then re-run the program. This time the output is: