Lesson 6
Combining Conditional Logic with PERFORM
Introduction to Combining Conditional Logic with PERFORM

Welcome back! In the previous lessons, you learned how to create decision paths using IF statements and EVALUATE statements in COBOL. You also explored how to use the PERFORM statement to create loops. Now, we will elevate your coding skills by combining these two powerful concepts: conditional logic and loops.

Understanding how to integrate IF statements within PERFORM loops will help you create more dynamic and responsive COBOL programs. This skill is crucial, especially for applications in the banking industry, where multiple conditions and iterations often occur.

What You'll Learn

In this lesson, you'll learn how to create loops with embedded conditional logic using the PERFORM statement. Specifically, you will:

  • Understand how to use PERFORM with loops and IF statements.
  • Execute tasks conditionally within a loop.
  • Improve efficiency in handling repetitive tasks with conditions.

Here's a snippet of COBOL code demonstrating the combination of loop and conditional logic:

cobol
1IDENTIFICATION DIVISION. 2PROGRAM-ID. PerformWithIfDemo. 3DATA DIVISION. 4WORKING-STORAGE SECTION. 5 601 WS-INDEX PIC 99 VALUE 1. 701 WS-REMAINDER PIC 9. 801 WS-TEMP PIC 99. 9 10PROCEDURE DIVISION. 11 PERFORM VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX > 10 12 DIVIDE WS-INDEX BY 2 GIVING WS-TEMP REMAINDER WS-REMAINDER 13 IF WS-REMAINDER = 0 THEN 14 DISPLAY "Number " WS-INDEX " is EVEN" 15 ELSE 16 DISPLAY "Number " WS-INDEX " is ODD" 17 END-IF 18 END-PERFORM 19 STOP RUN.

Note that here we use PIC 99, which is another way to define a variable as a two-digit number using PIC 9(2).

Key Components of the Code

In the provided example:

  1. Loop Initialization: The loop starts with WS-INDEX set to 1.
  2. Condition Check: The loop continues to run until WS-INDEX is greater than 10.
  3. Iteration: For each iteration, WS-INDEX is incremented by 1.
  4. Conditional Logic: Inside the loop, the code checks if WS-INDEX is even or odd by dividing it by 2 and examining the remainder.
  5. Output: Based on the condition, it displays whether WS-INDEX is even or odd.

This example illustrates how you can perform different actions based on conditions within a loop, making your programs more versatile and efficient.

Why It Matters

Combining loops with conditional logic offers several key benefits:

  1. Enhanced Functionality: You can execute more complex operations by handling different conditions within each iteration of the loop.
  2. Efficiency in Coding: Instead of writing separate code blocks for different conditions, you can manage everything within a single loop, making your programs more streamlined.
  3. Dynamic Problem Solving: By handling multiple conditions dynamically, you can create more responsive and adaptable programs, crucial for real-world applications like banking systems.

Excited to dive deeper? Let’s move on to the practice section and master combining conditional logic with loops in COBOL!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.