RPGLE Select and When Logic

An IF/ELSEIF chain can express several alternatives, but the repeated keywords can become awkward when the code is really saying “choose one route from this list.” RPGLE SELECT and WHEN make that shape explicit: begin one selection group, list the conditions in priority order, optionally provide an OTHER fallback, and close the group with ENDSL.1

This RPGLE select when tutorial focuses on that practical multi-branch decision. The key rule is simple and important: RPG considers the WHEN conditions in source order and chooses the block belonging to the first true condition. Only one WHEN block runs in the group.1 This is case-style logic in a broad editorial sense, but it is not a promise that RPGLE behaves exactly like any other language’s switch or case construct. The rules here come from IBM’s ILE RPG definition.

Quick Summary

  • Start an operand-less selection group with SELECT; and close it with ENDSL;.1
  • Each WHEN supplies an indicator expression. Its controlled operations are eligible when that expression is true.2
  • You can write multiple WHEN clauses. RPG evaluates them in the order they appear and selects the first true one.1
  • After the selected block finishes, control passes to ENDSL; later WHEN clauses in that group are not processed.1
  • OTHER is optional. When present, it is the fallback only if no WHEN condition is satisfied.3
  • Use SELECT/WHEN when several alternatives read more clearly as one ordered choice. This is readability guidance, not a compiler threshold.
  • Keep conditions side-effect free. This article makes no claim about short-circuit or operand evaluation inside a compound expression.

Reader Prerequisites

This tutorial continues directly from RPGLE IF, ELSE, and ELSEIF Decisions, the registry prerequisite and previous learning step. You should already be comfortable with a condition that produces a true or false result, with simple comparisons such as orderStatus = 'P', and with the way an ordered IF/ELSEIF group chooses one path. Those foundations are reused here rather than taught again.

If you want the broader sequence around this lesson, RPGLE for Beginners: A Practical IBM i Learning Path is the pillar guide for the cluster.

Learning Outcomes

By the end, you should be able to:

  1. Write and close a modern free-form SELECT group.
  2. Add several WHEN branches in deliberate business-priority order.
  3. Use OTHER as an optional no-match fallback.
  4. Predict which branch runs when more than one condition could be true.
  5. Convert a modest IF/ELSEIF chain to SELECT/WHEN without changing its result.
  6. Recognize when a simple IF is clearer and when deeper complexity belongs in a later lesson.

The SELECT and WHEN Mental Model

Think of a SELECT group as one ordered choice among alternatives:

select;
  when conditionOne;
    // First alternative
  when conditionTwo;
    // Second alternative
  other;
    // Optional fallback
endsl;

IBM describes the SELECT, WHEN, and OTHER family as conditionally processing one of several alternative sequences of operations.4 For the operand-less form taught here, every WHEN carries its own condition. That condition is an indicator expression: it must produce the true-or-false value that determines whether the block is selected.2

The indentation makes the alternatives easy to scan, but indentation does not create the group. The SELECT, WHEN, optional OTHER, and ENDSL statements define it.

A Basic SELECT with WHEN

Start with two possible order routes:

dcl-s orderStatus char(1);

orderStatus = 'P';

select;
  when orderStatus = 'H';
    dsply 'hold-order';
  when orderStatus = 'P';
    dsply 'release-order';
endsl;

The first condition is false because the status is not H. The second condition is true, so release-order displays. The group then finishes at ENDSL; it does not need an OTHER because doing nothing is acceptable for every status not listed.13

The complete source member is basic-select-when.rpgle. It is documentation-reviewed but has not been compiled or run on IBM i in this workspace.

Add Multiple WHEN Branches

A selection group becomes useful when the alternatives form a visible business list. This customer-tier decision has three recognized values:

select;
  when customerTier = 'P';
    dsply 'priority-service';
  when customerTier = 'G';
    dsply 'gold-service';
  when customerTier = 'S';
    dsply 'standard-service';
endsl;

For customerTier = 'S', RPG considers the first and second conditions, finds both false, then selects the third block. IBM’s SELECT definition permits multiple WHEN groups and states that control goes to the operations associated with the first satisfied condition.1

The order still matters even when the examples compare one variable with mutually exclusive codes. A reader sees the alternatives from top to bottom, so arrange them according to the business rule: exceptional or high-priority routes first, ordinary routes later.

Use OTHER as the Fallback

Production data does not always contain a recognized code. Add OTHER when an unrecognized or incomplete value needs an explicit action:

select;
  when customerTier = 'P';
    dsply 'priority-service';
  when customerTier = 'G';
    dsply 'gold-service';
  when customerTier = 'S';
    dsply 'standard-service';
  other;
    dsply 'review-tier';
endsl;

OTHER has no condition. IBM defines it as the sequence processed when no WHEN condition in the group is satisfied.3 It is optional, only one can appear in a selection group, and a later WHEN cannot follow it in the same group.3

For an unknown tier such as X, all three WHEN expressions are false and review-tier displays. For S, the third WHEN block runs and the fallback does not. The full member, initialized with S, is select-when-other.rpgle.

How Branch Evaluation Works

The source order is part of the decision:

  1. Enter the SELECT group.
  2. Evaluate the first WHEN expression.
  3. If it is false, evaluate the next WHEN expression.
  4. Continue until a true expression is found.
  5. Run the block associated with that first true WHEN.
  6. Skip the later alternatives and continue at the group’s ENDSL.
  7. If no WHEN is true, run OTHER when it exists; otherwise continue after ENDSL.

IBM states both that only one WHEN block is executed and that, if more than one WHEN condition is true, the statements associated with the first true condition are chosen.1 That directly supports the useful shorthand “first matching WHEN wins,” provided “match” means an indicator expression evaluated as true.

Conceptual source-level flow for an RPGLE SELECT group. WHEN conditions are tested in source order. The first true condition runs its block and proceeds to ENDSL; if none is true, OTHER supplies the optional fallback.
Conceptual source-level evaluation, not machine-level implementation.

This diagram describes the language-level result, not compiler-generated machine instructions. It also describes selection among WHEN clauses, not how RPG evaluates operands inside one expression. No short-circuit behavior is assumed here.

Worked Example: Route an Order by Status

Consider an order process with these priorities:

  • Held orders always go to hold review.
  • Pending expedited orders go to expedited release.
  • Other pending orders go to standard release.
  • Shipped orders go to shipment monitoring.
  • Completed orders can be closed.
  • Unknown codes require manual review.

The rule reads naturally as one list of alternative routes:

dcl-s orderStatus char(1);
dcl-s expedited ind;

orderStatus = 'P';
expedited = *on;

select;
  when orderStatus = 'H';
    dsply 'hold-review';
  when (orderStatus = 'P') and (expedited = *on);
    dsply 'expedite-release';
  when orderStatus = 'P';
    dsply 'standard-release';
  when orderStatus = 'S';
    dsply 'shipment-monitor';
  when orderStatus = 'C';
    dsply 'close-order';
  other;
    dsply 'manual-review';
endsl;

With the initialized values, the first WHEN is false and the compound second condition is true. expedite-release displays, and the later plain orderStatus = 'P' branch is skipped.1 That order is essential: if the broader pending-order condition came first, it would select standard release before the expedited rule could be considered.

The complete source is order-status-routing.rpgle. Its expected observation follows from the initialized values and documented semantics; it is not captured runtime output.

SELECT/WHEN Versus IF/ELSEIF

Both structures can express an ordered choice. IF executes its controlled block when its indicator expression is true, while an ELSEIF block is eligible when its expression is true and the preceding IF or ELSEIF expression was false.56 SELECT groups the same broad kind of alternatives beneath a keyword that announces a multi-way selection.4

Compare the shapes:

if customerTier = 'P';
  dsply 'if-priority';
elseif customerTier = 'G';
  dsply 'if-gold';
elseif customerTier = 'S';
  dsply 'if-standard';
else;
  dsply 'if-review';
endif;
select;
  when customerTier = 'P';
    dsply 'select-priority';
  when customerTier = 'G';
    dsply 'select-gold';
  when customerTier = 'S';
    dsply 'select-standard';
  other;
    dsply 'select-review';
endsl;

For customerTier = 'G', each group chooses its second alternative. The difference is communicative: SELECT tells the maintainer at the opening line to expect a list of branches. The complete side-by-side member is if-chain-vs-select.rpgle.

StructureBest forNumber of branchesDefault branchReadabilityTypical mistake
IFOne conditional actionOne explicit branchNone; false continues after ENDIFClearest for one focused testAdding structure that the rule does not need
IF / ELSEIF / ELSEA small ordered conditional chainTwo or a fewOptional ELSEFamiliar when the chain stays shortPutting a broad test before a specific one
SELECT / WHEN / OTHERSeveral alternative decision branchesMultipleOptional OTHERMakes the multi-way shape visible immediatelyExpecting later true branches to run

There is no compiler-defined number at which you must switch structures. The table is editorial guidance. Choose the form that lets a reviewer verify the business rule most quickly, while preserving the same condition order and fallback behavior.

Editorial guide for choosing an RPGLE decision structure: IF for one condition, IF and ELSE for two outcomes, IF and ELSEIF for a small chain, SELECT and WHEN for several alternatives, and ARTICLE-066 for nesting or readability trouble.
Editorial guidance, not an IBM compiler rule.

A Small Compound Condition

A WHEN accepts an indicator expression, so it can express a modest compound rule. IBM’s own WHEN example includes NOT, AND, equality, and parentheses.2 This tutorial needs only one AND:

when (orderStatus = 'P') and (expedited = *on);
  dsply 'expedite-release';

The condition names the intended business case: pending and expedited. The parentheses visually separate the comparisons. They are used for clarity here, not as a complete lesson on precedence.

Keep such expressions side-effect free. This article does not claim that one operand prevents another from being evaluated, and its correctness does not depend on operand order or short-circuiting. ARTICLE-067, RPGLE Boolean Logic and Comparison Patterns, owns the detailed operator and expression discussion.

Readability Guidance

  • Order branches by business priority when conditions can overlap.
  • Put the exceptional route before the broad route it would otherwise satisfy.
  • Use names such as orderStatus and expedited that make a condition understandable without a comment.
  • Keep each controlled block focused enough that the next WHEN remains visible.
  • Include OTHER when an unexpected value needs logging, review, rejection, or another explicit fallback.
  • Omit OTHER when continuing without an action is deliberately correct.
  • Align SELECT, WHEN, OTHER, and ENDSL; indent the operations they control.
  • Avoid importing fall-through expectations from another language. RPG’s documented selection runs one WHEN block.1

A comment should explain an unusual business priority, not translate syntax line by line. For example, “held orders override every release route” adds information; “check whether status is H” merely repeats the expression.

When SELECT/WHEN Is Not the Right Tool

Use a simple IF when the rule has one condition and one optional action. Use IF/ELSE when the code is most naturally a true-versus-false choice. ARTICLE-064 covers those structures fully; repeating them as SELECT/WHEN does not automatically improve the code.

SELECT is also not a cure for every complicated decision. If each branch contains more branching, the real problem may be nesting, responsibilities, or early-exit design. Continue to ARTICLE-066, RPGLE Nested Decisions and Guard Clauses, for that problem.

Finally, do not stretch one SELECT group into an operator reference. If understanding a branch requires a map of AND, OR, NOT, precedence, and comparison forms, ARTICLE-067 is the appropriate next lesson.

Common Mistakes

Closing with ENDIF. A SELECT group ends with ENDSL, not ENDIF.14

Treating every WHEN as an independent IF. One selection group processes only one alternative. The first true WHEN owns the selected block, and later alternatives are skipped.1

Putting a broad condition first. In the order example, plain pending status before pending-and-expedited would capture expedited orders too. Put the more specific priority first.

Adding a condition to OTHER. OTHER has no expression. It represents the optional path used when none of the WHEN conditions is true.3

Writing WHEN after OTHER. IBM allows only one OTHER and does not allow another WHEN after it in the same group.3

Assuming OTHER is mandatory. It is optional. Without a match and without OTHER, control continues after ENDSL.13

Expecting switch-style fall-through. Do not borrow that mental model. IBM states that only one WHEN block in the group is executed.1

Relying on short-circuit behavior. Keep expressions free of mutations or unsafe operations whose correctness depends on one operand being skipped. No such guarantee is needed for the examples in this tutorial.

FAQ

How do SELECT and WHEN work in RPGLE?

SELECT begins a group of alternatives. Each WHEN provides an indicator expression. RPG evaluates the conditions in source order, runs the block belonging to the first true WHEN, and then continues at ENDSL.12

What does OTHER do in an RPGLE SELECT block?

OTHER provides an optional fallback when no WHEN condition is satisfied. It has no condition of its own, only one is allowed in a group, and no WHEN can follow it within that group.3

Can I have multiple WHEN statements?

Yes. The operand-less SELECT group can contain zero or more WHEN groups, though a useful business selection normally contains the alternatives it needs.1 Arrange multiple clauses so their source order reflects priority.

Does RPGLE stop after the first matching WHEN?

Within that SELECT group, yes. IBM says that when multiple conditions are true, control goes to the block associated with the first true WHEN, and only one WHEN block is executed.1 After that block, processing continues at ENDSL.

What is the difference between SELECT/WHEN and IF/ELSEIF?

Both can represent ordered conditions that choose one branch. SELECT/WHEN makes a several-alternative selection visually explicit, while IF/ELSEIF is often economical for a small chain. The language behavior is documented by IBM; the choice between readable equivalent forms is editorial judgment.46

When should I use SELECT instead of IF?

Consider SELECT when the rule is easiest to read as several alternative decision branches. Keep IF for one focused condition and IF/ELSE for a clear two-outcome rule. There is no fixed compiler threshold; preserve the business order and choose the clearer source.

How do I end a SELECT block in free-form RPGLE?

End it with ENDSL;. IBM identifies ENDSL as the end of the select group.14

Key Takeaways

  • SELECT announces one multi-branch decision; WHEN introduces each conditional alternative.
  • WHEN requires an indicator expression, and a true expression makes that block eligible.
  • Conditions are considered in source order, the first true WHEN is selected, and later clauses do not run.
  • Optional OTHER handles the case where no WHEN is true.
  • ENDSL closes the group.
  • SELECT/WHEN and IF/ELSEIF can express equivalent rules; readability and business shape guide the choice.
  • Keep compound expressions modest and side-effect free, and defer deeper nesting or Boolean detail to the articles that own those topics.

Continue Your Learning

  1. Revisit the prerequisite, RPGLE IF, ELSE, and ELSEIF Decisions, to compare the same ordered-choice mental model in a smaller chain.
  2. Use RPGLE for Beginners: A Practical IBM i Learning Path to place this decision lesson in the broader RPGLE sequence.
  3. Continue next to RPGLE Nested Decisions and Guard Clauses (ARTICLE-066) when nesting or long branch bodies become the readability problem.
  4. Then use RPGLE Boolean Logic and Comparison Patterns (ARTICLE-067) for detailed operators, precedence, and reusable condition patterns.

The next two lessons are named without links because this workspace does not establish public destinations for them. No URL is inferred from a planned slug.

IBM Evidence Used

The core behavior comes from the IBM i 7.5 ILE RPG Reference pages for SELECT, WHEN, OTHER, and structured programming operations. The 7.4 SELECT, WHEN, and OTHER pages were used as cross-checks for the operand-less syntax and behavior.789 The 7.5 IF and ELSEIF pages support the concise comparison with the prerequisite structure.

IBM’s SELECT topic directly supports the group shape, ordered conditions, first-true behavior, single selected block, OTHER fallback, no-match continuation, and ENDSL closure.1 IBM’s WHEN topic supplies the indicator-expression requirement and the truth condition, while its example demonstrates the equality and logical forms used here.2 IBM’s OTHER topic supports its optional, no-match, single-instance, final-alternative role.3

The IBM Docs pages used here do not provide a publication date in the retrieved material, so only the access date is recorded. Some direct IBM Docs requests returned a 403 or cache miss during review; substantive IBM content was available through indexed documentation results and the accessible WHEN page. Navigation-only shells were not treated as proof. No local IBM i compiler is available, so every code asset is documentation-reviewed; not compiled or run on IBM i in this workspace.

References

References

  1. SELECT (Begin a Select Group) — IBM
  2. WHEN (When True Then Select) — IBM
  3. OTHER (Otherwise Select) — IBM
  4. Structured Programming Operations — IBM
  5. IF (If) — IBM
  6. ELSEIF (Else If) — IBM
  7. SELECT (Begin a Select Group) — IBM
  8. WHEN (When True Then Select) — IBM
  9. OTHER (Otherwise Select) — IBM



Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top