Easytrieve IF Statement

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.

Progress0 of 0 lessons

Statement Format

text
1
2
3
4
5
6
7
8
IF 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.

How Truth Flows

IF evaluation order
SituationWhat runs
First IF condition truestatement-1 only; skip ELSE-IF and ELSE
First false, ELSE-IF trueThat ELSE-IF body; later ELSE-IF/ELSE skipped
All IF/ELSE-IF false, ELSE presentELSE body (statement-3)
All false, no ELSENothing in the IF group; continue after END-IF

ELSE-IF Versus Nested 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.

text
1
2
3
4
5
6
7
8
9
* 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 Formatting Rule

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.

Conditional Expression Types

Easytrieve accepts several condition families (also usable in DO WHILE / DO UNTIL). Understanding each type prevents forcing everything into field1 = field2:

  • Field relational — compare field to field, literal, or arithmetic expression with EQ/=, NE, GT/>, GE, LT/<, LE.
  • Field series — IF field = a, b, c style lists for membership.
  • Field class — IF field ALPHABETIC, NUMERIC, and related class tests for data validation.
  • Field bits — test bit patterns ON a hexadecimal mask.
  • File presence — IF EOF file-name or presence tests after GET.
  • File relational — IF MATCHED file-1, file-2 for synchronized JOB input.
  • Record relational — IF DUPLICATE file-name for duplicate detection patterns.

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.

text
1
2
3
4
5
6
7
8
9
10
11
12
13
IF 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

Comparison Operators

Common relational operators in IF
OperatorMeaningBeginner tip
EQ or =EqualMost common edit and key match
NE or ¬= / -= formsNot equalSkip or error on mismatch
GT or >Greater thanThresholds, salary brackets
GE or >=Greater or equalInclusive lower bounds
LT or <Less thanBelow cutoff skips
LE or <=Less or equalInclusive upper bounds

Use the symbolic or alphabetic form consistently inside a program. Mixed styles work but hurt readability in reviews.

NOT NUMERIC and Record Bypass

text
1
2
3
4
5
6
7
8
9
10
11
12
JOB 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.

Nesting and END-IF Matching

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.

IF Versus CASE

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.

Testing IF Logic

  1. Build a truth table for three ELSE-IF brackets with boundary values (500.00 exactly).
  2. Omit END-IF once on purpose; read the compiler message.
  3. Compare nested IF versus ELSE-IF for the same bonus rules—outputs must match.
  4. Test IF EOF after GET on empty and one-record files.
  5. Combine AND/OR and verify parentheses against expected records selected.

Common IF Mistakes

  • Missing END-IF.
  • Adding END-IF after every ELSE-IF.
  • Assuming false IF runs something when no ELSE exists.
  • Using CASE-like thinking for open-ended ranges better suited to IF.
  • Forgetting NOT NUMERIC before arithmetic.
  • Misaligned nesting so ELSE binds to the wrong IF.

Explain It Like I'm Five

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.

Exercises

  1. Write IF/ELSE-IF/ELSE for three salary tax brackets.
  2. Add a NOT NUMERIC guard with GOTO JOB.
  3. Rewrite an ELSE-IF chain as nested IF; keep identical results.
  4. Write IF MATCHED for two synchronized files (conceptual).
  5. List five condition types and one example each.

Quiz

Test Your Knowledge

1. Every IF must end with:

  • A single END-IF
  • END-JOB only
  • STOP
  • ELSE only

2. ELSE-IF requires its own END-IF:

  • False — only the outer IF needs END-IF
  • True always
  • Only in REPORT
  • Only online

3. If IF is false and there is no ELSE:

  • No statements in the IF body run; control continues after END-IF
  • Program abends
  • GOTO JOB forced
  • FILE closes

4. ELSE must appear:

  • On its own source statement (unless period-space form)
  • Only inline with IF
  • In PARM
  • In JCL

5. IF EOF file-name is which condition type?

  • File presence style EOF test
  • Arithmetic only
  • Printer only
  • Macro only
Published
Read time17 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 IF ELSE-IF ELSE END-IFSources: Broadcom Easytrieve 11.6 IF/ELSE-IF/ELSE/END-IF; Conditional Expressions; JOB ActivitiesApplies to: Easytrieve IF decision logic