Business programs constantly choose: approve or hold an order, ship or back-order an item, apply a discount or leave the price unchanged. In modern RPGLE, IF, ELSEIF, ELSE, and ENDIF express those choices directly. An IF tests a condition; RPG runs the controlled block when that condition is true.1
This RPGLE if else guide starts with a single conditional action, adds a two-way choice, and then builds an ordered chain. By the end, you will be able to look at a small business rule and predict exactly which block is eligible to run.
Quick Summary
- Use
IF condition; ... ENDIF;when an action is needed only when a condition is true.1 - Add
ELSEwhen exactly one of two blocks must run. - Add
ELSEIF condition;for a small, ordered set of tests. Its block runs only when its condition is true and the precedingIForELSEIFcondition was false.2 ELSEis optional. When present, it is the default path after the earlier conditions are not satisfied.3- End the complete group once with
ENDIF;.4 - Put the most specific or highest-priority business condition first. Order is part of the rule, not merely formatting.
- Do not rely on an assumed short-circuit or left-to-right operand-evaluation rule. Keep conditional expressions simple and free of side effects.
Reader Prerequisites
This article assumes the overall source organization from RPGLE Program Structure Explained and the DCL-S variable basics from RPGLE Variables and Storage Types. The examples reuse those skills without re-teaching program anatomy, scope, or storage duration.
Learning Outcomes
After this tutorial, you should be able to:
- Write and close a one-way
IFblock. - Use
IFandELSEfor two mutually exclusive outcomes. - Arrange multiple
ELSEIFconditions in deliberate order. - Predict the selected block for a given set of values.
- Read simple comparisons and one small compound condition.
- Recognize when a decision chain should move to the next lesson.
The Decision Mental Model
Think of an IF group as a sequence of questions, not a list of independent actions:
- Evaluate the
IFcondition. - If it is true, run that controlled block.
- If it is false, try the next
ELSEIF, if one exists. - Continue through later
ELSEIFclauses only while preceding conditions are false. - If no tested condition selects a block, run
ELSEwhen it exists. - Continue with the statement after
ENDIF.
IBM describes ELSEIF as the combination of ELSE and IF, avoiding another level of nesting. Its controlled operations run when its own expression is true and the previous IF or ELSEIF expression was false.2 That is why an ordered chain selects the first eligible true branch. It does not keep testing later alternatives after selecting a block.
This branch-selection rule is different from the evaluation of operands inside one expression. This article makes no short-circuit claim. Keep each condition free of side effects so correctness never depends on which operand RPG evaluates first.
Basic IF: Run Something Only When True
The smallest decision has one condition and one controlled block:
dcl-s orderAmount packed(9:2);
orderAmount = 1250.00;
if orderAmount > 1000.00;
dsply 'review-required';
endif;
Because 1250.00 > 1000.00 is true, the display operation runs. If orderAmount were 900.00, RPG would skip the display and continue after ENDIF. IBM defines the operand of free-form IF as an indicator-valued expression and says the controlled operations are performed when it is true.1
The full example is in basic-if.rpgle. Use this form when doing nothing is the correct false-path behavior.
IF with ELSE: Choose Between Two Outcomes
Add ELSE when the decision has exactly two outcomes:
dcl-s inventoryQty int(10);
dcl-s requestedQty int(10);
inventoryQty = 24;
requestedQty = 10;
if inventoryQty >= requestedQty;
dsply 'ship-standard';
else;
dsply 'hold-order';
endif;
The condition is true, so ship-standard displays. The ELSE block is the alternative and does not run. With insufficient inventory, the first block would be skipped and hold-order would display instead. IBM documents ELSE as optional and as the calculation group used when the preceding condition is not satisfied.3
Notice what ELSE does not have: a condition. It means “all earlier conditions in this group failed.” The complete member is if-else.rpgle.
ELSEIF for Ordered Conditions
Use ELSEIF when a small decision has more than two ordered outcomes:
dcl-s orderAmount packed(9:2);
orderAmount = 1750.00;
if orderAmount >= 5000.00;
dsply 'manager-approval';
elseif orderAmount >= 1000.00;
dsply 'manual-approval';
elseif orderAmount > 0;
dsply 'auto-approval';
else;
dsply 'invalid-amount';
endif;
For 1750.00, the first condition is false and the second is true, so manual-approval displays. The later orderAmount > 0 condition is also mathematically true, but its block is not eligible: the earlier ELSEIF already selected the chain’s branch.2
This is why threshold order matters. If orderAmount > 0 came first, every positive amount would take auto-approval; the higher thresholds below it would never get a chance. Arrange overlapping ranges from the most selective or highest threshold to the broadest fallback. The complete member is elseif-chain.rpgle.
How RPG Chooses the Branch
For the chain above, the path depends on the first condition that can select a block:
orderAmount | First true condition | Selected result |
|---|---|---|
6500.00 | orderAmount >= 5000.00 | manager-approval |
1750.00 | orderAmount >= 1000.00 | manual-approval |
250.00 | orderAmount > 0 | auto-approval |
0.00 | None | invalid-amount from ELSE |

The diagram represents the language-level choice. It does not describe generated machine instructions. What matters to the programmer is that the clauses form one group, the conditions are considered in source order, and all paths rejoin after ENDIF.
Simple Comparison Conditions
Most introductory decisions compare a variable with another value. IBM’s structured-operations reference demonstrates these symbolic comparison forms and defines their relationships.4
| Condition | Meaning in the example |
|---|---|
orderAmount > 1000.00 | amount is greater than 1000 |
orderAmount >= 5000.00 | amount is at least 5000 |
customerStatus = 'A' | status equals active |
inventoryQty < requestedQty | inventory is less than the request |
These examples intentionally use only a small subset of comparison forms. ARTICLE-067, RPGLE Boolean Logic and Comparison Patterns, owns the complete comparison and logical-operator reference.
Keep the values being compared compatible and make the business meaning visible in the names. inventoryQty >= requestedQty is easier to audit than a condition built from unexplained flags or magic values.
A Small Compound Condition
Sometimes one action requires two facts to be true. RPGLE uses AND between the comparisons; IBM’s own IF and ELSEIF examples demonstrate this form.12
elseif (priorityCustomer = *on) and (orderAmount >= 1500.00);
dsply 'priority-review';
Both comparisons must describe the desired case: the customer is marked priority and the amount meets the threshold. Parentheses make the two comparisons visually explicit. They are a readability choice here, not a lesson in operator precedence.
Avoid function calls that mutate data inside a compound condition. This tutorial neither assumes nor promises left-to-right or short-circuit operand evaluation. If the rule grows beyond one or two plainly named comparisons, move the detailed expression design to ARTICLE-067.
Worked Example: Route an Order for Review
Now combine status, a priority flag, and amount into one ordered business decision:
dcl-s customerStatus char(1);
dcl-s priorityCustomer ind;
dcl-s orderAmount packed(9:2);
customerStatus = 'A';
priorityCustomer = *on;
orderAmount = 1800.00;
if customerStatus = 'H';
dsply 'account-hold';
elseif (priorityCustomer = *on) and (orderAmount >= 1500.00);
dsply 'priority-review';
elseif orderAmount >= 5000.00;
dsply 'manager-review';
else;
dsply 'standard-process';
endif;
Read it in order:
- The customer is active (
'A'), not on hold ('H'), so the first block is skipped. - The priority indicator is on and
1800.00meets the1500.00threshold, sopriority-reviewdisplays. - The later amount condition and the
ELSEblock are not selected. - Execution continues after
ENDIF.
The ordering states a business priority: an account hold overrides every other route. The compound priority rule is considered before the general amount route. If the business instead requires every order of at least 5000.00 to receive manager review—even for priority customers—the amount condition must be moved earlier. That is not a syntax change; it is a business-rule change expressed through order.
The full runnable member is business-decision-example.rpgle. It is documentation-reviewed but has not been compiled or run on IBM i in this workspace.
IF versus ELSEIF versus ELSE
| Structure | Number of paths | When it is appropriate | Fall-through/default behavior | Typical mistake |
|---|---|---|---|---|
IF | One conditional path plus “do nothing” | Run an action only when one condition is true | False skips the block and continues after ENDIF | Adding an unnecessary empty ELSE |
IF / ELSE | Exactly two mutually exclusive paths | One outcome for true and one for false | ELSE supplies the false/default path | Writing a condition after ELSE |
IF / ELSEIF / ELSE | A small ordered set, with optional default | Conditions overlap or have explicit priority | The first eligible true condition selects a block; optional ELSE handles no match | Placing a broad condition before a specific one |
This table is practical guidance built on the documented behavior, not a compiler mandate. Multiple structures can express the same rule; choose the one that makes the rule easiest to verify.

Readability Guidance
- Indent every controlled block consistently beneath its clause.
- Align
IF,ELSEIF,ELSE, andENDIFat the same indentation level. - Name values after the business concept:
requestedQty, notx. - Order overlapping conditions deliberately and document unusual priority rules.
- Prefer a straightforward comparison over an expression with hidden side effects.
- Use
ELSEonly when a real default action exists. - Keep a small chain together; if readers must scroll far to understand it, the decision probably belongs to ARTICLE-065 or ARTICLE-066 territory.
Indentation does not select the branch—the clauses and ENDIF do—but good indentation lets a reviewer see the structure before reading every statement.
When an IF Chain Starts Becoming the Wrong Tool
An IF/ELSEIF chain works well when the tests are few and their order expresses priority. When many alternatives compare the same value, SELECT/WHEN may communicate the alternatives more directly. ARTICLE-065, RPGLE Select and When Logic, owns that syntax and the choice between those structures.
When the difficulty comes from decisions inside decisions, or from a long path before the useful work begins, continue to ARTICLE-066, RPGLE Nested Decisions and Guard Clauses. This article intentionally does not teach deep nesting, early exits, or refactoring patterns.
Common Mistakes
Forgetting ENDIF. One ENDIF; closes the whole IF/ELSEIF/ELSE group.4
Giving ELSE a condition. Conditions belong on IF or ELSEIF. ELSE is the optional remaining path.3
Putting a broad threshold first. orderAmount > 0 before orderAmount >= 5000.00 captures all positive amounts before the high-value rule is considered.
Using separate IF statements when only one outcome is allowed. Two standalone IF blocks are two decisions; both can run when both conditions are true. An IF/ELSEIF chain represents ordered alternatives.
Assuming ELSE is mandatory. IBM documents it as optional.3 Omit it when doing nothing is the correct no-match result.
Relying on short-circuiting. Do not place a required mutation, validation, or safety step in one operand and assume another operand prevents its evaluation. Keep conditions side-effect free.
Hiding the structure with indentation. A valid block can still be difficult to review. Consistent indentation makes clause ownership visible.
FAQ
How do I write an IF statement in RPGLE?
Write if, an indicator-valued expression, and a semicolon; place the controlled operations underneath; then close the group with endif;. The block runs when the expression is true.1
What is the difference between ELSE and ELSEIF in RPGLE?
ELSEIF has another condition and is eligible after the preceding IF or ELSEIF condition was false. ELSE has no condition and provides the optional default when the earlier conditions do not select a block.23
Can I use multiple ELSEIF statements?
Yes. IBM’s ELSEIF reference example contains multiple ELSEIF clauses in one group.2 Keep the chain small enough that its ordering remains obvious.
Does only one ELSEIF branch run?
In one IF/ELSEIF/ELSE group, yes: each ELSEIF block requires its own expression to be true and the previous IF or ELSEIF expression to have been false.2 Once a block has been selected, later alternatives in that group are not selected.
How do I combine conditions in an RPGLE IF?
For a small example, join two comparisons with AND, such as (priorityCustomer = *on) and (orderAmount >= 1500.00). Keep the comparisons parenthesized and side-effect free. Detailed Boolean logic belongs to ARTICLE-067.
When should I use SELECT/WHEN instead of IF/ELSEIF?
As editorial guidance, consider SELECT/WHEN when many alternative branches make an ordered ELSEIF chain cumbersome, especially when repeatedly testing the same subject. ARTICLE-065 teaches the mechanics; this article does not.
How do I end an IF block in free-form RPGLE?
Use ENDIF;. IBM identifies ENDIF as the end of an IF group.4
Key Takeaways
IFruns a controlled block when its condition is true.ELSEsupplies an optional alternative without another condition.ELSEIFadds ordered conditions without another nesting level.- An ordered chain selects the first eligible true branch; clause order expresses business priority.
ENDIF;closes the complete group.- Keep compound conditions simple and never depend on an assumed short-circuit order.
- Move many alternatives, deep nesting, and detailed Boolean patterns to their dedicated follow-on lessons.
Continue Your Learning
- Pillar: RPGLE for Beginners: A Practical IBM i Learning Path
- Prerequisite: RPGLE Program Structure Explained
- Required foundation: RPGLE Variables and Storage Types
- Current: RPGLE IF, ELSE, and ELSEIF Decisions (ARTICLE-064)
- Recommended next: RPGLE Select and When Logic (ARTICLE-065)
- Then: RPGLE Nested Decisions and Guard Clauses (ARTICLE-066)
- Reference follow-on: RPGLE Boolean Logic and Comparison Patterns (ARTICLE-067)
ARTICLE-065, ARTICLE-066, and ARTICLE-067 are named without links because no reconciled local public destination is established for them. No URL is inferred from their planned slugs.
IBM Evidence Used
The syntax and branch semantics were checked against IBM i 7.5 ILE RPG Reference pages for IF, ELSEIF, ELSE, and structured programming operations. The 7.5 IF page defines the operand as an indicator-valued expression and says the controlled operations run when it is true. The ELSEIF page states that its expression must be true and the preceding IF or ELSEIF expression false. The ELSE and structured-operations pages establish the optional alternative and ENDIF closure.1234
IBM i 7.4 IF and ELSE pages were cross-checked and show the same retained syntax and behavior.56 No release difference relevant to these examples was found.
The compound examples use only the AND form shown in IBM’s own conditional examples. No operator-precedence or short-circuit guarantee is asserted. The four source assets are original teaching material and have been documentation-reviewed; they were not compiled or run on IBM i in this workspace.
References
IBM, “IF (If),” IBM i 7.5 ILE RPG Reference. Accessed 2026-09-11. ↩↩↩↩↩↩
IBM, “ELSEIF (Else If),” IBM i 7.5 ILE RPG Reference. Accessed 2026-09-11. ↩↩↩↩↩↩↩↩
IBM, “ELSE (Else),” IBM i 7.5 ILE RPG Reference. Accessed 2026-09-11. ↩↩↩↩↩↩
IBM, “Structured Programming Operations,” IBM i 7.5 ILE RPG Reference. Accessed 2026-09-11. ↩↩↩↩↩
IBM, “IF (If),” IBM i 7.4 ILE RPG Reference. Accessed 2026-09-11. ↩
IBM, “ELSE (Else),” IBM i 7.4 ILE RPG Reference. Accessed 2026-09-11. ↩
References
- IF (If) — IBM
- ELSEIF (Else If) — IBM
- ELSE (Else) — IBM
- Structured Programming Operations — IBM
- IF (If) — IBM
- ELSE (Else) — IBM