Nested control structures in COBOL allow you to create more complex and powerful logic by embedding one control structure within another. This is particularly useful for handling multi-level decision-making processes and repetitive tasks that depend on multiple conditions.
Key Concepts
- Nested IF Statements: Placing one IF statement inside another to handle multiple conditions.
- Nested PERFORM Loops: Using one PERFORM loop inside another to manage complex iterations.
- Combining IF and PERFORM: Integrating conditional statements within loops for more dynamic control.
Nested IF Statements
Nested IF statements are used when you need to check multiple conditions in a hierarchical manner. Here’s a basic example:
IF condition-1 IF condition-2 PERFORM action-1 ELSE PERFORM action-2 END-IF ELSE PERFORM action-3 END-IF
Example
Consider a scenario where you need to determine the grade of a student based on their score:
DATA DIVISION. WORKING-STORAGE SECTION. 01 STUDENT-SCORE PIC 9(3). 01 STUDENT-GRADE PIC X(1). PROCEDURE DIVISION. MOVE 85 TO STUDENT-SCORE. IF STUDENT-SCORE >= 90 MOVE 'A' TO STUDENT-GRADE ELSE IF STUDENT-SCORE >= 80 MOVE 'B' TO STUDENT-GRADE ELSE IF STUDENT-SCORE >= 70 MOVE 'C' TO STUDENT-GRADE ELSE MOVE 'F' TO STUDENT-GRADE END-IF END-IF END-IF. DISPLAY 'Student Grade: ' STUDENT-GRADE. STOP RUN.
Explanation
- The outermost IF checks if the score is 90 or above.
- If not, the next IF checks if the score is 80 or above.
- This continues until all conditions are checked, and the appropriate grade is assigned.
Nested PERFORM Loops
Nested PERFORM loops are used to handle multi-level iterations. This is useful for tasks like processing multi-dimensional arrays or performing operations on nested data structures.
Example
Consider a scenario where you need to print a multiplication table:
DATA DIVISION. WORKING-STORAGE SECTION. 01 I PIC 9 VALUE 1. 01 J PIC 9 VALUE 1. PROCEDURE DIVISION. PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10 PERFORM VARYING J FROM 1 BY 1 UNTIL J > 10 DISPLAY I ' * ' J ' = ' I * J END-PERFORM END-PERFORM. STOP RUN.
Explanation
- The outer PERFORM loop iterates from 1 to 10.
- For each iteration of the outer loop, the inner PERFORM loop also iterates from 1 to 10.
- The DISPLAY statement prints the product of the current values of I and J.
Combining IF and PERFORM
You can combine IF statements within PERFORM loops to create more dynamic and conditional iterations.
Example
Consider a scenario where you need to print even numbers from 1 to 20:
DATA DIVISION. WORKING-STORAGE SECTION. 01 I PIC 9 VALUE 1. PROCEDURE DIVISION. PERFORM VARYING I FROM 1 BY 1 UNTIL I > 20 IF I MOD 2 = 0 DISPLAY I END-IF END-PERFORM. STOP RUN.
Explanation
- The PERFORM loop iterates from 1 to 20.
- The IF statement checks if the current value of I is even.
- If the condition is true, the number is displayed.
Practical Exercise
Task
Write a COBOL program that processes a list of student scores and assigns grades based on the following criteria:
- A: 90 and above
- B: 80 to 89
- C: 70 to 79
- D: 60 to 69
- F: below 60
Solution
DATA DIVISION. WORKING-STORAGE SECTION. 01 STUDENT-SCORES. 05 SCORE PIC 9(3) OCCURS 5 TIMES VALUE 0. 01 STUDENT-GRADES. 05 GRADE PIC X OCCURS 5 TIMES VALUE SPACE. 01 I PIC 9 VALUE 1. PROCEDURE DIVISION. MOVE 95 TO SCORE(1). MOVE 82 TO SCORE(2). MOVE 76 TO SCORE(3). MOVE 65 TO SCORE(4). MOVE 50 TO SCORE(5). PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5 IF SCORE(I) >= 90 MOVE 'A' TO GRADE(I) ELSE IF SCORE(I) >= 80 MOVE 'B' TO GRADE(I) ELSE IF SCORE(I) >= 70 MOVE 'C' TO GRADE(I) ELSE IF SCORE(I) >= 60 MOVE 'D' TO GRADE(I) ELSE MOVE 'F' TO GRADE(I) END-IF END-IF END-IF END-IF END-PERFORM. PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5 DISPLAY 'Student ' I ' Score: ' SCORE(I) ' Grade: ' GRADE(I) END-PERFORM. STOP RUN.
Explanation
- The program initializes an array of student scores.
- It then iterates through the scores, assigning grades based on the specified criteria.
- Finally, it displays each student's score and corresponding grade.
Conclusion
Nested control structures in COBOL provide a powerful way to handle complex logic and repetitive tasks. By mastering nested IF statements and PERFORM loops, you can create more efficient and readable COBOL programs. Practice these concepts with various scenarios to become proficient in using nested control structures.