Exercises — Chapter 6: Advanced Conditional Logic
Exercise 6.1: Nested IF Refactoring (Beginner)
The following code processes employee benefit eligibility. Refactor it to reduce nesting to a maximum of two levels using one of the strategies discussed in this chapter (guard clauses, condition flags, or paragraph extraction).
IF WS-EMP-STATUS = 'A'
IF WS-EMP-TYPE = 'F'
IF WS-YEARS-SERVICE >= 1
IF WS-HOURS-WEEKLY >= 30
MOVE 'Y' TO WS-BENEFIT-ELIGIBLE
IF WS-YEARS-SERVICE >= 5
MOVE 'Y' TO WS-PENSION-ELIGIBLE
ELSE
MOVE 'N' TO WS-PENSION-ELIGIBLE
END-IF
ELSE
MOVE 'N' TO WS-BENEFIT-ELIGIBLE
MOVE 'N' TO WS-PENSION-ELIGIBLE
END-IF
ELSE
MOVE 'N' TO WS-BENEFIT-ELIGIBLE
MOVE 'N' TO WS-PENSION-ELIGIBLE
END-IF
ELSE
IF WS-EMP-TYPE = 'P'
IF WS-HOURS-WEEKLY >= 20
MOVE 'Y' TO WS-BENEFIT-ELIGIBLE
ELSE
MOVE 'N' TO WS-BENEFIT-ELIGIBLE
END-IF
MOVE 'N' TO WS-PENSION-ELIGIBLE
ELSE
MOVE 'N' TO WS-BENEFIT-ELIGIBLE
MOVE 'N' TO WS-PENSION-ELIGIBLE
END-IF
END-IF
ELSE
MOVE 'N' TO WS-BENEFIT-ELIGIBLE
MOVE 'N' TO WS-PENSION-ELIGIBLE
END-IF
Deliverables: 1. Define appropriate 88-level condition names for all status/type fields 2. Rewrite the logic using EVALUATE TRUE or guard clauses 3. Verify that your refactored version produces identical results for all input combinations
Exercise 6.2: EVALUATE Decision Table (Intermediate)
A university registration system determines tuition rates based on three factors: - Residency: In-state (I), Out-of-state (O), International (X) - Level: Undergraduate (U), Graduate (G), Doctoral (D) - Load: Full-time (F), Part-time (P)
The tuition rates per credit hour are:
| In-State Full | In-State Part | Out-of-State Full | Out-of-State Part | International Full | International Part | |
|---|---|---|---|---|---|---|
| Undergraduate | $350 | $400 | $850 | $950 | $1,200 | $1,350 | |||
| Graduate | $500 | $575 | $1,100 | $1,250 | $1,600 | $1,800 | |||
| Doctoral | $600 | $700 | $1,300 | $1,500 | $1,900 | $2,100 |
Tasks: 1. Define the DATA DIVISION entries with appropriate 88-level condition names 2. Write an EVALUATE using ALSO to implement this as a three-subject decision table 3. Include WHEN OTHER to handle invalid combinations 4. Add a DISPLAY statement showing the computed tuition for 15 credit hours
Exercise 6.3: 88-Level Condition Names (Beginner)
Create a complete DATA DIVISION entry for a vehicle classification system with the following requirements:
- Vehicle Type: Car (C), Truck (T), Motorcycle (M), Bus (B), RV (R)
- Group: Personal vehicles (C, M), Commercial vehicles (T, B), Oversized (B, R)
- Fuel Type: Gas (G), Diesel (D), Electric (E), Hybrid (H)
- Group: Combustion engines (G, D), Electric-capable (E, H)
- Weight Class: Light (L, under 6,000 lbs), Medium (M, 6,000-10,000), Heavy (H, over 10,000)
- Registration Status: Active (A), Expired (E), Suspended (S), Revoked (R)
- Group: Valid (A only), Invalid (E, S, R), Reinstateable (E, S)
Write a PROCEDURE DIVISION paragraph that uses these 88-levels to determine: 1. Whether the vehicle can use a standard toll lane (personal AND light AND valid registration) 2. Whether the vehicle qualifies for an electric vehicle discount (electric-capable AND valid) 3. Whether the vehicle requires a commercial inspection (commercial OR oversized OR heavy)
Exercise 6.4: Complex Conditions with Parentheses (Intermediate)
For each of the following conditions, determine the result given these values:
- WS-A = 10, WS-B = 20, WS-C = 30
- WS-X = 'Y', WS-Y = 'N', WS-Z = 'Y'
a) IF WS-A > 5 OR WS-B > 25 AND WS-C < 40
b) IF (WS-A > 5 OR WS-B > 25) AND WS-C < 40
c) IF NOT WS-X = 'Y' AND WS-Y = 'N'
d) IF NOT (WS-X = 'Y' AND WS-Y = 'N')
e) IF WS-A > WS-B OR > WS-C
f) IF WS-X = 'Y' OR 'N' AND WS-Z = 'Y'
For each, show the evaluation order, intermediate results, and final result. Then rewrite each condition with explicit parentheses that make the evaluation order unambiguous.
Exercise 6.5: Class Condition Validation (Intermediate)
Write a complete input validation paragraph for a customer record with these fields:
01 WS-CUSTOMER-INPUT.
05 WS-CUST-NAME-X PIC X(30).
05 WS-CUST-SSN-X PIC X(09).
05 WS-CUST-ZIP-X PIC X(05).
05 WS-CUST-PHONE-X PIC X(10).
05 WS-CUST-AGE-X PIC X(03).
05 WS-CUST-BALANCE-X PIC X(10).
Validation rules: 1. Name must be ALPHABETIC (letters and spaces only) 2. SSN must be NUMERIC (digits only) 3. ZIP must be NUMERIC 4. Phone must be NUMERIC 5. Age must be NUMERIC and, when converted, between 18 and 120 6. Balance must be NUMERIC
For each field, validate using class conditions, log a specific error message if invalid, and set an overall validation flag. Only process the record if all validations pass.
Exercise 6.6: MedClaim Adjudication Extension (Advanced)
Extend the MedClaim adjudication logic from Section 6.9 to handle these additional business rules:
- Emergency Services: If the service code begins with 'ER', skip the authorization and network checks (emergency services must always be processed regardless of network status or authorization)
- Coordination of Benefits (COB): If the member has a COB flag of 'Y', pend the claim with reason 'P004' (COB review needed) unless the claim amount is under $200
- Provider Sanctions: Add a provider sanction status field. If the provider is sanctioned ('S'), deny with reason 'D006'
- Timely Filing: If the service date is more than 365 days before the submission date, deny with reason 'D007'
Write the complete modified adjudication paragraph with all new conditions integrated in the correct priority order.
Exercise 6.7: EVALUATE TRUE Conversion (Beginner)
Convert the following nested IF chain to an EVALUATE TRUE statement:
IF WS-GRADE >= 90
MOVE 'A' TO WS-LETTER-GRADE
IF WS-GRADE >= 97
MOVE '+' TO WS-MODIFIER
ELSE
IF WS-GRADE >= 93
MOVE ' ' TO WS-MODIFIER
ELSE
MOVE '-' TO WS-MODIFIER
END-IF
END-IF
ELSE
IF WS-GRADE >= 80
MOVE 'B' TO WS-LETTER-GRADE
IF WS-GRADE >= 87
MOVE '+' TO WS-MODIFIER
ELSE
IF WS-GRADE >= 83
MOVE ' ' TO WS-MODIFIER
ELSE
MOVE '-' TO WS-MODIFIER
END-IF
END-IF
ELSE
IF WS-GRADE >= 70
MOVE 'C' TO WS-LETTER-GRADE
MOVE ' ' TO WS-MODIFIER
ELSE
IF WS-GRADE >= 60
MOVE 'D' TO WS-LETTER-GRADE
MOVE ' ' TO WS-MODIFIER
ELSE
MOVE 'F' TO WS-LETTER-GRADE
MOVE ' ' TO WS-MODIFIER
END-IF
END-IF
END-IF
END-IF
Requirements: 1. Use a single EVALUATE TRUE with appropriate WHEN clauses 2. Handle the +/- modifiers for A and B grades 3. Ensure the WHEN clauses are ordered correctly
Exercise 6.8: Debugging Conditional Logic (Advanced)
The following program has three bugs in its conditional logic. Find and fix each one, explaining what the bug is and what symptoms it would produce in production.
WORKING-STORAGE SECTION.
01 WS-ORDER-STATUS PIC X(01).
88 ORDER-NEW VALUE 'N'.
88 ORDER-PROCESSING VALUE 'P'.
88 ORDER-SHIPPED VALUE 'S'.
88 ORDER-DELIVERED VALUE 'D'.
88 ORDER-CANCELLED VALUE 'C'.
88 ORDER-IS-ACTIVE VALUE 'N' 'P' 'S'.
01 WS-PAYMENT-STATUS PIC X(01).
88 PAYMENT-PENDING VALUE 'P'.
88 PAYMENT-RECEIVED VALUE 'R'.
88 PAYMENT-REFUNDED VALUE 'F'.
01 WS-ITEM-QTY PIC 9(05).
01 WS-ITEM-PRICE PIC 9(05)V99.
01 WS-ORDER-TOTAL PIC 9(07)V99.
01 WS-DISCOUNT-PCT PIC 9(02)V99.
01 WS-DISCOUNT-AMT PIC 9(07)V99.
PROCEDURE DIVISION.
1000-MAIN.
PERFORM 2000-PROCESS-ORDER
STOP RUN.
2000-PROCESS-ORDER.
* Bug 1 is in this section
IF ORDER-IS-ACTIVE OR ORDER-CANCELLED
COMPUTE WS-ORDER-TOTAL =
WS-ITEM-QTY * WS-ITEM-PRICE
END-IF
* Bug 2 is in this section
EVALUATE WS-ORDER-TOTAL
WHEN 0 THRU 99.99
MOVE 0 TO WS-DISCOUNT-PCT
WHEN 100 THRU 499.99
MOVE 5.00 TO WS-DISCOUNT-PCT
WHEN 500 THRU 999.99
MOVE 10.00 TO WS-DISCOUNT-PCT
WHEN 1000 THRU 9999.99
MOVE 15.00 TO WS-DISCOUNT-PCT
END-EVALUATE
* Bug 3 is in this section
IF WS-DISCOUNT-PCT NOT = 0
OR WS-ORDER-TOTAL > 0
COMPUTE WS-DISCOUNT-AMT =
WS-ORDER-TOTAL * WS-DISCOUNT-PCT / 100
END-IF
.
For each bug: 1. Identify the exact line(s) containing the bug 2. Explain what the code actually does versus what was intended 3. Describe the production symptoms (wrong output, abend, etc.) 4. Provide the corrected code