IBM - VisualAge Cobol for Windows Programming Guide
DisciplinaCobol25 materiais • 168 seguidores
terminator becomes an imperative statement called a delimited scope statement. Only imperative statements (or delimited scope statements) can be nested. RELATED CONCEPTS \u201cConditional statements\u201d \u201cScope terminators\u201d on page 17 Conditional statements A conditional statement is either a simple conditional statement (IF, EVALUATE, SEARCH) or a conditional statement made up of an imperative statement that includes a conditional phrase or option. You can end a conditional statement with an implicit or explicit scope terminator. If you end a conditional statement explicitly, it becomes a delimited scope statement (which is an imperative statement). You can use a delimited scope statement in these ways: v To delimit the range of operation for a COBOL conditional statement and to explicitly show the levels of nesting For example, use an END-IF statement instead of a period to end the scope of an IF statement within a nested IF. v To code a conditional statement where the COBOL syntax calls for an imperative statement For example, code a conditional statement as the object of an inline PERFORM: PERFORM UNTIL TRANSACTION-EOF PERFORM 200-EDIT-UPDATE-TRANSACTION IF NO-ERRORS 16 Programming Guide PERFORM 300-UPDATE-COMMUTER-RECORD ELSE PERFORM 400-PRINT-TRANSACTION-ERRORS END-IF READ UPDATE-TRANSACTION-FILE INTO WS-TRANSACTION-RECORD AT END SET TRANSACTION-EOF TO TRUE END-READ END-PERFORM An explicit scope terminator is required for the inline PERFORM statement, but it is not valid for the out-of-line PERFORM statement. For additional program control, you can use the NOT phrase with conditional statements. For example, you can provide instructions to be performed when a particular exception does not occur, such as NOT ON SIZE ERROR. The NOT phrase cannot be used with the ON OVERFLOW phrase of the CALL statement, but it can be used with the ON EXCEPTION phrase. Do not nest conditional statements. Nested statements must be imperative statements (or delimited scope statements) and must follow the rules for imperative statements. The following statements are examples of conditional statements if they are coded without scope terminators: v Arithmetic statement with ON SIZE ERROR v Data-manipulation statements with ON OVERFLOW v CALL statements with ON OVERFLOW v I/O statements with INVALID KEY, AT END, or AT END-OF-PAGE v RETURN with AT END RELATED CONCEPTS \u201cImperative statements\u201d on page 16 \u201cScope terminators\u201d RELATED TASKS \u201cSelecting program actions\u201d on page 67 RELATED REFERENCES Conditional statements (IBM COBOL Language Reference) Compiler-directing statements A compiler-directing statement is not part of the program logic. A compiler-directing statement causes the compiler to take specific action about the program structure, COPY processing, listing control, control flow, or CALL interface convention. RELATED REFERENCES \u201cCompiler-directing statements\u201d on page 198 Compiler-directing statements (IBM COBOL Language Reference) Scope terminators Scope terminators can be explicit or implicit. Explicit scope terminators end a verb without ending a sentence. They consist of END followed by a hyphen and the name of the verb being terminated, such as END-IF. An implicit scope terminator is a period (.) that ends the scope of all previous statements not yet ended. Chapter 1. Structuring your program 17 Each of the two periods in the following program fragment ends an IF statement, making the code equivalent to the code after it that instead uses explicit scope terminators: IF ITEM = \u201cA\u201d DISPLAY \u201cTHE VALUE OF ITEM IS \u201d ITEM ADD 1 TO TOTAL MOVE \u201cC\u201d TO ITEM DISPLAY \u201cTHE VALUE OF ITEM IS NOW \u201d ITEM. IF ITEM = \u201cB\u201d ADD 2 TO TOTAL. IF ITEM = \u201cA\u201d DISPLAY \u201cTHE VALUE OF ITEM IS \u201d ITEM ADD 1 TO TOTAL MOVE \u201cC\u201d TO ITEM DISPLAY \u201cTHE VALUE OF ITEM IS NOW \u201d ITEM END-IF IF ITEM = \u201cB\u201d ADD 2 TO TOTAL END-IF If you use implicit terminators, the end of statements can be unclear. As a result, you might end statements unintentionally, changing your program\u2019s logic. Explicit scope terminators make a program easier to understand and prevent unintentional ending of statements. For example, in the program fragment below, changing the location of the first period in the first implicit scope example changes the meaning of the code: IF ITEM = \u201cA\u201d DISPLAY \u201cVALUE OF ITEM IS \u201d ITEM ADD 1 TO TOTAL. MOVE \u201cC\u201d TO ITEM DISPLAY \u201c VALUE OF ITEM IS NOW \u201d ITEM IF ITEM = \u201cB\u201d ADD 2 TO TOTAL. The MOVE statement and the DISPLAY statement after it are performed regardless of the value of ITEM, despite what the indentation indicates, because the first period terminates the IF statement. For improved program clarity and to avoid unintentional ending of statements, use explicit scope terminators, especially within paragraphs. Use implicit scope terminators only at the end of a paragraph or the end of a program. Be careful when coding an explicit scope terminator for an imperative statement that is nested within a conditional statement. Ensure that the scope terminator is paired with the statement for which it was intended. In the following example, the scope terminator will be paired with the second READ statement, though the programmer intended it to be paired with the first. READ FILE1 AT END MOVE A TO B READ FILE2 END-READ To ensure that the explicit scope terminator is paired with the intended statement, the preceding example can be recoded in this way: 18 Programming Guide READ FILE1 AT END MOVE A TO B READ FILE2 END-READ END-READ RELATED CONCEPTS \u201cConditional statements\u201d on page 16 \u201cImperative statements\u201d on page 16 Declaratives Declaratives provide one or more special-purpose sections that are executed when an exception condition occurs. Start each declarative section with a USE statement that identifies the function of the section; in the procedures, specify the actions to be taken when the condition occurs. RELATED TASKS \u201cFinding and handling input-output errors\u201d on page 223 RELATED REFERENCES Declaratives (IBM COBOL Language Reference) Chapter 1. Structuring your program 19 20 Programming Guide Chapter 2. Using data This section is intended to help the non-COBOL programmer relate terms used in other programming languages to COBOL terms for data. It introduces COBOL fundamentals for: v Variables, structures, literals, and constants v Assigning and displaying values v Intrinsic (built-in) functions v Tables (arrays) and pointers RELATED TASKS \u201cUsing variables, structures, literals, and constants\u201d \u201cAssigning values to data items\u201d on page 23 \u201cUsing intrinsic functions (built-in functions)\u201d on page 27 \u201cUsing tables (arrays) and pointers\u201d on page 28 Using variables, structures, literals, and constants Most high-level programming languages share the concept of data being represented as variables, structures, literals, and constants. You place all data definitions in the DATA DIVISION of your program. The data in a COBOL program can be alphabetic, alphanumeric, or numeric. Variables A variable is a data item whose value can change during a program. The values are restricted, however, to the data type that you define when you give the variable a name and a length. For example, if a customer name is a variable in your program, you could code: Data Division. . . . 01 Customer-Name Pic X(20). 01 Original-Customer-Name Pic X(20). . . . Procedure Division. . . . Move Customer-Name to Original-Customer-Name . . . Data structure: data items and group items Related data items can be parts of a hierarchical data structure. A data item that does not have any subordinate items is called an elementary data item. A data item that is composed of subordinated data items is called a group item. A record can be either an elementary data item or a group of data items.