MainframeMaster

COBOL Tutorial

COBOL ELSE Clause

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.

Understanding ELSE Clause

The ELSE clause:

  • Provides alternative execution: Executes when the IF condition is false
  • Completes conditional logic: Ensures one path always executes
  • Handles false cases: Allows explicit handling of when conditions aren't met
  • Is optional: Not required but recommended when you need to handle false cases

Basic ELSE Syntax

cobol
1
2
3
4
5
IF 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.

Simple ELSE Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
WORKING-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.

ELSE with Nested IF

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WORKING-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 Without IF (Not Allowed)

ELSE must always be paired with an IF statement. You cannot use ELSE alone. Every ELSE must have a corresponding IF before it.

IF Without ELSE

cobol
1
2
3
4
5
6
7
8
9
10
PROCEDURE 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.

Complete ELSE Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
IDENTIFICATION 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.

Best Practices for ELSE

  • Use ELSE when needed: When you must handle the false case
  • Keep ELSE focused: ELSE clauses should be clear and focused
  • Proper indentation: Indent ELSE clauses for readability
  • Match IF with END-IF: Ensure proper pairing
  • Avoid deep nesting: Consider EVALUATE for complex multi-way selection
  • Test both paths: Test both IF and ELSE execution paths
  • Use meaningful conditions: Make conditions clear and readable

Explain Like I'm 5: ELSE

Think of ELSE like choosing between two paths:

  • IF it's sunny - go to the park (first path)
  • ELSE - stay inside (second path)

So ELSE is like having a backup plan - if the first condition isn't true, you do something else!

Test Your Knowledge

1. What does the ELSE clause do?

  • Executes when the IF condition is true
  • Executes when the IF condition is false
  • Always executes
  • Never executes

2. Is ELSE required in an IF statement?

  • Yes, always required
  • No, it's optional
  • Only for nested IFs
  • Only in certain COBOL versions

3. What happens if IF condition is false and there's no ELSE?

  • Program crashes
  • No statements in IF block execute, execution continues after END-IF
  • First statement after IF executes
  • Last statement before END-IF executes

4. Can you nest IF statements with ELSE?

  • No, nesting is not allowed
  • Yes, each IF can have its own ELSE
  • Only one level of nesting
  • Only without ELSE

5. Where does ELSE appear in an IF statement?

  • Before IF
  • After IF condition, before statements
  • After the true-case statements, before END-IF
  • After END-IF

6. What is the relationship between ELSE and WHEN OTHER?

  • They are the same
  • ELSE is for IF statements, WHEN OTHER is for EVALUATE
  • ELSE is for EVALUATE, WHEN OTHER is for IF
  • They cannot be used together