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
PERFORMwith loops andIFstatements. - 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:
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:
- Loop Initialization: The loop starts with
WS-INDEXset to 1. - Condition Check: The loop continues to run until
WS-INDEXis greater than 10. - Iteration: For each iteration,
WS-INDEXis incremented by 1. - Conditional Logic: Inside the loop, the code checks if
WS-INDEXis even or odd by dividing it by 2 and examining the remainder. - Output: Based on the condition, it displays whether
WS-INDEXis 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.
