COBOL

Appeared in:
1959
Paradigm:
Typing discipline:
Versions and implementations (Collapse all | Expand all):
Programming language

COBOL (acronym for COmmon Business-Oriented Language) is one of the oldest programming languages in existence, aimed primarily at creating business-applications.

First COBOL specification was created in 1959. Language creators aimed to make it machine-independent and as close to natural language as possible. Both aims were hit; COBOL programs are considered to be understandable even for non-professionals, since they are self-documenting.

COBOL is a very old language, and used to be widely used for solving practical problems, so it had spawned a lot of dialects and implementations. It survived through several revisions; main standards of the language were set in 1968, 1974, 1985 and 2002. 2002 standard introduced a lot of major changes in the language, including support for object-oriented programming, user-defined functions, pointers and memory management, bit, boolean and floating-point data types, and conventions for interacting with other languages and frameworks.

Examples:

Hello, World!:

Example for versions OpenCOBOL 1.0, TinyCOBOL 0.65.9
        IDENTIFICATION DIVISION.
        PROGRAM-ID. HELLO-WORLD.
 
        PROCEDURE DIVISION.
                DISPLAY 'Hello, World!'
                STOP RUN.

Factorial:

Example for versions OpenCOBOL 1.0, TinyCOBOL 0.65.9

This example uses iterative factorial definition. Variable which stores factorial value has type 9(15), i.e., a number of 15 digits. Command MULTIPLY multiplies first argument by second and (counter-intuitively) puts the result into the second argument. Command DISPLAY puts a newline after the printed value, so one has to concatenate the parts of the string before printing them. Numerical values have to be cast to string by saving them into string-type variables. There is an exception handler at multiplication, though this is optional.

Program output depends on the compiler: the number are always printed fixed-width, padded with zeros — in TinyCOBOL to total width of 18 characters, in OpenCOBOL — 15.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SAMPLE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

         77 fact pic 9(15) comp.
         77 n pic 99.
         77 i pic 99.
         77 ist pic XX.
         77 factst pic X(18).

       PROCEDURE DIVISION.
         move 16 to n
         move 0 to i
         move 1 to fact
         perform until i greater than n
           move i to ist
           move fact to factst
           display ist "! = " factst
           add 1 to i
           multiply i by fact
             on size error display "value too big"
           end-multiply
         end-perform.
         stop run.

Fibonacci numbers:

Example for versions OpenCOBOL 1.0, TinyCOBOL 0.65.9

This example uses iterative calculation of Fibonacci numbers. Two numbers are added using command ADD which puts the sum of two arguments in the third one. DISPLAY adds a newline after each item it prints, so all numbers have to be concatenated in one string before being printed. To do this, STRING command is used. For each item concatenated one has to set DELIMITED BY option: SIZE — all variable is used (declared size of it), SPACE — the part of the variable till first whitespace. This means that the concatenated string can’t contain spaces, and the output looks like this:

001,001,002,003,005,008,013,021,034,055,089,144,233,377,610,987,...

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SAMPLE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

         77 fib1 pic 999.
         77 fib2 pic 999.
         77 fib3 pic 999.
         77 i pic 99.
         77 fibst pic XXX.
         77 res pic X(64).

       PROCEDURE DIVISION.
         move 0 to i
         move 0 to fib1
         move 1 to fib2
         move "" to res
         perform until i greater than 15
           add fib1 to fib2 giving fib3
           move fib2 to fib1
           move fib3 to fib2
           move fib1 to fibst
           string res   DELIMITED BY SPACE
                  fibst DELIMITED BY SIZE
                  ","   DELIMITED BY SIZE into res
           add 1 to i
         end-perform.
         display res "..."
         stop run.