HP OpenVMS Systems Documentation
Like statements, COBOL sentences also can be compiler-directing, imperative, or conditional. Sentence type depends upon the types of statements the sentence contains. Table 6-2 summarizes the contents of the three types of COBOL sentences. The remaining text in this section describes each type of statement and sentence in greater detail.
|Type||Contents of Sentence|
|Imperative||One or more consecutive imperative statements ending with a period|
|Conditional||One or more conditional statements, optionally preceded by an imperative statement, terminated by the separator period|
|Compiler-Directing||Only one compiler-directing statement ending with a period|
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.
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
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 is a special category of statement used in structured programming. A delimited-scope statement is any statement that includes its explicit scope terminator. See Section 6.3.4 for a list of explicit scope terminators.
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 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: