IF is the primary decision statement in Easytrieve. It tests a conditional expression and runs the statements that belong to the true path. When the first condition is false, optional ELSE-IF clauses test alternate expressions without requiring a separate END-IF for each branch. Optional ELSE runs when every preceding condition is false. A single END-IF closes the entire IF group— forgetting END-IF is a classic compile error. Conditions are not limited to field comparisons: you can test NUMERIC or ALPHABETIC class, bit patterns, EOF, MATCHED files in synchronized jobs, and DUPLICATE records. Nesting is allowed when one IF appears inside another's statement list; each nested IF still needs its own END-IF. Beginners coming from COBOL recognize the shape but must learn Easytrieve's ELSE-IF (no per-branch END-IF) and the rich condition catalog. This page teaches syntax, truth-table style behavior, condition types, nesting versus ELSE-IF, formatting rules for ELSE, and production patterns for edits, bonuses, and record bypass with GOTO JOB.
12345678IF conditional-expression-1 [statement-1] [ELSE-IF conditional-expression-2] [statement-2] ... [ELSE] [statement-3] END-IF
Statement-1, statement-2, and statement-3 may each be zero or more Easytrieve statements, including nested IF. Indent bodies under IF/ELSE-IF/ELSE so readers see structure at a glance.
| Situation | What runs |
|---|---|
| First IF condition true | statement-1 only; skip ELSE-IF and ELSE |
| First false, ELSE-IF true | That ELSE-IF body; later ELSE-IF/ELSE skipped |
| All IF/ELSE-IF false, ELSE present | ELSE body (statement-3) |
| All false, no ELSE | Nothing in the IF group; continue after END-IF |
ELSE-IF is the clean way to express multi-way choices on related conditions—bonus brackets, status codes, region groups—without stacking END-IF. Nested IF is better when the inner decision only makes sense inside one branch (for example, only when PAY-GROSS is numeric do you compare thresholds). Broadcom shows three equivalent styles for a bonus example: separate sequential IFs, nested IF under ELSE, and a single IF with ELSE-IF. Prefer ELSE-IF for flat multi-way logic; prefer nesting when decisions are hierarchical.
123456789* ELSE-IF chain (one END-IF): IF PAY-GROSS NOT NUMERIC DISPLAY EMP# ' PERSONNEL RECORD IS DAMAGED' GOTO JOB ELSE-IF PAY-GROSS > 500.00 XMAS-BONUS = PAY-GROSS * 1.03 ELSE XMAS-BONUS = PAY-GROSS * 1.05 END-IF
ELSE must sit on its own source statement, unless you use the period-and-space statement separator form documented for Easytrieve source lines. Putting ELSE mid-line without that convention confuses readers and can break parsing. END-IF likewise terminates clearly—do not omit it because an ELSE is present; ELSE does not close the IF.
Easytrieve accepts several condition families (also usable in DO WHILE / DO UNTIL). Understanding each type prevents forcing everything into field1 = field2:
Combine conditions with AND and OR. Parentheses and evaluation order matter in complex predicates— see logical expressions and order-of-evaluation tutorials when mixing AND/OR.
12345678910111213IF DIVISION = 'A', 'B', 'C' DEDUCTIONS = GROSS * 0.15 ELSE DEDUCTIONS = GROSS * 0.18 END-IF IF EOF MASTER STOP END-IF IF MATCHED TRANFILE, MASTFILE PERFORM APPLY-TRAN END-IF
| Operator | Meaning | Beginner tip |
|---|---|---|
| EQ or = | Equal | Most common edit and key match |
| NE or ¬= / -= forms | Not equal | Skip or error on mismatch |
| GT or > | Greater than | Thresholds, salary brackets |
| GE or >= | Greater or equal | Inclusive lower bounds |
| LT or < | Less than | Below cutoff skips |
| LE or <= | Less or equal | Inclusive upper bounds |
Use the symbolic or alphabetic form consistently inside a program. Mixed styles work but hurt readability in reviews.
123456789101112JOB INPUT PERSNL NAME MYPROG FINISH FINISH-PROC IF PAY-GROSS NOT NUMERIC DISPLAY EMP# ' PERSONNEL RECORD IS DAMAGED' GO TO JOB END-IF IF PAY-GROSS > 500.00 XMAS-BONUS = PAY-GROSS * 1.03 ELSE XMAS-BONUS = PAY-GROSS * 1.05 END-IF TOT-XMAS-BONUS = TOT-XMAS-BONUS + XMAS-BONUS PRINT MYREPORT
Class tests protect arithmetic. Without NOT NUMERIC, multiply on damaged packed data can abend or corrupt totals. GOTO JOB then advances automatic input so the bad record never prints.
Each IF opens a level; each END-IF closes the nearest open IF. Mismatched counts are compile errors. When nesting, close the inner IF before the outer ELSE or END-IF. ELSE-IF does not add an END-IF requirement—only IF does. A practical habit: write END-IF immediately when you write IF, then fill the body between them.
CASE shines when one field selects among many discrete literal values with WHEN clauses. IF / ELSE-IF shines for ranges, class tests, combined AND/OR predicates, and EOF/MATCHED checks CASE does not express as naturally. Many programs use both: CASE for status codes, IF for numeric ranges and edits.
IF is a fork in the road. You ask a yes/no question about your data. If the answer is yes, you follow the yes path. If not, you can ask another question (ELSE-IF), or take the leftover path (ELSE), or do nothing special and keep walking after the END-IF sign that says the fork is over. You always need that END-IF sign so Easytrieve knows where the fork finishes.
1. Every IF must end with:
2. ELSE-IF requires its own END-IF:
3. If IF is false and there is no ELSE:
4. ELSE must appear:
5. IF EOF file-name is which condition type?