Concepts you should be comfortable with:
Blocking records:
If records are stored on disk within a block, then the block of data will be transferred with one I/O statement and the program can process the records in the block one at a time without doing a physical I/O. A block of records is therefore referred to as a physical record while each individual record that will be handled with a READ is referred to as a logical block. Blocking can be documented in the program through the BLOCK contains clause in the FD. Today, operating systems usually handle blocking automatically and therefore, to the programmer, the blocking is frequently transparent.
FD FILE-IN
BLOCK CONTAINS
Note that blocking can also be expressed in characters.
Along the same lines, you have occasionally seen a LABEL RECORDS clause in my programs. These were used to tell whether standard label records were on the file or not. This is now purely documentation and is a function handled by the operating system.
Special Names:
Special names allows you to associate a name that you will use in your program to a hardware feature (implementor name) available in COBOL. We are used to just saying after advancing PAGE when we write the first line of headers. Some computers do not support this feature, so you must establish the name for advancing a page in the special names section.
ENVIRONMENT DIVISION
CONFIGURATION SECTION.
SPECIAL-NAMES.
C01 IS TOP-OF-PAGE.
After this is set up, I can move to the top of page by doing the write of the first header line after advancing TOP-OF-PAGE.
Structure of program:
Remember that a programming language is made up of three logical structures:
PERFORM:
(for exact layouts of these verbs, see your compiler's help)PERFORM ROUTINE1 THRU ROUTINE2.
PERFORM B-300-CALC 10 TIMES.
PERFORM B-300-CALC COUNT-WS TIMES.
Note that the thru/through clause can be used here.
PERFORM B-300-CALC
UNTIL AMT > 100.
This will cause the paragraph/routine B-300-CALC to be performed until AMT > 100. The logic is it checks the UNTIL clause and then executes the paragraph. If AMT starts out greater than 100, the paragraph will never be executed. Note that TEST BEFORE is the default if the TEST clause is not used.
PERFORM B-300-CALC
WITH TEST AFTER
UNTIL AMT > 100.
With the test after clause the paragraph is executed and then the TEST is made. This means that the paragraph will aways be executed at least once.
Note that the thru/through clause and or the test before/after clauses can be used here
Note also that you can have embedded varying from...by...until... clauses
PERFORM B-300-CALC
VARYING AMT FROM 1 BY 10
UNTIL AMT > 100.
Usage is:
Text fields (i.e. PIC X or PIC A) have usage display which is the default for all fields if no usage clause is used. Basically usage display means that each character will be stored with its ASCII/EBCDIC code using one byte of storage. Numeric fields can have a variety of usage clauses associated with them. Binary, Comp, and Computational essentially refer to data that is represented in pure binary format - it is efficient and if data is stored in binary format than this usage should be specified.
Packed-decimal is used for more efficiency in mathematical calculations (packed-decimal fields were referred to as comp-3 and computational-3 in other COBOLs). The data is stored in a packed format, which means that two digits are stored in one byte. This is the format that numbers will be converted to prior to doing the calculation or accumulation. If the conversion does not have to be made before doing the accumulation and again after doing the calculation, machine language code and therefore time is saved.
Indexed fields are used with table handling as you saw with the SEARCH verb and the index that was set up with the table.
Remember also that if data has the possibility of going negative, the picture must contain an S to remember the sign and the output, if you are printing, must contain a place to print the sign. If the data is being stored on disk, make sure that the disk field contains an S to remember the sign.
OPEN:
So far we have looked at OPEN with INPUT, OUTPUT and I-O. There is also the possibility of opening a file with EXTEND which means that new data will be added to the end of the file. For example, lets say you wanted to add data to a file you will be processing sequentially and you want to add the records in at the end of the file. The EXTEND would be used to handle this.
WRITE:
Remember that the WRITE statement does have a BEFORE or AFTER clause. Most programs are written using the AFTER ADVANCING option, but the programmer could also use the BEFORE ADVANCING clause to advance before writing.
Relational operators:
Remember, when you are making comparisons you can use:
IS GREATER THAN
IS NOT GREATER THAN
GREATER (Note that the IS and THEN are optional)
NOT GREATER
>
NOT >
Same optional use of IS and THEN with LESS and EQUAL - also same optional use of NOT:
LESS
<
EQUAL
=
GREATER OR EQUAL
IS GREATER THAN OR EQUAL TO
>=
LESS OR EQUAL
IS LESS THAN OR EQUAL TO
<=
Next Sentence and Continue:
Next sentence basically means you have nothing to do if this condition is met and so control will pass to the statement following the next period.
IF AMT > 100 |
IF AMT > 100 |
Next sentence and continue will pass control to the sentence after the IF
IF AMT > 100 |
IF AMT > 100 |
Next sentence will pass control to the command PERFORM B-300-PROC. |
Continue pass control to the MOVE statement and then to the PERFORM. |
EVALUATE:
In this example, you are evaluating type employee and responding with the appropriate processing.
EVALUATE TYPE-EMPLOYEE WHEN "F" MOVE "FULL TIME" TO EMP-TYPE-PR WHEN "P" MOVE "PART TIME" TO EMP-TYPE-PR WHEN "C" MOVE "CONSULTANT" TO EMP-TYPE-PR WHEN "T" MOVE "TEMPORARY" TO EMP-TYPE-PR WHEN OTHER MOVE "INVALID" TO EMP-TYPE-PR END-EVALUATE
This is just another way to do the evaluation, you are testing if the when statements are true.
EVALUATE TRUE WHEN TYPE-EMPLOYEE = "F" MOVE "FULL TIME" TO EMP-TYPE-PR WHEN TYPE-EMPLOYEE = "P" MOVE "PART TIME" TO EMP-TYPE-PR WHEN TYPE-EMPLOYEE = "C" MOVE "CONSULTANT" TO EMP-TYPE-PR WHEN TYPE-EMPLOYEE = "T" MOVE "TEMPORARY" TO EMP-TYPE-PR WHEN OTHER MOVE "INVALID" TO EMP-TYPE-PR END-EVALUATE
In this one type-employee has been defined with level 88s and the true test is using these level 88s in the when testing.
01 INPUT-REC. 05 TYPE-EMPLOYEE PIC X. 88 FULLTIME VALUE "F". 88 PARTTIME VALUE "P". 88 CONSULTANT VALUE "C". 88 TEMPORARY VALUE "T". ... EVALUATE TRUE WHEN FULLTIME MOVE "FULL TIME" TO EMP-TYPE-PR WHEN PARTTIME MOVE "PART TIME" TO EMP-TYPE-PR WHEN CONSULTANT MOVE "CONSULTANT" TO EMP-TYPE-PR WHEN TEMPORARY MOVE "TEMPORARY" TO EMP-TYPE-PR WHEN OTHER MOVE "INVALID" TO EMP-TYPE-PR END-EVALUATE
ON SIZE ERROR:
Remember that the on size error and the not on size error clauses can be attached to any mathematical statement (ADD, SUBTRACT, MULTIPLY, DIVIDE, COMPUTE). A size error results when there is a loss of significant digits or a division by 0.
SECTIONS:
We have only talked briefly about sections since they are not heavily used. Paragraphs in the procedure division can be grouped into sections. Like a paragraph, a section ends when another section begins. However, to differentiate paragraphs from sections, the word SECTION has to be used. If a section is performed, all paragraphs within that section will be executed. In the example below if the program said:
PERFORM SECTION-1-NAME
then PARA-1-NAME, PARA-2-NAME, and PARA-3-NAME would all be performed. When SECTION-2-NAME SECTION was encountered, the perform would be complete.
SECTION-1-NAME SECTION.
PARA-1-NAME.
...
PARA-2-NAME.
...
PARA-3-NAME.
...
SECTION-2-NAME SECTION.
GO TO:
The GO TO is strongly discouraged in structured programming. The GO TO goes to a paragraph without the built in return that we are familiar with in the PERFORM. This means control stays at the point the GO TO goes to and the program processes all instructions sequentially from that point on. This drop through processing ignores paragraph names as it sequentially drops through the instructions.
GO TO ...DEPENDING ON:
The GO TO...DEPENDING on is essentially an IF statement combined with a GO TO. The depending on has the name of the field which must contain a number in a range starting with 1. If the field contains a 1, then branching goes to the first paragraph listed. If in contains a 2 then branching goes to the second paragraph etc.
GO TO FIRST-PARA
SECOND-PARA
THIRD-PARA
FOURTH-PARA
DEPENDING ON TYPE-CODE.
GO TO OTHER-PARA.
In this example, if type-code is a 1, processing will go to/branch to first-para, if a 2 to second-para, if a 3 to third-para, if a 4 to fourth-para. Anything else will encounter the GO TO statement after the GO TO...DEPENDING ON and will go to other-para.