A compiler-directing statement causes the compiler to take an action
during compilation. The verbs COPY, REPLACE, RECORD, and USE define
A compiler-directing sentence can contain other statements but it must
contain only one compiler-directing statement. The compiler-directing
statement must be the last statement in the sentence and must be
followed immediately by a period.
6.1.2 Imperative Statements and Sentences
An imperative statement specifies an unconditional action for the program. It must contain a verb and the verb's operands, and cannot contain any conditional phrases. For example, the following statements are imperative:
OPEN INPUT FILE-A COMPUTE C = A + B
However, the following statement is not imperative because it contains the phrase, ON SIZE ERROR, which makes the program's action conditional:
COMPUTE C = A + B ON SIZE ERROR PERFORM NUM-TOO-BIG.
A delimited-scope statement is a special category of statement used in structured programming. A delimited-scope statement is any statement that includes its explicit scope terminator. For more information, see Section 6.1.4 and Section 6.3.4.
In the Procedure Division rules, an imperative statement can be a sequence of consecutive imperative statements. The sequence must end with: (1) a separator period or (2) any phrase associated with a statement that contains the imperative statement. For example, the following sentence contains a sequence of two imperative statements following the AT END phrase.
READ FILE-A AT END PERFORM NO-MORE-RECS DISPLAY "No more records." END-READ.
An imperative sentence contains only imperative statements and ends
6.1.3 Conditional Statements and Sentences
A conditional statement determines a condition's truth value. (A truth value is either a yes or no answer to the question, "Is the condition true?".) The statement uses the truth value generated by the program to determine subsequent program action.
Conditional statements are as follows:
A conditional sentence is a conditional statement that ends with a separator period. It can include an optional preceding imperative statement. For example, the following sentence is conditional even though it contains the imperative statement, GO TO PROC-A:
READ FILEA AT END GO TO PROC-A.
The program interprets this sentence to mean "If not at the end of
the file, read the next record; otherwise, go to PROC-A."
6.1.4 Scope of Statements
Scope terminators delimit the scope of some Procedure Division statements.
The scope of statements contained (nested) in other statements can also terminate implicitly. When statements are contained in other statements, a separator period (that terminates the sentence) terminates all nested statements as well.
In the following example, the separator period terminates the IF, MOVE, and PERFORM statements:
IF ITEMA = ITEMB MOVE ITEMC TO ITEMB PERFORM PROCA.
In the following example, the ELSE phrase of the IF statement terminates the scope of the READ and the first MOVE statements:
IF ITEMA = ITEMB READ FILEA AT END MOVE ITEMC TO ITEMB ELSE MOVE ITEMD TO ITEME.
A delimited-scope statement can be nested in another delimited-scope statement with the same verb. Then, each explicit scope terminator terminates the statement that begins with the closest unpaired preceding occurrence of the verb.
In the following example, the END-IF after the ADD statements (line 8) terminates the IF on line 5. The END-IF after the SUBTRACT (line 10) terminates the IF on line 3. The scope of the first IF statement (line 1) is terminated by the separator period on line 11.
Every user-defined name in a COBOL program names a resource. (See the section on User-Defined Words in Section 1.2.1.) To use a resource, however, a statement in a COBOL program must contain a reference that uniquely identifies that resource. Qualification, reference modification, and subscripting or indexing allow unique and unambiguous references to that resource. Qualified procedure-names allow uniqueness of reference to procedures, and qualified condition-names allow uniqueness of reference to condition-names.
When you assign the same name in separate (contained) programs to two or more occurrences of a resource, certain conventions apply that limit the scope of names. Name scoping and scope of names are COBOL language terms that describe the methods for resolving references to user-defined words in a contained program environment. (See Section 6.2.6, Scope of Names.)
Some user-defined words can be made available to every program in the
run unit. (See the Section 5.3.21 clause in Chapter 5.) These words
are called external data. Other user-defined words can
be made available to programs contained within the program that defines
that resource. (See the Section 5.3.25 clause in Chapter 5.) These
words are called global data.
A reference to a user-defined word is unique if one or more of the following conditions exists:
A nonunique name within a hierarchy of names can be used in more than one place in your program. Unless you are redefining it, you must refer to this nonunique name using one or more higher-level names in the hierarchy. These higher-level names are called qualifiers. Using them to achieve uniqueness of reference is called qualification.
To make your reference unique, you need not specify all available qualifiers for a name, only those necessary to eliminate ambiguity.
Consider the following two record descriptions:
01 REC1. 05 ITEMA PIC XX. 05 ITEMB PIC X(20). 01 REC2. 05 GROUP1. 10 ITEMA PIC 9(5). 10 ITEMB PIC X(3). 05 GROUP2. 10 ITEMC PIC X(4). 10 ITEMD PIC X(8).
ITEMA and ITEMB appear in both record descriptions. Therefore, you must use qualifiers when you refer to these items in Procedure Division statements. For example, all of the following references to ITEMA are unique: ITEMA OF GROUP1, ITEMA OF REC1, ITEMA IN GROUP1 OF REC2.
Regardless of the preceding, you cannot use the same data-name as:
When a program is contained within a program or contains another program, specific conventions apply. (See Section 6.2.6.)
The general formats for qualification are as follows:
The following syntax rules apply to qualification:
Occurrences of a table are not individually named. You refer to them by using a subscript or index to specify their location relative to the table's beginning. Subscripting is a general operation; indexing is a special form of subscripting.
Unless otherwise specified by the rules for a statement, subscripts and indexes are evaluated once, at the beginning of a statement. If a statement contains rules describing the evaluation of subscripts, those rules also apply to the evaluation of indexes.
Subscripts can appear only in references to individual elements in a list, or table, of like elements that do not have individual data-names. (See the Section 5.3.35 clause in Chapter 5.)
The general format for subscripting is as follows:
All restrictions in the rules for subscripting also apply to indexing (See the following subsection describing Indexing.) The following rules apply to subscripting:
Use the check compiler option with the bounds keyword for run-time upper- and lower-bound subscript range verification. The default action is not to check. For more information, see the COBOL online help file for your particular platform.
In the following examples, references to ITEME require two subscripts. The first subscript refers to the occurrence number of the most inclusive dimension, ITEMD (that contains ITEME).
|Example 6-1 Subscripting Example|
WORKING-STORAGE SECTION. 01 ITEMA PIC 99 COMP VALUE IS 3. 01 ITEMB PIC 99 COMP VALUE IS 5. 01 ITEMC VALUE IS "ABCDEFGHIJKLMNOPQRSTUVWX". 03 ITEMD OCCURS 4 TIMES. 05 ITEME OCCURS 6 TIMES PIC X.
|ITEME (ITEMA * 2 - 4, ITEMB - 2)||I|
|ITEME (ITEMA * ITEMB / 15, (ITEMA + ITEMB) / 4)||B|
Indexing is a special subscripting procedure. In indexing, you use the INDEXED BY phrase of the OCCURS clause to assign an index-name to each table level. You then refer to a table element using the index-name as a subscript.
The general format for indexing follows:
All the restrictions in the rules for subscripting apply to indexing. (See Subscripting.) The following rules apply only to indexing:
Use the check compiler option with the bounds keyword for run-time upper- and lower-bound index range verification. The default is not to check. For more information, see the COBOL online help file for your particular platform.
Example 6-2 is similar to Example 6-1 that illustrates subscripting. However, this example shows the use of index-names in references to the table, initializing indexes with the SET statement, and storing index-name values in index data items.
|Example 6-2 Indexing Example|
WORKING-STORAGE SECTION. 01 ITEMA USAGE IS INDEX. 01 ITEMB USAGE IS INDEX. 01 ITEMC VALUE IS "ABCDEFGHIJKLMNOPQRSTUVWX". 03 ITEMD OCCURS 4 TIMES INDEXED BY DX. 05 ITEME OCCURS 6 TIMES INDEXED BY EX PIC X. PROCEDURE DIVISION. PARA. SET DX TO 4. SET EX TO 1. DISPLAY ITEMD (DX). (1) DISPLAY ITEME (DX, EX). (2) DISPLAY ITEME (DX - 3, EX) (3) SET ITEMA TO DX. SET ITEMB TO EX.
This example produces the following results:
data-name must refer to a data item whose usage is DISPLAY.
function-name must refer to an alphanumeric function.
The specifications for leftmost-character-position and length must be arithmetic expressions.
Each character of a data item has an ordinal number corresponding to its position. The leftmost position is number 1; successive positions to the right are numbered 2, 3, 4, and so on. If the data-name's data description entry has a SIGN IS SEPARATE clause, the sign position is assigned an ordinal number in the data item.
For a data item defined as numeric, numeric edited, alphanumeric, alphabetic, or alphanumeric edited, reference modification operates as if the data item were redefined as an alphanumeric data item the same size as that referred to by data-name.
Unless otherwise specified by the rules for a statement, reference modification is evaluated only once, at the beginning of a statement. Reference modification is evaluated immediately after subscripting or indexing evaluation. Rules that describe the evaluation of subscripting for the various statements also apply to the evaluation of reference modification.
The components of reference modification define the data item as follows:
The resulting unique data item is treated as an elementary item without the JUSTIFIED clause. It has the same class and category as the data item referred to by data-name. However, the categories numeric, numeric edited, and alphanumeric edited are considered category alphanumeric.
Reference modification is valid anywhere an alphanumeric identifier is allowed unless specific rules for a general format prohibit it.
Use the check compiler option with the bounds keyword for run-time upper- and lower-bound reference modification range verification. The default is not to check. For more information, see the COBOL online help file for your particular platform.
WORKING-STORAGE SECTION. 01 ITEMA PIC X(15) VALUE IS "ABCDEFGHIJKLMNO". 01 ITEMB PIC 99 VALUE IS 10.
|ITEMA (ITEMB / 2:ITEMB - 6)||EFGH|