TinyCOBOLImplementation of programming language COBOL
TinyCOBOL is an open-source COBOL compiler, based on COBOL 85 standard. It was under active development till late 2010.
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:
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.