The ELSE clause in COBOL provides an alternative execution path when the condition in an IF statement is false. ELSE is an essential component of conditional logic, allowing programs to handle both true and false cases explicitly. Understanding ELSE is fundamental to writing complete conditional logic in COBOL programs.
ELSE makes conditional logic complete by ensuring that one path always executes - either the IF path when the condition is true, or the ELSE path when the condition is false. This is crucial for handling all possible scenarios and ensuring programs behave correctly in all cases.
The ELSE clause:
12345IF condition statements-for-true-case ELSE statements-for-false-case END-IF
When the condition is true, statements between IF and ELSE execute. When false, statements between ELSE and END-IF execute.
1234567891011121314WORKING-STORAGE SECTION. 01 AMOUNT PIC 9(5). PROCEDURE DIVISION. MAIN-LOGIC. IF AMOUNT > 1000 DISPLAY "Large amount: " AMOUNT PERFORM PROCESS-LARGE-AMOUNT ELSE DISPLAY "Small amount: " AMOUNT PERFORM PROCESS-SMALL-AMOUNT END-IF STOP RUN.
This example demonstrates basic ELSE usage - handling both large and small amounts explicitly.
1234567891011121314151617181920WORKING-STORAGE SECTION. 01 STATUS-FLAG PIC X(1). 01 PROCESS-TYPE PIC X(1). PROCEDURE DIVISION. MAIN-LOGIC. IF STATUS-FLAG = "A" IF PROCESS-TYPE = "B" DISPLAY "Active batch processing" PERFORM ACTIVE-BATCH ELSE DISPLAY "Active non-batch processing" PERFORM ACTIVE-NON-BATCH END-IF ELSE DISPLAY "Inactive status" PERFORM INACTIVE-PROCESSING END-IF STOP RUN.
Nested IF statements can each have their own ELSE clauses. Proper indentation and END-IF matching is crucial.
ELSE must always be paired with an IF statement. You cannot use ELSE alone. Every ELSE must have a corresponding IF before it.
12345678910PROCEDURE DIVISION. MAIN-LOGIC. IF AMOUNT > 0 DISPLAY "Positive amount: " AMOUNT PERFORM PROCESS-POSITIVE END-IF *> If AMOUNT <= 0, nothing happens - execution continues DISPLAY "Processing complete" STOP RUN.
IF without ELSE is valid when you only need to handle the true case. The false case results in no action.
123456789101112131415161718192021222324252627282930IDENTIFICATION DIVISION. PROGRAM-ID. ELSE-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-BALANCE PIC S9(8)V99. 01 TRANSACTION-AMOUNT PIC S9(7)V99. 01 NEW-BALANCE PIC S9(8)V99. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== Account Transaction Processing ===" MOVE 1000.00 TO CUSTOMER-BALANCE MOVE -500.00 TO TRANSACTION-AMOUNT ADD TRANSACTION-AMOUNT TO CUSTOMER-BALANCE GIVING NEW-BALANCE IF NEW-BALANCE >= 0 DISPLAY "Transaction approved" DISPLAY "New balance: $" NEW-BALANCE MOVE NEW-BALANCE TO CUSTOMER-BALANCE ELSE DISPLAY "Transaction declined - insufficient funds" DISPLAY "Current balance: $" CUSTOMER-BALANCE DISPLAY "Transaction amount: $" FUNCTION ABS(TRANSACTION-AMOUNT) DISPLAY "Required balance: $" FUNCTION ABS(NEW-BALANCE) END-IF STOP RUN.
This complete example demonstrates ELSE in a practical account transaction scenario, handling both approved and declined transactions.
Think of ELSE like choosing between two paths:
So ELSE is like having a backup plan - if the first condition isn't true, you do something else!
1. What does the ELSE clause do?
2. Is ELSE required in an IF statement?
3. What happens if IF condition is false and there's no ELSE?
4. Can you nest IF statements with ELSE?
5. Where does ELSE appear in an IF statement?
6. What is the relationship between ELSE and WHEN OTHER?