Control Flow in REXX
Master conditionals and loops in REXX - IF/ELSE, SELECT/WHEN, DO loops, and LEAVE/ITERATE with Docker-ready examples using Regina REXX
Control flow is what turns a list of instructions into a program that makes decisions and repeats work. As an imperative, structured language, REXX gives you a clean, English-like set of keywords for branching and looping—IF, SELECT, and DO—without the punctuation overhead found in many other languages.
True to Mike Cowlishaw’s design goals, REXX control flow reads almost like plain English. There are no curly braces, no colons, and no significant indentation. A block of statements is simply wrapped in DO ... END, and conditions use ordinary words like then, else, when, and otherwise. Because REXX is typeless, conditions evaluate strings and numbers interchangeably—1, '1', and the result of 2 - 1 all compare as equal.
In this tutorial you’ll learn how to branch with IF/THEN/ELSE, handle multiple cases with SELECT/WHEN, and iterate using REXX’s remarkably flexible DO loop. You’ll also see how LEAVE and ITERATE give you precise control over loop execution.
Conditionals with IF/THEN/ELSE
The IF instruction tests a condition and runs an instruction when it’s true. An optional ELSE clause handles the false case. When you need more than one statement in a branch, wrap it in DO ... END.
Create a file named control_flow_if.rexx:
| |
A few things to notice:
- The condition needs no parentheses, though you may add them for clarity.
thenis required after the condition—it’s part of REXX’s grammar, not optional punctuation.- An
ELSEclause attaches to a single instruction. Because an entireIFis itself one instruction,else if ...chains naturally without any specialelifkeyword. - Comparison uses a single
=(REXX has no==). Other operators include\=(not equal),<,>,<=, and>=.
Multi-way Branching with SELECT
When you have several mutually exclusive cases, SELECT is clearer than a long IF/ELSE chain. It evaluates each WHEN condition in order and runs the first one that’s true. The OTHERWISE clause is the catch-all.
Create a file named control_flow_select.rexx:
| |
Key points about SELECT:
- Each
WHENis a full boolean condition, not just a value to match—so you can test ranges, combine conditions with|(or) and&(and), and call functions. - The first matching
WHENwins; remaining conditions are not evaluated. OTHERWISEis strongly recommended. If noWHENmatches and there’s noOTHERWISE, REXX raises a syntax error.- The whole construct is closed with a single
END.
Loops with DO
The DO instruction is REXX’s universal looping construct. It handles counted loops, conditional loops, and simple repetition—all with the same keyword.
Create a file named control_flow_loops.rexx:
| |
This single example shows the main loop forms:
do i = 1 to 5counts an index variable automatically.by 2changes the step; it can be negative to count down.do 3repeats a block a fixed number of times without an index.do whilechecks the condition before the body, so it may run zero times.do untilchecks after the body, so it always runs at least once.
Loop Control with LEAVE and ITERATE
LEAVE exits a loop immediately (like break in other languages), and ITERATE skips to the next iteration (like continue). Combined with DO FOREVER, they let you build loops that run until an arbitrary condition is met.
Create a file named control_flow_control.rexx:
| |
Notes on loop control:
LEAVEandITERATEact on the innermost loop by default. You can target an outer loop by naming its index variable, e.g.leave i.//is REXX’s remainder operator, soj // 2 = 0tests whetherjis even.DO FOREVERhas no exit condition of its own—you must useLEAVE(orRETURN/EXIT) to break out, or it will run indefinitely.
Running with Docker
The examples run unchanged in the Regina REXX container.
| |
Expected Output
Running control_flow_if.rexx:
It's pleasant outside
You are an adult
You can vote
Grade: B
Running control_flow_select.rexx:
It's a regular work day
Good afternoon
Running control_flow_loops.rexx:
Counting up:
i = 1
i = 2
i = 3
i = 4
i = 5
Counting by twos:
n = 0
n = 2
n = 4
n = 6
n = 8
n = 10
Three cheers:
Hooray!
Hooray!
Hooray!
Countdown:
count = 3
count = 2
count = 1
Runs at least once:
x = 10
Running control_flow_control.rexx:
Searching for 7:
Found 7 at iteration 7
Odd numbers from 1 to 10:
Odd: 1
Odd: 3
Odd: 5
Odd: 7
Odd: 9
Accumulating until the sum exceeds 20:
Final total: 21 after adding 1 to 6
Key Concepts
IF/THEN/ELSEbranches on a condition.thenis mandatory, and multiple statements in a branch must be grouped withDO ... END.- Chained
ELSE IFworks naturally because anIFis a single instruction—REXX needs no dedicatedelif/elseifkeyword. SELECT/WHEN/OTHERWISEhandles multi-way branching cleanly. EachWHENis a full condition, andOTHERWISEshould always be present to avoid a runtime error when nothing matches.- The
DOloop is universal:DO i = 1 TO n [BY step]for counted loops,DO nfor fixed repetition,DO WHILEfor pre-tested loops,DO UNTILfor post-tested (run-at-least-once) loops, andDO FOREVERfor open-ended loops. LEAVEandITERATEprovide break/continue behavior and can target a named outer loop (e.g.leave i).- No ternary operator: REXX has no
?:conditional expression—use a shortIF/THEN/ELSEor aSELECTinstead. - Typeless comparisons: conditions compare strings and numbers transparently, and a single
=is the equality operator (there is no==). - Operators in conditions: combine tests with
&(and),|(or), and\(not); use//for remainder when writing numeric tests.
Running Today
All examples can be run using Docker:
docker pull rzuckerm/rexx:3.6-5.00-1
Comments
Loading comments...
Leave a Comment