2020-04-08 16:30:10 +00:00
|
|
|
---
|
|
|
|
language: COBOL
|
|
|
|
contributors:
|
|
|
|
- ["Hyphz", "http://github.com/hyphz/"]
|
|
|
|
filename: learn.COB
|
|
|
|
---
|
|
|
|
COBOL is a business-oriented language revised multiple times since its original design in 1960. It is claimed to still be used in over 80% of
|
|
|
|
organizations.
|
|
|
|
|
2020-04-09 14:20:49 +00:00
|
|
|
```cobol
|
2020-04-08 16:30:10 +00:00
|
|
|
*COBOL. Coding like it's 1985.
|
2020-04-09 14:20:49 +00:00
|
|
|
*Compiles with GnuCOBOL in OpenCobolIDE 4.7.6.
|
2020-04-08 16:30:10 +00:00
|
|
|
|
|
|
|
*COBOL has significant differences between legacy (COBOL-85)
|
|
|
|
*and modern (COBOL-2002 and COBOL-2014) versions.
|
2020-04-09 14:20:49 +00:00
|
|
|
*Legacy versions require columns 1-6 to be blank (they are used
|
2020-04-08 16:30:10 +00:00
|
|
|
*to store the index number of the punched card..)
|
|
|
|
*A * in column 7 means a comment.
|
|
|
|
*In legacy COBOL, a comment can only be a full line.
|
|
|
|
*Modern COBOL doesn't require fixed columns and uses *> for
|
|
|
|
*a comment, which can appear in the middle of a line.
|
|
|
|
*Legacy COBOL also imposes a limit on maximum line length.
|
|
|
|
*Keywords have to be in capitals in legacy COBOL,
|
|
|
|
*but are case insensitive in modern.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
*First, we must give our program ID.
|
2020-04-09 14:20:49 +00:00
|
|
|
*Identification division can include other values too,
|
2020-04-08 16:30:10 +00:00
|
|
|
*but they are comments only. Program-id is mandatory.
|
|
|
|
identification division.
|
|
|
|
program-id. learn.
|
|
|
|
|
|
|
|
*Let's declare some variables.
|
|
|
|
data division.
|
|
|
|
working-storage section.
|
|
|
|
|
2020-04-09 14:20:49 +00:00
|
|
|
*Variables are specified by a "picture" - how they should be
|
2020-04-08 16:30:10 +00:00
|
|
|
*displayed, and variable type is inferred from this.
|
|
|
|
*The "01" value is the level number which is used for building
|
|
|
|
*data structures.
|
|
|
|
01 myname picture xxxxxxxxxx. *> A 10 character string.
|
|
|
|
01 age picture 999. *> A number up to 3 digits.
|
|
|
|
01 valx picture 999. *> Another number up to 3 digits.
|
|
|
|
01 inyear picture s9(7). *> S makes number signed.
|
2020-04-09 14:20:49 +00:00
|
|
|
*> Brackets indicate 7 repeats of 9,
|
2020-04-08 16:30:10 +00:00
|
|
|
*> ie a 6 digit number (not an array).
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
*Now let's write some code.
|
|
|
|
procedure division.
|
|
|
|
|
|
|
|
main-procedure.
|
|
|
|
*> COBOL is the language that uses DISPLAY instead of PRINT.
|
|
|
|
*> Note: no full stops after commands. Only after the LAST
|
|
|
|
*> command.
|
|
|
|
display "Hello. What's your name?"
|
|
|
|
|
|
|
|
*> Let's input a string.
|
|
|
|
*> If input too long, later characters are trimmed.
|
|
|
|
accept myname
|
|
|
|
display "Hello " myname *> We can display several things.
|
|
|
|
display "How old are you?"
|
|
|
|
|
|
|
|
*> Let's input a number.
|
|
|
|
*> If input too long, EARLIER characters are trimmed.
|
|
|
|
accept age
|
|
|
|
|
|
|
|
display age *> Left-padded to three chracaters with zeroes,
|
|
|
|
*> because of the defined PICTURE for age.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
*> We have two ways of doing a FOR loop.
|
|
|
|
*> Old style way: doesn't give an index.
|
|
|
|
perform age times
|
|
|
|
display "*" with no advancing *> Ie, no newline at end
|
2020-04-09 14:20:49 +00:00
|
|
|
end-perform
|
2020-04-08 16:30:10 +00:00
|
|
|
display "." *> Output buffer isn't flushed until newline.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
*> New style way: with an index.
|
2020-04-09 14:20:49 +00:00
|
|
|
perform varying valx from 1 by 1 until valx > age
|
|
|
|
display valx "-" with no advancing
|
|
|
|
end-perform
|
2020-04-08 16:30:10 +00:00
|
|
|
display "."
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
*> If tests are still good old if tests.
|
2020-04-09 14:20:49 +00:00
|
|
|
if myname = "Bob" then
|
2020-04-08 16:30:10 +00:00
|
|
|
display "I don't like Bob."
|
2020-04-09 14:20:49 +00:00
|
|
|
else
|
|
|
|
display "I don't know you."
|
|
|
|
end-if
|
|
|
|
|
|
|
|
*> There are two ways of doing subprograms and calling
|
2020-04-08 16:30:10 +00:00
|
|
|
*> them.
|
|
|
|
*> The simplest way: a paragraph.
|
|
|
|
perform subparagraph
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
*> The complex way, with parameters and stuff.
|
2020-04-09 14:20:49 +00:00
|
|
|
call "eratosthenes" using age returning valx
|
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
display "There were " valx " primes."
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
stop run.
|
|
|
|
|
|
|
|
subparagraph. *> Marks the top of an internal subprogram.
|
|
|
|
*> Shares variable score with its caller.
|
|
|
|
|
2020-04-09 14:20:49 +00:00
|
|
|
*> Read year from system timer.
|
|
|
|
*> Remember the whole "year 2000 crisis"? The yyyyddd
|
|
|
|
*> option was added in response to that.
|
|
|
|
accept inyear from day yyyyddd.
|
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
*> We can do math step-by-step like this...
|
2020-04-09 14:20:49 +00:00
|
|
|
divide 1000 into inyear.
|
2020-04-08 16:30:10 +00:00
|
|
|
subtract age from inyear.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
display "You were born in " inyear "."
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
*> Or we can just use expressions.
|
|
|
|
compute inyear = 1970 - inyear.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
|
|
|
if inyear >= 0 then
|
2020-04-08 16:30:10 +00:00
|
|
|
display "When you were " inyear ", " with no advancing
|
|
|
|
else
|
|
|
|
display inyear " years before you were born, " with no
|
|
|
|
advancing
|
|
|
|
end-if
|
2020-04-09 14:20:49 +00:00
|
|
|
|
|
|
|
display "COBOL was the most popular language in the world."
|
|
|
|
. *> You can put the final . on a new line if it's clearer.
|
|
|
|
|
|
|
|
|
|
|
|
*If we want to use a subprogram, we use literally a subprogram.
|
|
|
|
*This is the entire program layout, repeated for the
|
|
|
|
*eratosthenes subroutine.
|
2020-04-08 16:30:10 +00:00
|
|
|
identification division.
|
|
|
|
program-id. eratosthenes.
|
|
|
|
|
|
|
|
data division.
|
|
|
|
working-storage section.
|
2020-04-09 14:20:49 +00:00
|
|
|
*Declare an array.
|
|
|
|
*We can declare a variable to use as an index for it at the
|
2020-04-08 16:30:10 +00:00
|
|
|
*same time.
|
|
|
|
01 sieve pic 9 occurs 999 times indexed by sa, sb.
|
2020-04-09 14:20:49 +00:00
|
|
|
*> Standard cobol doesn't have a boolean type.
|
2020-04-08 16:30:10 +00:00
|
|
|
01 pstart pic 999.
|
|
|
|
01 counter pic 999.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
*Our parameters have to be declared in the linkage section.
|
2020-04-09 14:20:49 +00:00
|
|
|
*Their pictures must match the values they're called with.
|
2020-04-08 16:30:10 +00:00
|
|
|
linkage section.
|
|
|
|
01 maxvalue picture 999.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
|
|
|
*"using" declares our actual parameter variables.
|
2020-04-08 16:30:10 +00:00
|
|
|
*"returning" declares the variable value returned at end.
|
|
|
|
procedure division using maxvalue returning counter.
|
|
|
|
main-procedure.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
display "Here are all the primes up to " maxvalue "."
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
perform varying sa from 1 by 1 until sa > maxvalue
|
|
|
|
move 1 to sieve (sa)
|
2020-04-09 14:20:49 +00:00
|
|
|
end-perform
|
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
perform varying sa from 2 by 1 until sa > maxvalue
|
2020-04-09 14:20:49 +00:00
|
|
|
if sieve(sa) = 1 then
|
2020-04-08 16:30:10 +00:00
|
|
|
compute pstart = sa + sa
|
2020-04-09 14:20:49 +00:00
|
|
|
perform varying sb from pstart by sa until sb >
|
|
|
|
maxvalue
|
2020-04-08 16:30:10 +00:00
|
|
|
move 0 to sieve(sb)
|
|
|
|
end-perform
|
|
|
|
end-if
|
|
|
|
end-perform
|
|
|
|
|
|
|
|
initialise counter *> To zero by default for a number.
|
|
|
|
|
2020-04-09 14:20:49 +00:00
|
|
|
perform varying sa from 2 by 1 until sa > maxvalue
|
2020-04-08 16:30:10 +00:00
|
|
|
if sieve(sa) = 1 THEN
|
|
|
|
display sa
|
|
|
|
add 1 to counter
|
2020-04-09 14:20:49 +00:00
|
|
|
end-if
|
2020-04-08 16:30:10 +00:00
|
|
|
end-perform.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
|
|
|
end program eratosthenes.
|
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
end program learn.
|
2020-04-09 14:20:49 +00:00
|
|
|
|
2020-04-08 16:30:10 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
##Ready For More?
|
|
|
|
|
|
|
|
* [GnuCOBOL](https://sourceforge.net/projects/open-cobol/)
|
|
|
|
|