MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Decision Tables

Decision tables in COBOL are structured approaches to handling complex decision logic involving multiple conditions and multiple actions. While COBOL doesn't have built-in decision table syntax, decision tables are implemented using EVALUATE statements, nested IF structures, or table-driven logic. Decision tables organize complex business rules into a clear, tabular format that maps conditions to actions, making complex logic more maintainable, testable, and understandable.

This comprehensive guide will teach you how to implement decision tables using EVALUATE with multiple conditions, create table-driven decision logic, organize complex business rules, handle all condition combinations, and follow best practices for maintainable decision table implementations. Whether you're implementing pricing rules, approval workflows, or complex business logic, mastering decision tables is essential for managing complex conditional logic in COBOL programs.

What are Decision Tables?

Decision tables organize complex decision logic into a structured format:

  • Conditions: The factors being evaluated
  • Condition Values: The specific values for each condition
  • Actions: The possible actions to take
  • Rules: Mappings of condition combinations to actions

Decision tables make complex logic:

  • Clear: Easy to see which actions correspond to which conditions
  • Complete: Ensures all condition combinations are considered
  • Maintainable: Easy to add, modify, or remove rules
  • Testable: Systematic testing of all combinations

Implementing Decision Tables with EVALUATE

EVALUATE with ALSO is ideal for implementing decision tables as it can evaluate multiple conditions simultaneously.

Simple Decision Table Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
WORKING-STORAGE SECTION. 01 CUSTOMER-TYPE PIC X(1). 88 REGULAR-CUSTOMER VALUE 'R'. 88 PREMIUM-CUSTOMER VALUE 'P'. 01 ORDER-AMOUNT PIC 9(6)V99. 01 DISCOUNT-RATE PIC 9(2)V99. PROCEDURE DIVISION. MAIN-LOGIC. *> Decision table: Customer type + Order amount -> Discount rate EVALUATE TRUE ALSO ORDER-AMOUNT WHEN REGULAR-CUSTOMER ALSO LESS THAN 100.00 MOVE 0.00 TO DISCOUNT-RATE DISPLAY "No discount for regular customer, small order" WHEN REGULAR-CUSTOMER ALSO GREATER THAN 1000.00 MOVE 5.00 TO DISCOUNT-RATE DISPLAY "5% discount for regular customer, large order" WHEN PREMIUM-CUSTOMER ALSO LESS THAN 100.00 MOVE 2.00 TO DISCOUNT-RATE DISPLAY "2% discount for premium customer, small order" WHEN PREMIUM-CUSTOMER ALSO GREATER THAN 1000.00 MOVE 10.00 TO DISCOUNT-RATE DISPLAY "10% discount for premium customer, large order" WHEN OTHER MOVE 0.00 TO DISCOUNT-RATE DISPLAY "Default: No discount" END-EVALUATE STOP RUN.

This example demonstrates a decision table mapping customer type and order amount combinations to discount rates.

Complex Decision Table Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
IDENTIFICATION DIVISION. PROGRAM-ID. DECISION-TABLE-EXAMPLE. AUTHOR. MainframeMaster Tutorial. DATA DIVISION. WORKING-STORAGE SECTION. 01 LOAN-APPLICATION. 05 CREDIT-SCORE PIC 9(3). 05 INCOME-LEVEL PIC X(1). 88 LOW-INCOME VALUE 'L'. 88 MEDIUM-INCOME VALUE 'M'. 88 HIGH-INCOME VALUE 'H'. 05 EMPLOYMENT-STATUS PIC X(1). 88 EMPLOYED VALUE 'E'. 88 SELF-EMPLOYED VALUE 'S'. 88 UNEMPLOYED VALUE 'U'. 05 LOAN-DECISION PIC X(10). 05 INTEREST-RATE PIC 9(2)V99. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== Loan Approval Decision Table ===" *> Initialize test data MOVE 750 TO CREDIT-SCORE SET HIGH-INCOME TO TRUE SET EMPLOYED TO TRUE *> Decision table: Credit score + Income + Employment -> Decision + Rate EVALUATE CREDIT-SCORE ALSO TRUE ALSO TRUE WHEN GREATER THAN 700 ALSO HIGH-INCOME ALSO EMPLOYED MOVE "APPROVED" TO LOAN-DECISION MOVE 3.50 TO INTEREST-RATE DISPLAY "Decision: " LOAN-DECISION " Rate: " INTEREST-RATE "%" WHEN GREATER THAN 700 ALSO HIGH-INCOME ALSO SELF-EMPLOYED MOVE "APPROVED" TO LOAN-DECISION MOVE 4.00 TO INTEREST-RATE DISPLAY "Decision: " LOAN-DECISION " Rate: " INTEREST-RATE "%" WHEN GREATER THAN 700 ALSO MEDIUM-INCOME ALSO EMPLOYED MOVE "APPROVED" TO LOAN-DECISION MOVE 4.50 TO INTEREST-RATE DISPLAY "Decision: " LOAN-DECISION " Rate: " INTEREST-RATE "%" WHEN GREATER THAN 650 ALSO HIGH-INCOME ALSO EMPLOYED MOVE "APPROVED" TO LOAN-DECISION MOVE 5.00 TO INTEREST-RATE DISPLAY "Decision: " LOAN-DECISION " Rate: " INTEREST-RATE "%" WHEN GREATER THAN 600 ALSO MEDIUM-INCOME ALSO EMPLOYED MOVE "CONDITIONAL" TO LOAN-DECISION MOVE 6.00 TO INTEREST-RATE DISPLAY "Decision: " LOAN-DECISION " Rate: " INTEREST-RATE "%" WHEN OTHER MOVE "DENIED" TO LOAN-DECISION MOVE 0.00 TO INTEREST-RATE DISPLAY "Decision: " LOAN-DECISION END-EVALUATE STOP RUN.

This complex example demonstrates a decision table for loan approval with multiple conditions (credit score, income, employment) mapping to decisions and interest rates.

Table-Driven Decision Tables

Table-driven approaches store decision rules in data structures, allowing rules to be modified without code changes.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
WORKING-STORAGE SECTION. 01 DECISION-RULES. 05 RULE-TABLE OCCURS 10 TIMES INDEXED BY RULE-INDEX. 10 RULE-CONDITION-1 PIC X(1). 10 RULE-CONDITION-2 PIC X(1). 10 RULE-ACTION PIC X(20). 01 CURRENT-CONDITION-1 PIC X(1). 01 CURRENT-CONDITION-2 PIC X(1). 01 SELECTED-ACTION PIC X(20). 01 RULE-FOUND-FLAG PIC X(1) VALUE 'N'. 88 RULE-FOUND VALUE 'Y'. PROCEDURE DIVISION. MAIN-LOGIC. *> Initialize decision rules table MOVE 'A' TO RULE-CONDITION-1(1) MOVE 'X' TO RULE-CONDITION-2(1) MOVE "Action for A+X" TO RULE-ACTION(1) MOVE 'B' TO RULE-CONDITION-1(2) MOVE 'Y' TO RULE-CONDITION-2(2) MOVE "Action for B+Y" TO RULE-ACTION(2) *> Look up rule MOVE 'A' TO CURRENT-CONDITION-1 MOVE 'X' TO CURRENT-CONDITION-2 PERFORM VARYING RULE-INDEX FROM 1 BY 1 UNTIL RULE-INDEX > 10 OR RULE-FOUND IF RULE-CONDITION-1(RULE-INDEX) = CURRENT-CONDITION-1 AND RULE-CONDITION-2(RULE-INDEX) = CURRENT-CONDITION-2 MOVE RULE-ACTION(RULE-INDEX) TO SELECTED-ACTION SET RULE-FOUND TO TRUE END-IF END-PERFORM IF RULE-FOUND DISPLAY "Selected action: " SELECTED-ACTION ELSE DISPLAY "No matching rule found" END-IF STOP RUN.

Table-driven approaches allow rules to be loaded from external sources or modified without recompiling code.

Best Practices for Decision Tables

  • Use EVALUATE with ALSO: For multi-condition decision tables
  • Use condition names: Make conditions readable with 88-level items
  • Organize rules logically: Most common first, or by priority
  • Always include WHEN OTHER: Handle unmatched combinations
  • Document decision logic: Comment complex decision tables
  • Test all combinations: Ensure all rules work correctly
  • Keep focused: Each decision table should handle one decision point
  • Consider table-driven: For frequently changing rules

Explain Like I'm 5: Decision Tables

Think of decision tables like a menu at a restaurant:

  • Conditions are like "Are you hungry?" and "Do you like spicy food?"
  • Rules are like "If hungry AND like spicy → order spicy dish"
  • Actions are like "Order pizza" or "Order salad"

So decision tables are like a smart menu that helps you decide what to order based on your preferences!

Test Your Knowledge

1. What are decision tables used for in COBOL?

  • Storing data
  • Handling complex decision logic with multiple conditions and actions
  • Reading files
  • Performing calculations

2. What COBOL statement is best for implementing decision tables?

  • IF
  • EVALUATE with ALSO
  • PERFORM
  • MOVE

3. What should you always include in a decision table implementation?

  • Multiple files
  • WHEN OTHER for unmatched cases
  • Arithmetic operations
  • File I/O

4. What are the main components of a decision table?

  • Files and records
  • Conditions, condition values, actions, and action selections
  • Variables and constants
  • Procedures and functions

5. What is a table-driven decision table approach?

  • Using EVALUATE statements
  • Storing decision rules in data structures and looking them up at runtime
  • Using nested IF statements
  • Hardcoding all rules

Related Concepts

Related Pages