mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-26 23:23:55 +00:00
[cobol] proofread
This commit is contained in:
parent
9a7151dba1
commit
390b17f4a8
101
cobol.md
101
cobol.md
@ -2,10 +2,10 @@
|
|||||||
name: COBOL
|
name: COBOL
|
||||||
contributors:
|
contributors:
|
||||||
- ["Hyphz", "http://github.com/hyphz/"]
|
- ["Hyphz", "http://github.com/hyphz/"]
|
||||||
filename: learn.COB
|
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.
|
COBOL is a business-oriented language revised multiple times since its original design in 1960.
|
||||||
|
|
||||||
```cobol
|
```cobol
|
||||||
*COBOL. Coding like it's 1985.
|
*COBOL. Coding like it's 1985.
|
||||||
@ -35,8 +35,9 @@ organizations.
|
|||||||
*PROCEDURE DIVISION.
|
*PROCEDURE DIVISION.
|
||||||
|
|
||||||
*First, we must give our program an ID.
|
*First, we must give our program an ID.
|
||||||
*Identification division can include other values too,
|
*The IDENTIFICATION DIVISION can include other values too,
|
||||||
*but they are comments only. Program-id is the only one that is mandatory.
|
*but they are comments only. PROGRAM-ID is the only one that
|
||||||
|
*is mandatory.
|
||||||
IDENTIFICATION DIVISION.
|
IDENTIFICATION DIVISION.
|
||||||
PROGRAM-ID. LEARN.
|
PROGRAM-ID. LEARN.
|
||||||
AUTHOR. JOHN DOE.
|
AUTHOR. JOHN DOE.
|
||||||
@ -45,29 +46,27 @@ organizations.
|
|||||||
*Let's declare some variables.
|
*Let's declare some variables.
|
||||||
*We do this in the WORKING-STORAGE section within the DATA DIVISION.
|
*We do this in the WORKING-STORAGE section within the DATA DIVISION.
|
||||||
*Each data item (aka variable) starts with a level number,
|
*Each data item (aka variable) starts with a level number,
|
||||||
*then the name of the item, followed by a picture clause
|
*then the name of the item, followed by a PICTURE clause
|
||||||
*describing the type of data that the variable will contain.
|
*describing the type of data that the variable will contain.
|
||||||
*Almost every COBOL programmer will abbreviate PICTURE as PIC.
|
*Almost every COBOL programmer will abbreviate PICTURE as PIC.
|
||||||
*A is for alphabetic, X is for alphanumeric, and 9 is for numeric.
|
*A is for alphabetic, X is for alphanumeric, and 9 is for numeric.
|
||||||
|
|
||||||
*example:
|
*example:
|
||||||
01 MYNAME PIC xxxxxxxxxx. *> A 10 character string.
|
01 MYNAME PIC XXXXXXXXXX. *> A 10 character string.
|
||||||
|
|
||||||
*But counting all those x's can lead to errors,
|
*But counting all those Xs can lead to errors,
|
||||||
*so the above code can, and should
|
*so the above code can be re-written as
|
||||||
*be re-written as:
|
|
||||||
01 MYNAME PIC X(10).
|
01 MYNAME PIC X(10).
|
||||||
|
|
||||||
*Here are some more examples:
|
*Here are some more examples:
|
||||||
01 AGE PIC 9(3). *> A number up to 3 digits.
|
01 AGE PICTURE 9(3). *> A number up to 3 digits.
|
||||||
|
01 BIRTH_YEAR PIC S9(7). *> A signed number up to 7 digits.
|
||||||
01 LAST_NAME PIC X(10). *> A string up to 10 characters.
|
01 LAST_NAME PIC X(10). *> A string up to 10 characters.
|
||||||
|
|
||||||
*In COBOL, multiple spaces are the same as a single space, so it is common
|
*In COBOL, multiple spaces are the same as a single space, so it
|
||||||
*to use multiple spaces to line up your code so that it is easier for other
|
*is common to use multiple spaces to line up your code so that it
|
||||||
*coders to read.
|
*is easier for other coders to read.
|
||||||
01 inyear picture s9(7). *> S makes number signed.
|
|
||||||
*> Brackets indicate 7 repeats of 9,
|
|
||||||
*> ie a 6 digit number (not an array).
|
|
||||||
|
|
||||||
*Now let's write some code. Here is a simple, Hello World program.
|
*Now let's write some code. Here is a simple, Hello World program.
|
||||||
IDENTIFICATION DIVISION.
|
IDENTIFICATION DIVISION.
|
||||||
@ -95,8 +94,8 @@ organizations.
|
|||||||
|
|
||||||
|
|
||||||
*********PERFORM********************
|
*********PERFORM********************
|
||||||
*The PERFORM keyword allows you to jump to another specified section of the code,
|
*The PERFORM keyword allows you to jump to another specified
|
||||||
*and then to return to the next executable
|
*section of the code, and then to return to the next executable
|
||||||
*statement once the specified section of code is completed.
|
*statement once the specified section of code is completed.
|
||||||
*You must write the full word, PERFORM, you cannot abbreviate it.
|
*You must write the full word, PERFORM, you cannot abbreviate it.
|
||||||
|
|
||||||
@ -106,12 +105,14 @@ organizations.
|
|||||||
PROCEDURE DIVISION.
|
PROCEDURE DIVISION.
|
||||||
FIRST-PARA.
|
FIRST-PARA.
|
||||||
DISPLAY 'THIS IS IN FIRST-PARA'.
|
DISPLAY 'THIS IS IN FIRST-PARA'.
|
||||||
PERFORM THIRD-PARA THRU FOURTH-PARA. *>skip second-para and perform 3rd & 4th
|
*skip SECOND-PARA and perform 3rd & 4th
|
||||||
*> then after performing third and fourth,
|
*then after performing THIRD-PARA and FOURTH-PARA,
|
||||||
*> return here and continue the program until STOP RUN.
|
*return here and continue the program until STOP RUN.
|
||||||
|
PERFORM THIRD-PARA THRU FOURTH-PARA.
|
||||||
|
|
||||||
SECOND-PARA.
|
SECOND-PARA.
|
||||||
DISPLAY 'THIS IS IN SECOND-PARA'.
|
DISPLAY 'THIS IS IN SECOND-PARA'.
|
||||||
|
|
||||||
STOP RUN.
|
STOP RUN.
|
||||||
|
|
||||||
THIRD-PARA.
|
THIRD-PARA.
|
||||||
@ -121,22 +122,25 @@ organizations.
|
|||||||
DISPLAY 'THIS IS IN FOURTH-PARA'.
|
DISPLAY 'THIS IS IN FOURTH-PARA'.
|
||||||
|
|
||||||
|
|
||||||
*When you compile and execute the above program, it produces the following result:
|
*When you compile and execute the above program, it produces the
|
||||||
THIS IS IN FIRST-PARA
|
*following result (note the order):
|
||||||
THIS IS IN THIRD-PARA
|
*THIS IS IN FIRST-PARA
|
||||||
THIS IS IN FOURTH-PARA
|
*THIS IS IN THIRD-PARA
|
||||||
THIS IS IN SECOND-PARA
|
*THIS IS IN FOURTH-PARA
|
||||||
|
*THIS IS IN SECOND-PARA
|
||||||
|
|
||||||
|
|
||||||
**********Combining variables together using STRING ***********
|
**********Combining variables together using STRING ***********
|
||||||
|
|
||||||
*Now it is time to learn about two related COBOL verbs: string and unstring.
|
*Now it is time to learn about two related COBOL verbs: STRING and
|
||||||
|
*UNSTRING.
|
||||||
|
|
||||||
*The string verb is used to concatenate, or put together, two or more strings.
|
*The STRING verb is used to concatenate, or put together, two or
|
||||||
*Unstring is used, not surprisingly, to separate a
|
*more strings.
|
||||||
|
*UNSTRING is used, not surprisingly, to separate a
|
||||||
*string into two or more smaller strings.
|
*string into two or more smaller strings.
|
||||||
*It is important that you remember to use ‘delimited by’ when you
|
*It is important that you remember to use DELIMITED BY when you
|
||||||
*are using string or unstring in your program.
|
*are using STRING or UNSTRING in your program.
|
||||||
|
|
||||||
IDENTIFICATION DIVISION.
|
IDENTIFICATION DIVISION.
|
||||||
PROGRAM-ID. LEARNING.
|
PROGRAM-ID. LEARNING.
|
||||||
@ -157,36 +161,37 @@ organizations.
|
|||||||
|
|
||||||
|
|
||||||
*The above code will output:
|
*The above code will output:
|
||||||
THE FULL NAME IS: BOB COBB
|
*THE FULL NAME IS: BOB COBB
|
||||||
|
|
||||||
|
|
||||||
*Let’s examine it to see why.
|
*Let's examine it to see why.
|
||||||
|
|
||||||
*First, we declared all of our variables, including the one that we are creating
|
*First, we declared all of our variables, including the one that
|
||||||
*by the string command, in the DATA DIVISION.
|
*we are creating by the string command, in the DATA DIVISION.
|
||||||
|
|
||||||
*The action takes place down in the PROCEDURE DIVISION.
|
*The action takes place down in the PROCEDURE DIVISION.
|
||||||
*We start with the STRING keyword and end with END-STRING. In between we
|
*We start with the STRING keyword and end with END-STRING. In
|
||||||
*list what we want to combine together into the larger, master variable.
|
*between we list what we want to combine together into the larger,
|
||||||
*Here, we are combining FIRST-NAME, a space, and LAST-NAME.
|
*master variable. Here, we are combining FIRST-NAME, a space, and
|
||||||
|
*LAST-NAME.
|
||||||
|
|
||||||
*The DELIMITED BY phrase that follows FIRST-NAME and
|
*The DELIMITED BY phrase that follows FIRST-NAME and
|
||||||
*LAST-NAME tells the program how much of each variable we want to capture.
|
*LAST-NAME tells the program how much of each variable we want to
|
||||||
|
*capture.
|
||||||
*DELIMITED BY SPACE tells the program to start at the beginning,
|
*DELIMITED BY SPACE tells the program to start at the beginning,
|
||||||
*and capture the variable until it runs into a space.
|
*and capture the variable until it runs into a space.
|
||||||
*DELIMITED BY SIZE tells the program to capture the full size of the variable.
|
*DELIMITED BY SIZE tells the program to capture the full size of
|
||||||
*Since we have DELIMITED BY SPACE after FIRST-NAME, the GIBBERISH part is ignored.
|
*the variable.
|
||||||
|
*Since we have DELIMITED BY SPACE after FIRST-NAME, the GIBBERISH
|
||||||
*To make this clearer, change line 10 in the above code to:
|
*part is ignored.
|
||||||
|
|
||||||
|
*To make this clearer, change line 10 in the above code to
|
||||||
STRING FIRST-NAME DELIMITED BY SIZE
|
STRING FIRST-NAME DELIMITED BY SIZE
|
||||||
|
|
||||||
*and then re-run the program. This time the output is:
|
*and then re-run the program. This time the output is:
|
||||||
|
*THE FULL NAME IS: BOB GIBBERISH COBB
|
||||||
THE FULL NAME IS: BOB GIBBERISH COBB
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Further reading
|
## Further reading
|
||||||
|
|
||||||
* [GnuCOBOL](https://sourceforge.net/projects/open-cobol/)
|
* [GnuCOBOL](https://gnucobol.sourceforge.io/)
|
||||||
|
|
||||||
|
@ -4,7 +4,8 @@ contributors:
|
|||||||
translators:
|
translators:
|
||||||
- ["GOLGO11", "https://github.com/GOLGO11/"]
|
- ["GOLGO11", "https://github.com/GOLGO11/"]
|
||||||
---
|
---
|
||||||
COBOL是一门面向商业的语言,它从1960年最初设计以来被修订过数次。它被宣称仍然有超过80%的机构在使用它。
|
|
||||||
|
COBOL是一门面向商业的语言,它从1960年最初设计以来被修订过数次。
|
||||||
|
|
||||||
```cobol
|
```cobol
|
||||||
*COBOL. 最好是按照它1985年的标准来编程。
|
*COBOL. 最好是按照它1985年的标准来编程。
|
||||||
@ -179,5 +180,4 @@ COBOL是一门面向商业的语言,它从1960年最初设计以来被修订
|
|||||||
|
|
||||||
## 想了解更多吗?
|
## 想了解更多吗?
|
||||||
|
|
||||||
* [GnuCOBOL](https://sourceforge.net/projects/open-cobol/)
|
* [GnuCOBOL](https://gnucobol.sourceforge.io/)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user