mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-26 00:31:39 +03:00
Merge pull request #3901 from hyphz/master
[en-en/cobol] Corrections and some added explanation.
This commit is contained in:
commit
91f1dfe656
@ -7,12 +7,13 @@ 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
|
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.
|
organizations.
|
||||||
|
|
||||||
```
|
```cobol
|
||||||
*COBOL. Coding like it's 1985.
|
*COBOL. Coding like it's 1985.
|
||||||
|
*Compiles with GnuCOBOL in OpenCobolIDE 4.7.6.
|
||||||
|
|
||||||
*COBOL has significant differences between legacy (COBOL-85)
|
*COBOL has significant differences between legacy (COBOL-85)
|
||||||
*and modern (COBOL-2002 and COBOL-2014) versions.
|
*and modern (COBOL-2002 and COBOL-2014) versions.
|
||||||
*Legacy versions require columns 1-6 to be blank (they are used
|
*Legacy versions require columns 1-6 to be blank (they are used
|
||||||
*to store the index number of the punched card..)
|
*to store the index number of the punched card..)
|
||||||
*A * in column 7 means a comment.
|
*A * in column 7 means a comment.
|
||||||
*In legacy COBOL, a comment can only be a full line.
|
*In legacy COBOL, a comment can only be a full line.
|
||||||
@ -21,9 +22,9 @@ organizations.
|
|||||||
*Legacy COBOL also imposes a limit on maximum line length.
|
*Legacy COBOL also imposes a limit on maximum line length.
|
||||||
*Keywords have to be in capitals in legacy COBOL,
|
*Keywords have to be in capitals in legacy COBOL,
|
||||||
*but are case insensitive in modern.
|
*but are case insensitive in modern.
|
||||||
|
|
||||||
*First, we must give our program ID.
|
*First, we must give our program ID.
|
||||||
*Identification division can include other values too,
|
*Identification division can include other values too,
|
||||||
*but they are comments only. Program-id is mandatory.
|
*but they are comments only. Program-id is mandatory.
|
||||||
identification division.
|
identification division.
|
||||||
program-id. learn.
|
program-id. learn.
|
||||||
@ -32,7 +33,7 @@ organizations.
|
|||||||
data division.
|
data division.
|
||||||
working-storage section.
|
working-storage section.
|
||||||
|
|
||||||
*Variables are specified by a "picture" - how they should be
|
*Variables are specified by a "picture" - how they should be
|
||||||
*displayed, and variable type is inferred from this.
|
*displayed, and variable type is inferred from this.
|
||||||
*The "01" value is the level number which is used for building
|
*The "01" value is the level number which is used for building
|
||||||
*data structures.
|
*data structures.
|
||||||
@ -40,9 +41,9 @@ organizations.
|
|||||||
01 age picture 999. *> A number up to 3 digits.
|
01 age picture 999. *> A number up to 3 digits.
|
||||||
01 valx picture 999. *> Another number up to 3 digits.
|
01 valx picture 999. *> Another number up to 3 digits.
|
||||||
01 inyear picture s9(7). *> S makes number signed.
|
01 inyear picture s9(7). *> S makes number signed.
|
||||||
*> Brackets indicate 6 repeats of 9,
|
*> Brackets indicate 7 repeats of 9,
|
||||||
*> ie a 6 digit number (not an array).
|
*> ie a 6 digit number (not an array).
|
||||||
|
|
||||||
*Now let's write some code.
|
*Now let's write some code.
|
||||||
procedure division.
|
procedure division.
|
||||||
|
|
||||||
@ -64,98 +65,104 @@ organizations.
|
|||||||
|
|
||||||
display age *> Left-padded to three chracaters with zeroes,
|
display age *> Left-padded to three chracaters with zeroes,
|
||||||
*> because of the defined PICTURE for age.
|
*> because of the defined PICTURE for age.
|
||||||
|
|
||||||
*> We have two ways of doing a FOR loop.
|
*> We have two ways of doing a FOR loop.
|
||||||
*> Old style way: doesn't give an index.
|
*> Old style way: doesn't give an index.
|
||||||
perform age times
|
perform age times
|
||||||
display "*" with no advancing *> Ie, no newline at end
|
display "*" with no advancing *> Ie, no newline at end
|
||||||
end-perform.
|
end-perform
|
||||||
display "." *> Output buffer isn't flushed until newline.
|
display "." *> Output buffer isn't flushed until newline.
|
||||||
|
|
||||||
*> New style way: with an index.
|
*> New style way: with an index.
|
||||||
perform varying valx from 1 by 1 until valx > age
|
perform varying valx from 1 by 1 until valx > age
|
||||||
display valx "-" with no advancing
|
display valx "-" with no advancing
|
||||||
end-perform.
|
end-perform
|
||||||
display "."
|
display "."
|
||||||
|
|
||||||
*> If tests are still good old if tests.
|
*> If tests are still good old if tests.
|
||||||
if myname = "Bob" then
|
if myname = "Bob" then
|
||||||
display "I don't like Bob."
|
display "I don't like Bob."
|
||||||
else
|
else
|
||||||
display "I don't know you."
|
display "I don't know you."
|
||||||
end-if
|
end-if
|
||||||
|
|
||||||
*> There are two ways of doing subprograms and calling
|
*> There are two ways of doing subprograms and calling
|
||||||
*> them.
|
*> them.
|
||||||
*> The simplest way: a paragraph.
|
*> The simplest way: a paragraph.
|
||||||
perform subparagraph
|
perform subparagraph
|
||||||
|
|
||||||
*> The complex way, with parameters and stuff.
|
*> The complex way, with parameters and stuff.
|
||||||
call "eratosthenes" using age returning valx
|
call "eratosthenes" using age returning valx
|
||||||
|
|
||||||
display "There were " valx " primes."
|
display "There were " valx " primes."
|
||||||
|
|
||||||
stop run.
|
stop run.
|
||||||
|
|
||||||
subparagraph. *> Marks the top of an internal subprogram.
|
subparagraph. *> Marks the top of an internal subprogram.
|
||||||
*> Shares variable score with its caller.
|
*> Shares variable score with its caller.
|
||||||
|
|
||||||
|
*> 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.
|
accept inyear from day yyyyddd.
|
||||||
|
|
||||||
*> We can do math step-by-step like this...
|
*> We can do math step-by-step like this...
|
||||||
divide 1000 into inyear.
|
divide 1000 into inyear.
|
||||||
subtract age from inyear.
|
subtract age from inyear.
|
||||||
|
|
||||||
display "You were born in " inyear "."
|
display "You were born in " inyear "."
|
||||||
|
|
||||||
*> Or we can just use expressions.
|
*> Or we can just use expressions.
|
||||||
compute inyear = 1970 - inyear.
|
compute inyear = 1970 - inyear.
|
||||||
|
|
||||||
*> Note: if inyear has gone negative, its negativity will
|
if inyear >= 0 then
|
||||||
*> not appear when printed, because we didn't include an
|
|
||||||
*> S (sign character) in the PICTURE.
|
|
||||||
if inyear >= 0 then
|
|
||||||
display "When you were " inyear ", " with no advancing
|
display "When you were " inyear ", " with no advancing
|
||||||
else
|
else
|
||||||
display inyear " years before you were born, " with no
|
display inyear " years before you were born, " with no
|
||||||
advancing
|
advancing
|
||||||
end-if
|
end-if
|
||||||
|
|
||||||
display "COBOL was the most popular language in the world."
|
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.
|
||||||
identification division.
|
identification division.
|
||||||
program-id. eratosthenes.
|
program-id. eratosthenes.
|
||||||
|
|
||||||
data division.
|
data division.
|
||||||
working-storage section.
|
working-storage section.
|
||||||
*Declare an array.
|
*Declare an array.
|
||||||
*We can declare a variable to use as an index for it at the
|
*We can declare a variable to use as an index for it at the
|
||||||
*same time.
|
*same time.
|
||||||
01 sieve pic 9 occurs 999 times indexed by sa, sb.
|
01 sieve pic 9 occurs 999 times indexed by sa, sb.
|
||||||
|
*> Standard cobol doesn't have a boolean type.
|
||||||
01 pstart pic 999.
|
01 pstart pic 999.
|
||||||
01 counter pic 999.
|
01 counter pic 999.
|
||||||
|
|
||||||
*Our parameters have to be declared in the linkage section.
|
*Our parameters have to be declared in the linkage section.
|
||||||
|
*Their pictures must match the values they're called with.
|
||||||
linkage section.
|
linkage section.
|
||||||
01 maxvalue picture 999.
|
01 maxvalue picture 999.
|
||||||
|
|
||||||
*"using" declares our actual parameter variables.
|
*"using" declares our actual parameter variables.
|
||||||
*"returning" declares the variable value returned at end.
|
*"returning" declares the variable value returned at end.
|
||||||
procedure division using maxvalue returning counter.
|
procedure division using maxvalue returning counter.
|
||||||
main-procedure.
|
main-procedure.
|
||||||
|
|
||||||
display "Here are all the primes up to " maxvalue "."
|
display "Here are all the primes up to " maxvalue "."
|
||||||
|
|
||||||
perform varying sa from 1 by 1 until sa > maxvalue
|
perform varying sa from 1 by 1 until sa > maxvalue
|
||||||
move 1 to sieve (sa)
|
move 1 to sieve (sa)
|
||||||
end-perform
|
end-perform
|
||||||
|
|
||||||
perform varying sa from 2 by 1 until sa > maxvalue
|
perform varying sa from 2 by 1 until sa > maxvalue
|
||||||
if sieve(sa) = 1 then
|
if sieve(sa) = 1 then
|
||||||
compute pstart = sa + sa
|
compute pstart = sa + sa
|
||||||
perform varying sb from pstart by sa until sb >
|
perform varying sb from pstart by sa until sb >
|
||||||
maxvalue
|
maxvalue
|
||||||
move 0 to sieve(sb)
|
move 0 to sieve(sb)
|
||||||
end-perform
|
end-perform
|
||||||
end-if
|
end-if
|
||||||
@ -163,17 +170,17 @@ organizations.
|
|||||||
|
|
||||||
initialise counter *> To zero by default for a number.
|
initialise counter *> To zero by default for a number.
|
||||||
|
|
||||||
perform varying sa from 2 by 1 until sa > maxvalue
|
perform varying sa from 2 by 1 until sa > maxvalue
|
||||||
if sieve(sa) = 1 THEN
|
if sieve(sa) = 1 THEN
|
||||||
display sa
|
display sa
|
||||||
add 1 to counter
|
add 1 to counter
|
||||||
end-if
|
end-if
|
||||||
end-perform.
|
end-perform.
|
||||||
|
|
||||||
end program eratosthenes.
|
end program eratosthenes.
|
||||||
|
|
||||||
end program learn.
|
end program learn.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
##Ready For More?
|
##Ready For More?
|
||||||
|
Loading…
Reference in New Issue
Block a user