Five Control Structures, One Habit: Choosing on Purpose
Most control-flow problems found in review are not wrong ideas. They are the right idea expressed with the wrong structure. ILE RPG gives five tools for branching and looping, and each says something specific about the logic it contains.
IF/ELSEIF versus SELECT/WHEN
An IF/ELSEIF/ELSE chain executes the first block whose condition is true and skips the rest, which makes it the natural fit for sequential, mutually exclusive conditions such as descending thresholds. Once the same field is being tested against three or more discrete values β a status code, a claim type β SELECT/WHEN/OTHER presents every outcome in one scannable block.
select;
when claimType = 'A';
routeQueue = 'AUTO_ADJ';
when claimType = 'H';
routeQueue = 'HOME_ADJ';
other;
routeQueue = 'GEN_REVIEW';
endsl;DOW versus DOU
DOW tests its condition before every pass, so its body may run zero times. DOU tests after every pass, so its body always runs at least once. The choice comes down to one question: must the body execute before there is anything meaningful to test? A retry loop says yes; a loop that drains a queue says no.
FOR states a count
When the number of iterations is known before the loop starts β the lines on an order, the loaded elements of an array β FOR says so in its header. Rebuilding the same loop with DOW and a hand-incremented counter hides that fact from the next reader.
LEAVE and ITER
ITER skips to the next iteration; LEAVE exits the loop. Both are legitimate and both add an exit point outside the loop header. Used to skip zero-quantity lines or stop at a credit limit, they clarify intent. Used to avoid writing a clear loop condition, they hide it.
Lesson 17 works through each structure with business examples and closes with an order-evaluation routine in which every structure does the job it is best suited for.
Get the lesson
Lesson 17, βControl Structures: IF, SELECT, DOW, DOU, and FOR,β is part of Beginner ILE RPG in IBM i ILE RPG Mastery and is available at ILERPG.com for USD 10. The complete Beginner ILE RPG series β all 18 lessons β is USD 180, or USD 135 with coupon code RPG30, which takes 25% off when you purchase the full series.