Quiz — Chapter 6: Advanced Conditional Logic

Multiple Choice

1. What is the evaluation order for the complex condition IF A OR B AND C?

a) (A OR B) AND C b) A OR (B AND C) c) Left to right: A, then OR B, then AND C d) It depends on the compiler

2. Which of the following is true about EVALUATE?

a) EVALUATE executes all WHEN clauses that match b) EVALUATE executes only the first matching WHEN clause c) EVALUATE requires a WHEN OTHER clause d) EVALUATE can only evaluate a single subject

3. An 88-level condition name can be associated with:

a) A single value only b) Multiple values using THRU, but not individual values c) Multiple individual values, ranges via THRU, or combinations thereof d) Only numeric values

4. What does IF WS-AMOUNT IS NUMERIC test when WS-AMOUNT is defined as PIC X(10)?

a) Whether WS-AMOUNT contains a valid signed numeric value b) Whether all 10 characters are digits 0-9 c) Whether WS-AMOUNT can be converted to a number d) Whether WS-AMOUNT contains at least one digit

5. Which statement correctly sets a condition name to true?

a) MOVE TRUE TO ACCT-IS-ACTIVE b) SET ACCT-IS-ACTIVE = TRUE c) SET ACCT-IS-ACTIVE TO TRUE d) IF ACCT-IS-ACTIVE SET TO TRUE

6. In an EVALUATE with ALSO, what does the keyword ANY mean?

a) The WHEN clause matches any value for that subject b) At least one subject must match c) All subjects must match d) The clause is optional

7. What is the maximum recommended nesting depth for IF statements in most COBOL coding standards?

a) 2 levels b) 3-4 levels c) 7 levels d) There is no recommended limit

8. IF WS-BALANCE IS NOT NEGATIVE is true when WS-BALANCE is:

a) Greater than zero only b) Zero only c) Zero or greater than zero d) Any value including negative

9. What happens when no WHEN clause matches in an EVALUATE without WHEN OTHER?

a) The program abends b) The program continues with the next statement after END-EVALUATE c) The last WHEN clause is executed by default d) A compile-time error occurs

10. The implied subject in IF WS-CODE = 'A' OR 'B' OR 'C' means the condition tests:

a) WS-CODE = 'A' OR the literal 'B' OR the literal 'C' b) WS-CODE = 'A' OR WS-CODE = 'B' OR WS-CODE = 'C' c) WS-CODE = 'ABC' d) This is a syntax error

True or False

11. COBOL guarantees short-circuit evaluation of AND conditions — if the first operand is false, the second is not evaluated.

12. An 88-level condition name can be defined at any level number, not just subordinate to elementary items.

13. EVALUATE TRUE ALSO TRUE allows you to create a two-dimensional decision table in code.

14. The SET statement can only set a condition name to TRUE, never to FALSE.

15. A user-defined class condition is defined in the SPECIAL-NAMES paragraph of the ENVIRONMENT DIVISION.

Short Answer

16. Explain the difference between EVALUATE WS-STATUS and EVALUATE TRUE in terms of what is being evaluated and when you would use each form.

17. A production program abends with S0C7 when processing a batch of account records. The field WS-ACCT-BALANCE (PIC 9(09)V99) contains invalid data from an upstream feed. Write a defensive coding pattern using class conditions that prevents this abend.

18. Given the following 88-level definitions, write a single IF statement using only condition names (no raw value comparisons) that checks: "Is the account active or pending, AND is the transaction a debit type, AND is the amount positive?"

       01  WS-ACCT-STATUS       PIC X(01).
           88  ACCT-ACTIVE       VALUE 'A'.
           88  ACCT-PENDING      VALUE 'P'.
           88  ACCT-CLOSED       VALUE 'C'.
           88  ACCT-PROCESSABLE  VALUE 'A' 'P'.
       01  WS-TXN-TYPE          PIC X(02).
           88  TXN-WITHDRAWAL    VALUE 'WD'.
           88  TXN-PAYMENT       VALUE 'PY'.
           88  TXN-FEE           VALUE 'FE'.
           88  TXN-IS-DEBIT      VALUE 'WD' 'PY' 'FE'.
       01  WS-TXN-AMOUNT        PIC S9(09)V99.

19. Describe two strategies for reducing nested IF depth without using GO TO. For each strategy, name one advantage and one disadvantage.

20. Why is it important to always include WHEN OTHER in an EVALUATE statement, even when you believe all possible values are covered by explicit WHEN clauses?


Answer Key

1. b) A OR (B AND C) — AND has higher precedence than OR.

2. b) EVALUATE executes only the first matching WHEN clause, then continues after END-EVALUATE.

3. c) Multiple individual values, ranges via THRU, or combinations thereof.

4. b) Whether all 10 characters are digits 0-9. For PIC X fields, NUMERIC tests for digit characters only.

5. c) SET ACCT-IS-ACTIVE TO TRUE

6. a) The WHEN clause matches any value for that subject — it acts as a wildcard.

7. b) 3-4 levels. Most coding standards recommend a maximum of 3-4 nested IF levels.

8. c) Zero or greater than zero. NOT NEGATIVE includes both POSITIVE and ZERO.

9. b) The program continues with the next statement after END-EVALUATE. No WHEN match means no action is taken.

10. b) WS-CODE = 'A' OR WS-CODE = 'B' OR WS-CODE = 'C'. The implied subject (WS-CODE) and operator (=) carry forward.

11. False. COBOL does not guarantee short-circuit evaluation. Both operands may be evaluated regardless of the first operand's result.

12. False. An 88-level must be subordinate to an elementary item (or a group item treated as elementary for condition purposes). It must directly follow the item it describes.

13. True. Each ALSO adds a dimension to the decision table.

14. False. In COBOL 2002 and later, if a FALSE IS clause is specified on the 88-level, SET can set it to FALSE. However, in pre-2002 standards, SET can only set to TRUE.

15. True. User-defined class conditions are defined using the CLASS clause in SPECIAL-NAMES.

16. EVALUATE WS-STATUS evaluates the identifier WS-STATUS against specific values in WHEN clauses (e.g., WHEN 'A', WHEN 'C'). It is used when branching based on the value of a single variable. EVALUATE TRUE evaluates which condition expression is true, allowing arbitrary boolean conditions in WHEN clauses (e.g., WHEN WS-BALANCE > 1000). It is used when the branching logic involves different comparisons, ranges, or conditions on multiple fields — essentially replacing nested IF-ELSE chains.

17. Example:

       IF WS-ACCT-BALANCE-X IS NUMERIC
           MOVE WS-ACCT-BALANCE-X TO WS-ACCT-BALANCE
       ELSE
           MOVE ZERO TO WS-ACCT-BALANCE
           MOVE 'NON-NUMERIC BALANCE' TO WS-ERROR-MSG
           ADD 1 TO WS-ERROR-COUNT
           PERFORM 9100-LOG-ERROR
       END-IF

The key is receiving the data into a PIC X field, testing with IS NUMERIC, and only moving to the PIC 9 field if the test passes.

18. IF ACCT-PROCESSABLE AND TXN-IS-DEBIT AND WS-TXN-AMOUNT IS POSITIVE

19. Strategy 1: Condition flags — Set a processing flag (e.g., WS-OK-TO-PROCESS) and check it before each successive condition. Advantage: No GO TO required. Disadvantage: Repeated flag checking adds verbosity and the flag must be managed carefully.

Strategy 2: Paragraph extraction — Move nested logic into separate, well-named paragraphs. Advantage: Each paragraph has a single responsibility and is independently testable. Disadvantage: Requires more paragraphs, and the flow of control is less visible in a single reading of the code.

20. Even when all known values are covered, WHEN OTHER protects against: (1) data corruption that produces unexpected values, (2) future changes that add new valid values without updating the EVALUATE, (3) upstream system changes that send new codes. Without WHEN OTHER, unexpected values cause the EVALUATE to silently do nothing, which is almost never the correct behavior and can be extremely difficult to diagnose.