Getting Started
3 snippetsFree-format RPG IV basics
Hello World
**FREE
dcl-s message char(50);
message = 'Hello, World!';
dsply message;
*inlr = *on;Control Specification
**FREE
ctl-opt dftactgrp(*no) actgrp(*new);
dcl-s result int(10);
result = 100;
dsply (%char(result));
*inlr = *on;Comments
**FREE
// Single line comment
/* Multi-line
comment */
dcl-s name char(30); // Inline commentVariables & Data Types
4 snippetsDeclaration and initialization
Standalone Fields
**FREE
dcl-s customerName char(50);
dcl-s customerId packed(7:0);
dcl-s balance packed(11:2);
dcl-s isActive ind inz(*off);
dcl-s todayDate date inz(*sys);Constants
**FREE
dcl-c TAX_RATE const(0.0825);
dcl-c MAX_ITEMS const(100);
dcl-c COMPANY_NAME const('ACME Corp');Data Structures
**FREE
dcl-ds employee qualified;
id packed(7:0);
name char(50);
salary packed(9:2);
hireDate date;
end-ds;Arrays
**FREE
dcl-s prices packed(7:2) dim(100);
dcl-s names char(30) dim(50) inz(*all'');
prices(1) = 19.99;
names(1) = 'John Smith';File Operations
3 snippetsDatabase file I/O
Read File
**FREE
dcl-f custfile disk usage(*input) keyed;
read custfile;
dow not %eof(custfile);
dsply custname;
read custfile;
enddo;
*inlr = *on;Update File
**FREE
dcl-f custfile disk usage(*update) keyed;
chain (customerId) custfile;
if %found(custfile);
balance = balance + payment;
update custrec;
endif;Write to File
**FREE
dcl-f custfile disk usage(*output);
custid = 1001;
custname = 'John Doe';
balance = 1500.00;
write custrec;
*inlr = *on;Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Control Flow
4 snippetsConditional logic and loops
If-Else
**FREE
if age >= 18;
dsply 'Adult';
elseif age >= 13;
dsply 'Teenager';
else;
dsply 'Child';
endif;Select-When
**FREE
select;
when grade = 'A';
dsply 'Excellent';
when grade = 'B';
dsply 'Good';
when grade = 'C';
dsply 'Average';
other;
dsply 'Failed';
endsl;Dow Loop
**FREE
counter = 1;
dow counter <= 10;
dsply (%char(counter));
counter += 1;
enddo;For Loop
**FREE
for index = 1 to 100;
total += prices(index);
endfor;Procedures
3 snippetsSubprocedures and functions
Procedure Definition
**FREE
dcl-proc calculateTax;
dcl-pi *n packed(9:2);
amount packed(9:2);
rate packed(5:4);
end-pi;
return amount * rate;
end-proc;Call Procedure
**FREE
dcl-s subtotal packed(9:2);
dcl-s taxAmount packed(9:2);
subtotal = 100.00;
taxAmount = calculateTax(subtotal : 0.0825);
total = subtotal + taxAmount;Void Procedure
**FREE
dcl-proc logMessage;
dcl-pi *n;
msg char(100) const;
end-pi;
dsply msg;
end-proc;
logMessage('Processing started');Built-in Functions
4 snippetsCommonly used BIFs
String Functions
**FREE
name = %trim(inputName);
length = %len(name);
upper = %upper(name);
lower = %lower(name);
substring = %subst(name : 1 : 5);Numeric Functions
**FREE
absValue = %abs(-100);
squareRoot = %sqrt(144);
rounded = %int(123.456);
remainder = %rem(10 : 3);Date Functions
**FREE
todayDate = %date();
currentTime = %time();
timestamp = %timestamp();
dayOfWeek = %subdt(todayDate : *d);Conversion Functions
**FREE
charValue = %char(123);
intValue = %int('456');
decValue = %dec(78.9 : 5 : 2);Error Handling
2 snippetsException handling with monitor
Monitor Block
**FREE
monitor;
result = numerator / denominator;
dsply ('Result: ' + %char(result));
on-error;
dsply 'Division error occurred';
endmon;Specific Errors
**FREE
monitor;
chain (custId) custfile;
if not %found();
dsply 'Customer not found';
endif;
on-error 1211;
dsply 'Record locked';
on-error 1218;
dsply 'Record not found';
endmon;Embedded SQL
3 snippetsSQL in RPG programs
Select Statement
**FREE
exec sql
select name, balance
into :custName, :custBalance
from customers
where custid = :customerId;
if sqlcode = 0;
dsply custName;
endif;Cursor Processing
**FREE
exec sql declare c1 cursor for
select custid, name from customers;
exec sql open c1;
exec sql fetch c1 into :custId, :custName;
dow sqlcode = 0;
dsply custName;
exec sql fetch c1 into :custId, :custName;
enddo;
exec sql close c1;Update with SQL
**FREE
exec sql
update customers
set balance = balance + :payment
where custid = :customerId;
if sqlcode = 0;
dsply 'Update successful';
endif;