MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Dynamic Programming

Dynamic programming in COBOL enables programs to adapt and allocate resources at runtime rather than at compile time. This includes dynamic tables with variable sizes, dynamic program calls, runtime flexibility in data structures, and adaptive behavior based on runtime conditions. Understanding dynamic programming is essential for building flexible COBOL applications that can handle variable amounts of data, adapt to different runtime scenarios, and efficiently use system resources in mainframe environments.

What is Dynamic Programming in COBOL?

Dynamic programming techniques allow COBOL programs to:

  • Vary table sizes: Adjust the number of table elements at runtime
  • Call programs dynamically: Select which programs to call based on runtime conditions
  • Adapt to data volumes: Handle variable amounts of input data
  • Optimize memory usage: Allocate only the memory needed
  • Make runtime decisions: Choose data structures and program flow at runtime

These capabilities make COBOL programs more flexible and efficient, especially when dealing with variable or unknown data volumes.

Dynamic Tables with DEPENDING ON

Dynamic tables use the DEPENDING ON clause to vary their size at runtime:

Dynamic Table Syntax

cobol
1
2
3
4
level-number data-name OCCURS 1 TO max-size TIMES DEPENDING ON size-field [INDEXED BY index-name].

Components:

  • 1 TO max-size: Defines the range of possible sizes (minimum 1, maximum max-size)
  • DEPENDING ON size-field: The numeric field that determines the active size
  • size-field: Must be a numeric data item (PIC 9)

Example: Basic Dynamic Table

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
WORKING-STORAGE SECTION. 01 TABLE-SIZE PIC 9(4) VALUE 0. 01 MAX-SIZE PIC 9(4) VALUE 1000. 01 CUSTOMER-TABLE. 05 CUSTOMER-ENTRY OCCURS 1 TO 1000 TIMES DEPENDING ON TABLE-SIZE INDEXED BY CUST-INDEX. 10 CUSTOMER-ID PIC 9(5). 10 CUSTOMER-NAME PIC X(30). 10 CUSTOMER-BALANCE PIC 9(8)V99. PROCEDURE DIVISION. MAIN-PARA. *> Read number of customers from input ACCEPT TABLE-SIZE *> Validate size IF TABLE-SIZE > MAX-SIZE DISPLAY "ERROR: Size exceeds maximum" STOP RUN END-IF IF TABLE-SIZE = 0 DISPLAY "No customers to process" STOP RUN END-IF *> Process the dynamic table PERFORM PROCESS-CUSTOMERS STOP RUN. PROCESS-CUSTOMERS. PERFORM VARYING CUST-INDEX FROM 1 BY 1 UNTIL CUST-INDEX > TABLE-SIZE *> Read customer data ACCEPT CUSTOMER-ID(CUST-INDEX) ACCEPT CUSTOMER-NAME(CUST-INDEX) ACCEPT CUSTOMER-BALANCE(CUST-INDEX) END-PERFORM.

This example demonstrates a dynamic table where the size is determined at runtime based on input. The table can hold up to 1000 elements, but only TABLE-SIZE elements are active.

Changing Table Size at Runtime

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
WORKING-STORAGE SECTION. 01 ITEM-COUNT PIC 9(4) VALUE 50. 01 ITEM-TABLE. 05 ITEM-ENTRY OCCURS 1 TO 500 TIMES DEPENDING ON ITEM-COUNT INDEXED BY ITEM-INDEX. 10 ITEM-CODE PIC 9(5). 10 ITEM-NAME PIC X(30). PROCEDURE DIVISION. MAIN-PARA. *> Start with 50 items MOVE 50 TO ITEM-COUNT PERFORM PROCESS-ITEMS *> Add more items ADD 25 TO ITEM-COUNT PERFORM PROCESS-ITEMS *> Reduce items SUBTRACT 10 FROM ITEM-COUNT PERFORM PROCESS-ITEMS STOP RUN. PROCESS-ITEMS. DISPLAY "Processing " ITEM-COUNT " items" PERFORM VARYING ITEM-INDEX FROM 1 BY 1 UNTIL ITEM-INDEX > ITEM-COUNT *> Process ITEM-ENTRY(ITEM-INDEX) END-PERFORM.

You can change the table size by modifying the DEPENDING ON field. The table adapts to the new size immediately.

Validating Dynamic Table Sizes

Always validate dynamic table sizes to prevent errors:

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
WORKING-STORAGE SECTION. 01 TABLE-COUNT PIC 9(4) VALUE 0. 01 MAX-TABLE-SIZE PIC 9(4) VALUE 1000. 01 MIN-TABLE-SIZE PIC 9(4) VALUE 1. 01 CUSTOMER-TABLE. 05 CUSTOMER-ENTRY OCCURS 1 TO 1000 TIMES DEPENDING ON TABLE-COUNT INDEXED BY CUST-INDEX. 10 CUSTOMER-DATA PIC X(50). PROCEDURE DIVISION. MAIN-PARA. *> Get table size from input ACCEPT TABLE-COUNT *> Validate size IF TABLE-COUNT < MIN-TABLE-SIZE DISPLAY "ERROR: Size too small: " TABLE-COUNT MOVE MIN-TABLE-SIZE TO TABLE-COUNT DISPLAY "Using minimum size: " TABLE-COUNT END-IF IF TABLE-COUNT > MAX-TABLE-SIZE DISPLAY "ERROR: Size exceeds maximum: " TABLE-COUNT DISPLAY "Maximum allowed: " MAX-TABLE-SIZE MOVE MAX-TABLE-SIZE TO TABLE-COUNT DISPLAY "Using maximum size: " TABLE-COUNT END-IF IF TABLE-COUNT = 0 DISPLAY "WARNING: Table size is zero" DISPLAY "No processing will occur" END-IF *> Now safe to use table IF TABLE-COUNT > 0 PERFORM PROCESS-TABLE END-IF STOP RUN.

Validation ensures the table size is within acceptable limits and prevents runtime errors from invalid subscripts or indices.

Dynamic Program Calls

Dynamic program calls allow selecting which program to call at runtime:

Static vs Dynamic Calls

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
*> Static call (compile-time program name) CALL "CALCULATE-TAX" USING AMOUNT TAX-RATE TAX-AMOUNT END-CALL *> Dynamic call (runtime program name) WORKING-STORAGE SECTION. 01 PROGRAM-NAME PIC X(30) VALUE 'CALCULATE-TAX'. PROCEDURE DIVISION. MAIN-PARA. *> Call program using variable CALL PROGRAM-NAME USING AMOUNT TAX-RATE TAX-AMOUNT ON EXCEPTION DISPLAY "Program " PROGRAM-NAME " not found" END-CALL.

Dynamic calls use a variable for the program name, allowing runtime selection of which program to execute.

Example: Selecting Program Based on Condition

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
WORKING-STORAGE SECTION. 01 CALCULATION-TYPE PIC X(10). 01 PROGRAM-NAME PIC X(30). 01 AMOUNT PIC 9(8)V99. 01 RESULT PIC 9(8)V99. PROCEDURE DIVISION. MAIN-PARA. ACCEPT CALCULATION-TYPE ACCEPT AMOUNT *> Select program based on type EVALUATE CALCULATION-TYPE WHEN 'TAX' MOVE 'CALCULATE-TAX' TO PROGRAM-NAME WHEN 'DISCOUNT' MOVE 'CALCULATE-DISCOUNT' TO PROGRAM-NAME WHEN 'COMMISSION' MOVE 'CALCULATE-COMMISSION' TO PROGRAM-NAME WHEN OTHER DISPLAY "Unknown calculation type" STOP RUN END-EVALUATE *> Call selected program dynamically CALL PROGRAM-NAME USING AMOUNT RETURNING RESULT ON EXCEPTION DISPLAY "ERROR: Program " PROGRAM-NAME " not found" END-CALL DISPLAY "Result: " RESULT STOP RUN.

This example demonstrates selecting which calculation program to call based on runtime input, providing flexibility in program execution.

Dynamic Record Processing

Dynamic programming enables handling variable-length records and variable numbers of items:

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
WORKING-STORAGE SECTION. 01 RECORD-COUNT PIC 9(6) VALUE 0. 01 MAX-RECORDS PIC 9(6) VALUE 10000. 01 TRANSACTION-TABLE. 05 TRANSACTION-ENTRY OCCURS 1 TO 10000 TIMES DEPENDING ON RECORD-COUNT INDEXED BY TRANS-INDEX. 10 TRANS-DATE PIC 9(8). 10 TRANS-AMOUNT PIC 9(8)V99. 10 TRANS-DESCRIPTION PIC X(50). PROCEDURE DIVISION. MAIN-PARA. *> Read records until end of file PERFORM UNTIL END-OF-FILE READ INPUT-FILE AT END SET END-OF-FILE TO TRUE NOT AT END ADD 1 TO RECORD-COUNT IF RECORD-COUNT > MAX-RECORDS DISPLAY "WARNING: Exceeded maximum records" SET END-OF-FILE TO TRUE ELSE MOVE INPUT-RECORD TO TRANSACTION-ENTRY(RECORD-COUNT) END-IF END-READ END-PERFORM *> Process only the records actually read DISPLAY "Processing " RECORD-COUNT " transactions" PERFORM PROCESS-TRANSACTIONS STOP RUN. PROCESS-TRANSACTIONS. PERFORM VARYING TRANS-INDEX FROM 1 BY 1 UNTIL TRANS-INDEX > RECORD-COUNT *> Process TRANSACTION-ENTRY(TRANS-INDEX) END-PERFORM.

This pattern allows processing variable numbers of records, adapting to the actual data volume encountered at runtime.

Best Practices for Dynamic Programming

Follow these best practices:

  • Always validate sizes: Check DEPENDING ON fields before using dynamic tables
  • Set size before use: Initialize DEPENDING ON fields before accessing table elements
  • Handle zero sizes: Check for zero or negative sizes to avoid errors
  • Validate maximums: Ensure sizes don't exceed maximum defined in OCCURS
  • Document dynamic behavior: Comment code to explain dynamic sizing logic
  • Handle exceptions: Use ON EXCEPTION for dynamic program calls
  • Test boundary conditions: Test with minimum, maximum, and zero sizes
  • Consider performance: Frequent resizing may impact performance
  • Use meaningful names: Name DEPENDING ON fields descriptively
  • Initialize properly: Set initial values for dynamic fields before use

Common Dynamic Programming Patterns

Pattern 1: Variable Input Processing

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
*> Process variable number of input items MOVE 0 TO ITEM-COUNT PERFORM UNTIL END-OF-INPUT READ INPUT-FILE AT END SET END-OF-INPUT TO TRUE NOT AT END ADD 1 TO ITEM-COUNT IF ITEM-COUNT <= MAX-ITEMS MOVE INPUT-RECORD TO ITEM-TABLE(ITEM-COUNT) END-IF END-READ END-PERFORM *> Process items actually read IF ITEM-COUNT > 0 PERFORM PROCESS-ITEMS END-IF

Pattern 2: Runtime Program Selection

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
*> Select program based on runtime condition EVALUATE PROCESSING-TYPE WHEN 'TYPE-A' MOVE 'PROCESS-TYPE-A' TO PROGRAM-NAME WHEN 'TYPE-B' MOVE 'PROCESS-TYPE-B' TO PROGRAM-NAME WHEN OTHER MOVE 'PROCESS-DEFAULT' TO PROGRAM-NAME END-EVALUATE CALL PROGRAM-NAME USING PROCESSING-DATA ON EXCEPTION PERFORM HANDLE-PROGRAM-ERROR END-CALL

Pattern 3: Adaptive Table Sizing

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
*> Start with estimated size MOVE ESTIMATED-SIZE TO TABLE-COUNT *> Process and adjust as needed PERFORM PROCESS-DATA *> If more space needed, increase IF NEED-MORE-SPACE IF TABLE-COUNT < MAX-SIZE ADD INCREMENT TO TABLE-COUNT END-IF END-IF *> If too much space, reduce IF HAVE-EXCESS-SPACE SUBTRACT DECREMENT FROM TABLE-COUNT END-IF

Explain Like I'm 5: Dynamic Programming

Think of dynamic programming like a flexible container:

  • Fixed-size table is like a box with a fixed number of slots - you always have the same number of slots, even if you don't use them all
  • Dynamic table is like a box where you can change how many slots you use - you decide at runtime based on how many items you have
  • DEPENDING ON is like a dial that controls how many slots are active - you turn the dial to change the size
  • Dynamic program calls is like choosing which tool to use based on what you need - you pick the tool at runtime
  • Validation is like checking that you don't try to use more slots than the box can hold

So dynamic programming is like having flexible containers and tools that you can adjust based on what you need, rather than having everything fixed in advance!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Basic Dynamic Table

Create a dynamic table that can hold 1 to 100 items. Read a number from input, set the table size, then read that many items into the table and display them.

Exercise 2: Resizing Table

Create a dynamic table and demonstrate resizing it multiple times during program execution. Start with 10 items, add 20 more, then reduce by 5, displaying the count at each step.

Exercise 3: Dynamic Program Call

Create a program that accepts a calculation type (ADD, SUBTRACT, MULTIPLY, DIVIDE) and dynamically calls the appropriate calculation program based on the input.

Exercise 4: Variable Record Processing

Create a program that reads a variable number of records from a file into a dynamic table, then processes only the records actually read. Handle the case where no records are found.

Exercise 5: Validation and Error Handling

Create a dynamic table with proper validation: check for zero size, maximum size, and handle errors appropriately. Include error messages and default behaviors.

Test Your Knowledge

1. What clause makes a table dynamic in COBOL?

  • DYNAMIC clause
  • DEPENDING ON clause
  • VARIABLE clause
  • RUNTIME clause

2. How do you change a dynamic table size at runtime?

  • Use RESIZE statement
  • Modify the DEPENDING ON field
  • Use ALLOCATE statement
  • You cannot change it

3. What is required for dynamic program calls?

  • Static program name
  • Program name in a variable
  • CALL BY VALUE
  • ON EXCEPTION clause

4. What is the maximum size of a dynamic table determined by?

  • Runtime value
  • The DEPENDING ON field
  • The maximum in OCCURS clause (compile time)
  • Available memory

5. What should you do before using a dynamic table?

  • Initialize all elements
  • Set the DEPENDING ON field to the desired size
  • Sort the table
  • Set an index to 1

6. What is a key advantage of dynamic programming?

  • Faster execution
  • Flexibility to handle variable amounts of data at runtime
  • Smaller program size
  • Easier syntax

Related Concepts

Related Pages