AI Generate RPG docs instantly

RPG Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

3 snippets

Free-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 comment

Variables & Data Types

4 snippets

Declaration 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 snippets

Database 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.

Try Free

Control Flow

4 snippets

Conditional 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 snippets

Subprocedures 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 snippets

Commonly 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 snippets

Exception 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 snippets

SQL 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;

More Cheat Sheets

FAQ

Frequently asked questions

What is a RPG cheat sheet?

A RPG cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in RPG. It helps developers quickly look up syntax without searching through documentation.

How do I learn RPG quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important RPG concepts?

Key RPG concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my RPG code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your RPG code using AI.

Code Conversion Tools

Convert RPG to Other Languages

Easily translate your RPG code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate RPG Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week