COBOL is a business-oriented language revised multiple times since its original design in 1960.
1 *COBOL. Coding like it's 1985.
2 *Compiles with GnuCOBOL in OpenCobolIDE 4.7.6.
3
4 *COBOL has significant differences between legacy (COBOL-85)
5 *and modern (COBOL-2002 and COBOL-2014) versions.
6 *Legacy versions require columns 1-6 to be blank (they are used
7 *to store the index number of the punched card).
8 *A '*' in column 7 means a comment.
9 *In legacy COBOL, a comment can only be a full line.
10 *Modern COBOL doesn't require fixed columns and uses *> for
11 *a comment, which can appear in the middle of a line.
12 *Legacy COBOL also imposes a limit on maximum line length.
13 *Keywords have to be in capitals in legacy COBOL,
14 *but are case insensitive in modern.
15 *Although modern COBOL allows you to use mixed-case characters
16 *it is still common to use all caps when writing COBOL code.
17 *This is what most professional COBOL developers do.
18 *COBOL statements end with a period.
19
20 *COBOL code is broken up into 4 divisions.
21 *Those divisions, in order, are:
22 *IDENTIFICATION DIVISION.
23 *ENVIRONMENT DIVISION.
24 *DATA DIVISION.
25 *PROCEDURE DIVISION.
26
27 *First, we must give our program an ID.
28 *The IDENTIFICATION DIVISION can include other values too,
29 *but they are comments only. PROGRAM-ID is the only one that
30 *is mandatory.
31 IDENTIFICATION DIVISION.
32 PROGRAM-ID. LEARN.
33 AUTHOR. JOHN DOE.
34 DATE-WRITTEN. 05/02/2020.
35
36 *Let's declare some variables.
37 *We do this in the WORKING-STORAGE section within the DATA DIVISION.
38 *Each data item (aka variable) starts with a level number,
39 *then the name of the item, followed by a PICTURE clause
40 *describing the type of data that the variable will contain.
41 *Almost every COBOL programmer will abbreviate PICTURE as PIC.
42 *A is for alphabetic, X is for alphanumeric, and 9 is for numeric.
43
44 *example:
45 01 MYNAME PIC XXXXXXXXXX. *> A 10 character string.
46
47 *But counting all those Xs can lead to errors,
48 *so the above code can be re-written as
49 01 MYNAME PIC X(10).
50
51 *Here are some more examples:
52 01 AGE PICTURE 9(3). *> A number up to 3 digits.
53 01 BIRTH_YEAR PIC S9(7). *> A signed number up to 7 digits.
54 01 LAST_NAME PIC X(10). *> A string up to 10 characters.
55
56 *In COBOL, multiple spaces are the same as a single space, so it
57 *is common to use multiple spaces to line up your code so that it
58 *is easier for other coders to read.
59
60
61 *Now let's write some code. Here is a simple, Hello World program.
62 IDENTIFICATION DIVISION.
63 PROGRAM-ID. HELLO.
64 DATA DIVISION.
65 WORKING-STORAGE SECTION.
66 01 THE-MESSAGE PIC X(20).
67 PROCEDURE DIVISION.
68 DISPLAY "STARTING PROGRAM".
69 MOVE "HELLO WORLD" TO THE-MESSAGE.
70 DISPLAY THE-MESSAGE.
71 STOP RUN.
72
73 *The above code will output:
74 *STARTING PROGRAM
75 *HELLO WORLD
76
77
78
79 ********COBOL can perform math***************
80 ADD 1 TO AGE GIVING NEW-AGE.
81 SUBTRACT 1 FROM COUNT.
82 DIVIDE VAR-1 INTO VAR-2 GIVING VAR-3.
83 COMPUTE TOTAL-COUNT = COUNT1 PLUS COUNT2.
84
85
86 *********PERFORM********************
87 *The PERFORM keyword allows you to jump to another specified
88 *section of the code, and then to return to the next executable
89 *statement once the specified section of code is completed.
90 *You must write the full word, PERFORM, you cannot abbreviate it.
91
92 IDENTIFICATION DIVISION.
93 PROGRAM-ID. HELLOCOBOL.
94
95 PROCEDURE DIVISION.
96 FIRST-PARA.
97 DISPLAY 'THIS IS IN FIRST-PARA'.
98 *skip SECOND-PARA and perform 3rd & 4th
99 *then after performing THIRD-PARA and FOURTH-PARA,
100 *return here and continue the program until STOP RUN.
101 PERFORM THIRD-PARA THRU FOURTH-PARA.
102
103 SECOND-PARA.
104 DISPLAY 'THIS IS IN SECOND-PARA'.
105
106 STOP RUN.
107
108 THIRD-PARA.
109 DISPLAY 'THIS IS IN THIRD-PARA'.
110
111 FOURTH-PARA.
112 DISPLAY 'THIS IS IN FOURTH-PARA'.
113
114
115 *When you compile and execute the above program, it produces the
116 *following result (note the order):
117 *THIS IS IN FIRST-PARA
118 *THIS IS IN THIRD-PARA
119 *THIS IS IN FOURTH-PARA
120 *THIS IS IN SECOND-PARA
121
122
123 **********Combining variables together using STRING ***********
124
125 *Now it is time to learn about two related COBOL verbs: STRING and
126 *UNSTRING.
127
128 *The STRING verb is used to concatenate, or put together, two or
129 *more strings.
130 *UNSTRING is used, not surprisingly, to separate a
131 *string into two or more smaller strings.
132 *It is important that you remember to use DELIMITED BY when you
133 *are using STRING or UNSTRING in your program.
134
135 IDENTIFICATION DIVISION.
136 PROGRAM-ID. LEARNING.
137 ENVIRONMENT DIVISION.
138 DATA DIVISION.
139 WORKING-STORAGE SECTION.
140 01 FULL-NAME PIC X(20).
141 01 FIRST-NAME PIC X(13) VALUE "BOB GIBBERISH".
142 01 LAST-NAME PIC X(5) VALUE "COBB".
143 PROCEDURE DIVISION.
144 STRING FIRST-NAME DELIMITED BY SPACE
145 " "
146 LAST-NAME DELIMITED BY SIZE
147 INTO FULL-NAME
148 END-STRING.
149 DISPLAY "THE FULL NAME IS: "FULL-NAME.
150 STOP RUN.
151
152
153 *The above code will output:
154 *THE FULL NAME IS: BOB COBB
155
156
157 *Let's examine it to see why.
158
159 *First, we declared all of our variables, including the one that
160 *we are creating by the string command, in the DATA DIVISION.
161
162 *The action takes place down in the PROCEDURE DIVISION.
163 *We start with the STRING keyword and end with END-STRING. In
164 *between we list what we want to combine together into the larger,
165 *master variable. Here, we are combining FIRST-NAME, a space, and
166 *LAST-NAME.
167
168 *The DELIMITED BY phrase that follows FIRST-NAME and
169 *LAST-NAME tells the program how much of each variable we want to
170 *capture.
171 *DELIMITED BY SPACE tells the program to start at the beginning,
172 *and capture the variable until it runs into a space.
173 *DELIMITED BY SIZE tells the program to capture the full size of
174 *the variable.
175 *Since we have DELIMITED BY SPACE after FIRST-NAME, the GIBBERISH
176 *part is ignored.
177
178 *To make this clearer, change line 10 in the above code to
179 STRING FIRST-NAME DELIMITED BY SIZE
180 *and then re-run the program. This time the output is:
181 *THE FULL NAME IS: BOB GIBBERISH COBB