Expressions in ILE RPG: Assignment, Operators, and Precedence
A line total, a reorder check, a formatted customer message: every calculation in RPG comes down to the same small toolkit. Operators combine values, assignment stores the result, and precedence decides what a mixed expression actually means.
Assignment truncates unless told otherwise
The equal sign in free-format RPG is shorthand for EVAL. The right-hand side is evaluated in full, then stored according to the target field's declared precision β and excess decimal digits are truncated, not rounded. Where a result must be rounded, as most invoice and tax calculations must, code the half-adjust extender explicitly.
extAmount = 24.955 * 3; // 74.86 (truncated)
eval(h) extAmount = 24.955 * 3; // 74.87 (half-adjusted)Whole-number division
/ divides and leaves the target's decimal positions to decide how much of the fraction survives. When the business question is "how many full cases, and how many units left over," use the %DIV and %REM built-in functions, which make whole-number intent explicit regardless of how the target is declared.
fullCases = %div(totalUnits : caseSize);
leftoverQty = %rem(totalUnits : caseSize);Concatenation and %TRIM
Between character values, + concatenates. Fixed-length CHAR fields carry trailing blanks, so concatenating them directly produces wide, unintended gaps. Pairing + with %TRIM should be the default whenever a CHAR field is involved.
Compound operators and conditions
+=, -=, *=, /=, and **= shorten simple self-updating statements such as running totals. Relational operators produce a true-or-false result, and AND, OR, and NOT combine those results into conditions.
Make precedence visible
RPG follows conventional arithmetic precedence, with comparisons and logical operators evaluated after arithmetic. Once an expression mixes categories, explicit parentheses cost nothing at compile time and remove any doubt for the next reader. Lesson 16 works through each operator family and closes with an order-line routine that uses every one of them.
Get the lesson
Lesson 16, βExpressions, Operators, and Assignment,β 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.