Conditional logic controls program flow based on conditions. Learn decision-making patterns, control structures, and best practices for writing clear, maintainable conditional code.
123456789101112*> Single condition with action IF CUSTOMER-BALANCE > 0 DISPLAY 'Customer has positive balance' PERFORM PROCESS-POSITIVE-BALANCE END-IF. *> Condition with alternative action IF TRANSACTION-TYPE = 'CREDIT' ADD TRANSACTION-AMOUNT TO ACCOUNT-BALANCE ELSE SUBTRACT TRANSACTION-AMOUNT FROM ACCOUNT-BALANCE END-IF.
Start with simple patterns: single conditions with actions, and conditions with alternative actions. Keep the logic straightforward and easy to follow. Use meaningful variable names that make the business logic clear.
1234567891011121314151617*> Multiple conditions with AND IF CUSTOMER-AGE >= 18 AND CUSTOMER-INCOME > 30000 DISPLAY 'Eligible for premium account' PERFORM SETUP-PREMIUM-ACCOUNT ELSE DISPLAY 'Standard account only' PERFORM SETUP-STANDARD-ACCOUNT END-IF. *> Multiple conditions with OR IF ACCOUNT-TYPE = 'SAVINGS' OR ACCOUNT-TYPE = 'CHECKING' DISPLAY 'Valid account type' PERFORM PROCESS-ACCOUNT ELSE DISPLAY 'Invalid account type' PERFORM HANDLE-ERROR END-IF.
Combine conditions using AND and OR operators. AND requires all conditions to be true, OR requires at least one to be true. Use parentheses to clarify evaluation order when mixing operators.
123456789101112131415IF CUSTOMER-TYPE = 'INDIVIDUAL' IF CUSTOMER-AGE >= 65 DISPLAY 'Senior individual customer' PERFORM PROCESS-SENIOR-INDIVIDUAL ELSE IF CUSTOMER-AGE >= 18 DISPLAY 'Adult individual customer' PERFORM PROCESS-ADULT-INDIVIDUAL ELSE DISPLAY 'Minor individual customer' PERFORM PROCESS-MINOR-INDIVIDUAL END-IF ELSE IF CUSTOMER-TYPE = 'CORPORATE' DISPLAY 'Corporate customer' PERFORM PROCESS-CORPORATE END-IF.
Nest conditions when you need to make decisions based on multiple criteria. Proper indentation is crucial for readability. Consider using EVALUATE for complex nested logic to improve clarity.
12345678910111213141516*> Check error conditions first IF CUSTOMER-ID = 0 DISPLAY 'Error: Invalid customer ID' PERFORM ERROR-HANDLING EXIT PARAGRAPH END-IF. IF CUSTOMER-STATUS = 'CLOSED' DISPLAY 'Error: Customer account closed' PERFORM ERROR-HANDLING EXIT PARAGRAPH END-IF. *> Main processing logic DISPLAY 'Processing valid customer' PERFORM MAIN-PROCESSING.
Use early returns to handle error conditions first, reducing nesting levels. This makes the main logic clearer by eliminating error cases early. Use EXIT PARAGRAPH or EXIT SECTION to exit early.
12345678910111213141516EVALUATE TRUE WHEN CUSTOMER-TYPE = 'INDIVIDUAL' AND CUSTOMER-AGE >= 65 AND CUSTOMER-INCOME > 50000 PERFORM PROCESS-SENIOR-HIGH-INCOME WHEN CUSTOMER-TYPE = 'INDIVIDUAL' AND CUSTOMER-AGE >= 65 AND CUSTOMER-INCOME <= 50000 PERFORM PROCESS-SENIOR-LOW-INCOME WHEN CUSTOMER-TYPE = 'INDIVIDUAL' AND CUSTOMER-AGE < 65 PERFORM PROCESS-NON-SENIOR-INDIVIDUAL WHEN CUSTOMER-TYPE = 'CORPORATE' AND COMPANY-SIZE = 'LARGE' PERFORM PROCESS-LARGE-CORPORATE WHEN CUSTOMER-TYPE = 'CORPORATE' AND COMPANY-SIZE = 'SMALL' PERFORM PROCESS-SMALL-CORPORATE WHEN OTHER PERFORM PROCESS-UNKNOWN-CUSTOMER-TYPE END-EVALUATE.
Use EVALUATE TRUE for complex business logic with multiple criteria. This pattern makes all conditions visible at the same level and is easier to maintain than deeply nested IF statements.
1234567891011*> Conditional loop with condition check PERFORM UNTIL EOF OR ERROR-FLAG = 'Y' READ INPUT-FILE AT END SET EOF TO TRUE NOT AT END IF RECORD-TYPE = 'VALID' PERFORM PROCESS-VALID-RECORD ELSE DISPLAY 'Invalid record type: ' RECORD-TYPE SET ERROR-FLAG TO TRUE END-IF END-PERFORM.
Use conditional logic within loops to control processing based on data conditions. Check for both normal termination (EOF) and error conditions. This provides robust error handling while processing data.
1234567891011121314*> Validate input before processing IF CUSTOMER-ID IS NOT NUMERIC DISPLAY 'Error: Customer ID must be numeric' PERFORM ERROR-HANDLING ELSE IF CUSTOMER-ID = 0 DISPLAY 'Error: Customer ID cannot be zero' PERFORM ERROR-HANDLING ELSE IF CUSTOMER-ID > 999999 DISPLAY 'Error: Customer ID too large' PERFORM ERROR-HANDLING ELSE DISPLAY 'Valid customer ID: ' CUSTOMER-ID PERFORM PROCESS-CUSTOMER END-IF.
Use defensive programming to validate data before processing. Check for invalid values, ranges, and data types. This prevents errors and makes programs more robust and reliable.