Easytrieve ELSE Statement

The ELSE statement is the second half of a decision in Easytrieve. On its own, an IF statement says "when this condition is true, run these statements". ELSE adds the other side of that decision: "otherwise, run these statements instead". It identifies the block of logic that executes when the preceding IF condition - and any ELSE-IF conditions that follow the IF - are all false. ELSE is optional; a bare IF/END-IF is perfectly valid when you only want to act on the true case. But whenever your logic needs a genuine fork, where one path runs for a match and a different path runs for everything else, ELSE is the tool. Two properties of ELSE surprise newcomers coming from other languages. First, ELSE never carries a condition of its own - if you need to test something, that is what ELSE-IF is for. Second, ELSE has no terminator; the single END-IF that closes the IF also closes the ELSE. There is no END-ELSE keyword in the language. This tutorial explains how ELSE fits into the full IF/ELSE-IF/ELSE/END-IF family, when to choose ELSE versus ELSE-IF, how execution flows through each branch, how nesting works, and the coding habits that keep multi-branch decisions readable. Worked examples build from a simple two-way choice up to a chained decision with several bands and a final catch-all.

Progress0 of 0 lessons

Statement Format

text
1
2
3
4
5
6
7
8
9
10
IF conditional-expression-1 [statement-1] [ELSE-IF conditional-expression-2] [ . . . ] [ [statement-2] ] [ELSE ] [ [statement-3] ] END-IF

ELSE appears near the bottom of the structure, after the IF and any ELSE-IF branches, and before the mandatory END-IF. Everything in square brackets is optional: you can have an IF with no ELSE at all, an IF with just an ELSE, or an IF with several ELSE-IF branches finished off by one ELSE. What you cannot do is put a condition on ELSE or give it its own terminator.

The Members of the IF Family

How each keyword behaves in a decision
KeywordHas a condition?Role
IFYesStarts the decision and tests the first condition
ELSE-IFYesTests another condition when all previous ones are false
ELSENoCatch-all that runs when every condition above is false
END-IFNoCloses the entire structure - exactly one per IF

A Simple Two-Way Decision

The most common use of ELSE is a plain either/or. The IF condition is tested; if it is true the first block runs, and if it is false the ELSE block runs. Exactly one of the two blocks executes every time - never both, never neither.

text
1
2
3
4
5
IF PAY-GROSS > 500.00 XMAS-BONUS = PAY-GROSS * 1.03 ELSE XMAS-BONUS = PAY-GROSS * 1.05 END-IF

Employees earning more than 500 get a 3 percent bonus; everyone else gets 5 percent. Because the two outcomes are mutually exclusive, ELSE is cleaner and faster than writing a second IF with the opposite condition, and it cannot accidentally leave the bonus unset.

ELSE Versus ELSE-IF

The decision between ELSE and ELSE-IF comes down to a single question: do you have another condition to test? If yes, use ELSE-IF. If you simply want to handle everything that did not match, use ELSE. You can chain as many ELSE-IF branches as you need and finish with one ELSE that catches the remainder. Crucially, none of the ELSE-IF branches need their own END-IF - the whole chain is closed by a single END-IF, which is what keeps deeply banded logic from drowning in terminators.

text
1
2
3
4
5
6
7
IF DIVISION = 'A' THRU 'L' DEDUCTIONS = GROSS * .15 ELSE-IF DIVISION = 'M' THRU 'R' DEDUCTIONS = GROSS * .2 ELSE DEDUCTIONS = GROSS * .18 END-IF

The first branch handles divisions A through L, the ELSE-IF handles M through R, and the ELSE handles every other division. Easytrieve tests the conditions top to bottom and runs the first one that is true; the ELSE only runs when both range tests fail.

How Control Flows Through the Branches

Only one branch of an IF/ELSE structure ever executes. Easytrieve evaluates the IF condition first. If it is true, the IF block runs and control jumps straight to the statement after END-IF, skipping every ELSE-IF and the ELSE. If it is false, the next ELSE-IF is tested, and so on. If no condition is true, the ELSE block runs. There is no fall-through between branches as there is in some languages - you never need a break statement, and the branches cannot bleed into one another.

Which branch runs for a given input
SituationBranch that executes
IF condition is trueThe IF block only
IF false, first ELSE-IF trueThat ELSE-IF block only
IF and all ELSE-IF false, ELSE presentThe ELSE block
IF and all ELSE-IF false, no ELSENo block - fall straight to after END-IF

Nesting an IF Inside ELSE

The statements inside an ELSE block can contain a complete IF/ELSE/END-IF of their own. This nesting lets you make a second decision only after the first one has gone the false way. Each nested IF needs its own END-IF, and lining up each ELSE under its matching IF is the single most important habit for keeping nested logic correct.

text
1
2
3
4
5
6
7
8
9
IF ACCOUNT-STATUS = 'A' PERFORM PROCESS-ACTIVE ELSE IF ACCOUNT-BALANCE > 0 PERFORM SEND-TO-COLLECTIONS ELSE PERFORM CLOSE-ACCOUNT END-IF END-IF

Active accounts are processed immediately. For everything else, an inner decision checks the balance: a positive balance goes to collections, otherwise the account is closed. The inner END-IF closes the inner IF, and the outer END-IF closes the outer IF - two decisions, two terminators.

Common ELSE Mistakes

  • Trying to put a condition on ELSE - use ELSE-IF when you need another test.
  • Writing END-ELSE - it does not exist; only END-IF closes the structure.
  • Coding more than one ELSE for a single IF - only the final catch-all is allowed.
  • Adding an END-IF after each ELSE-IF - the whole chain needs just one END-IF.
  • Misaligning a nested ELSE so it pairs with the wrong IF, changing the logic silently.
  • Assuming branches fall through - only one branch ever runs per pass.

Explain It Like I'm Five

Imagine a fork in a path. The IF is a sign that asks a yes-or-no question. If the answer is yes, you take the first path. ELSE is the other path - the one you take when the answer is no. You always take exactly one path, never both. ELSE-IF is like extra signs along the way asking more questions before you finally reach the last plain path (the ELSE). And END-IF is the spot where all the paths join back together and you keep walking.

Exercises

  1. Write an IF/ELSE that sets DISCOUNT to 10 for orders over 100 and 0 otherwise.
  2. Convert two separate IF statements with opposite conditions into a single IF/ELSE.
  3. Build a three-band grade check using two ELSE-IF branches and one ELSE.
  4. Nest an IF inside an ELSE and label which END-IF closes which IF.
  5. Explain in one sentence why ELSE never needs a condition.

Quiz

Test Your Knowledge

1. What does the ELSE statement identify in Easytrieve?

  • The statements executed when the IF condition is false
  • A new loop
  • The end of the program
  • A field definition

2. Does ELSE require its own terminator?

  • No - the single END-IF closes the whole IF/ELSE structure
  • Yes - it needs END-ELSE
  • Yes - it needs a period
  • Yes - it needs END-DO

3. What is the difference between ELSE and ELSE-IF?

  • ELSE-IF tests another condition; ELSE is the catch-all when everything above is false
  • They are identical
  • ELSE tests a condition; ELSE-IF does not
  • ELSE-IF needs its own END-IF

4. How many ELSE branches can a single IF have?

  • At most one
  • Unlimited
  • Exactly two
  • None

5. After the ELSE block finishes, where does execution continue?

  • At the statement following END-IF
  • Back at the IF
  • At the top of the JOB
  • At the next ELSE

Related Pages

Published
Read time15 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 IF, ELSE-IF, ELSE, and END-IF statementsSources: Broadcom Easytrieve 11.6 Language Reference IF, ELSE-IF, ELSE, and END-IF StatementsApplies to: Easytrieve ELSE conditional branch