IF handles the happy path; ELSE handles everything else. In Easytrieve ELSE is optional, conditionless, and terminal—it must be the last branch before END-IF when present. Beginners underuse ELSE and leave GIVING fields stale on SEARCH miss, or overuse ELSE when ELSE-IF bands would read cleaner. ELSE is the right tool for unknown department codes, unexpected status values, and default messages on reports that must print every row. It pairs with audit counters, error file WRITE, and MOVE of literal defaults. This chapter page complements the ELSE statement reference with batch patterns: when catch-all beats another ELSE-IF, how ELSE nests secondary IF, formatting rules requiring ELSE on its own line, and maintainability habits that keep ELSE blocks short and named by purpose in comments.
12345678910IF PAY-TYPE EQ 'S' MOVE SALARY-RATE TO WS-RATE ELSE-IF PAY-TYPE EQ 'H' MOVE HOURLY-RATE TO WS-RATE ELSE-IF PAY-TYPE EQ 'C' MOVE COMM-RATE TO WS-RATE ELSE MOVE ZERO TO WS-RATE ADD 1 TO UNKNOWN-TYPE-CNT END-IF
Three known pay types map to rates. ELSE handles commission typos, blank types, and future codes not yet in the ELSE-IF list. UNKNOWN-TYPE-CNT supports trailer audit. Without ELSE, WS-RATE might retain a prior record's value—dangerous on reports.
| Situation | Use |
|---|---|
| Another testable condition | ELSE-IF |
| Everything not matched above | ELSE |
| No false-path action needed | Omit ELSE |
| False path only if outer IF true | Nested IF inside true branch |
1234567SEARCH STATE-TAB WITH ST-CODE GIVING ST-NAME IF STATE-TAB * use ST-NAME on report ELSE ST-NAME = '** INVALID STATE **' ADD 1 TO ST-ERR-CNT END-IF
Classic decode ELSE: literal default plus counter. Operations reviews ST-ERR-CNT in job log summary procedures. ELSE is not optional when auditors require explicit handling of invalid codes—even if the literal is spaces, you must MOVE in ELSE to clear stale GIVING.
Broadcom requires ELSE on a separate statement unless using period-space alternate form. Do not append ELSE to the prior line in maintenance-friendly source. Align ELSE with IF and ELSE-IF in column area B for readability. Comments above ELSE should state business rule—UNKNOWN PRODUCT CODES TO ERROR FILE.
123456789IF AMT GT 10000 MOVE 'HIGH' TO WS-TIER ELSE IF AMT GT 5000 MOVE 'MID' TO WS-TIER ELSE MOVE 'LOW' TO WS-TIER END-IF END-IF
Outer ELSE contains inner IF for sub-bands below ten thousand. Alternative: flat ELSE-IF chain if all tiers are mutually exclusive at one level. Nested version emphasizes that mid/low only matter when not high—same outcome here, different readability. Pick one style per program.
ELSE may end with GOTO JOB to skip PRINT for bad rows after handling error. Ensure END-IF still appears before label targets confuse the compiler. Pattern: IF valid PRINT; ELSE WRITE error; GOTO JOB; END-IF—GOTO inside ELSE skips remainder of iteration.
Online maps use ELSE to show error messages when validation IF fails—MOVE message to SCREEN field in ELSE, REFRESH or RESHOW in AFTER-SCREEN per screen rules. Batch JOB patterns transfer directly with DISPLAY instead of SCREEN map fields.
| Mistake | Fix |
|---|---|
| ELSE-IF after ELSE | Move new tests above ELSE |
| Empty ELSE with stale fields | MOVE defaults in ELSE |
| Duplicate logic in every ELSE-IF and ELSE | Extract procedure |
| Expecting END-ELSE | Use single END-IF only |
IF and ELSE-IF are special cases—red lunch ticket, blue lunch ticket. ELSE is everyone who did not get a special ticket still gets a plain sandwich so nobody goes hungry. You only need one plain-sandwich line at the end. If you forget the plain sandwich line, some kids might keep yesterday's lunch bag by mistake—that is stale data without ELSE.
1. ELSE runs when:
2. Maximum ELSE branches per IF group:
3. ELSE after SEARCH miss often:
4. ELSE requires:
5. ELSE-IF after ELSE is: