RPGLE Program Structure Explained

An RPGLE program becomes less intimidating when you stop seeing it as one large block of code.
Most small programs can be understood as a series of sections, and each section has a different job.

This lesson explains RPGLE program structure without asking you to learn advanced syntax.
The earlier reading lesson taught you how to move through source, while this lesson explains why each major section exists.
You will use the same subtotal, tax, and total example from the previous lessons so you can focus on structure instead of a new business problem.

Estimated reading time: 8 to 10 minutes

Quick Summary

  • When present, control options normally appear near the top of an RPGLE program.
  • File declarations identify files the program may use.
  • Variable declarations prepare the data needed later.
  • Main logic performs the program’s real work.
  • Final statements end the program cleanly.

Reader Prerequisites

  • You should know that RPGLE is a programming language used on IBM i.
  • You should be able to read a short source member from top to bottom.
  • You should understand why descriptive names make code easier to follow.
  • You do not need to understand data types, file processing, procedures, or advanced keywords yet.

Review How to Read an RPGLE Program From Top to Bottom if you want the reading strategy first.
Then use RPGLE Naming Conventions and Code Organization to refresh the naming ideas used here.

Learning Outcomes

By the end of this lesson, you will be able to:

  • Name the major sections in a simple RPGLE program.
  • Explain why each section exists.
  • Tell setup information apart from executable work.
  • Find where the program begins its work and where it ends.
  • Describe a small program by section without decoding every keyword.

Why Program Structure Matters

Program structure gives the source a predictable shape.
When you know that shape, you can quickly answer useful questions.
What rules apply to this program, which files does it use, and what data does it prepare?
Where does it calculate a result, and where does it finish?

Without that map, every line can seem equally important.
With the map, you can first identify a line’s purpose and study its exact syntax later.
This also makes programs easier to review, maintain, and discuss with another developer.

The Main Parts of an RPGLE Program

For this beginner lesson, picture the sample’s RPGLE program structure as five main parts:

  1. Control options set high-level program rules.
  2. File declarations identify files used by the program.
  3. Variable declarations prepare named values.
  4. Main logic performs calculations and produces output.
  5. Program end statements finish the run.

Not every program contains every part, and control options may be omitted.
When these parts are present, they usually appear in that general order.
Blank lines and short comments can make their boundaries easier to see.

Control Options

When a program has control options, they belong near the top of the source, immediately after the free-form directive in this example.
They describe high-level choices about how the program should operate.

As a beginner, treat this section as the program’s overall setup.
You do not need to memorize the individual options yet.
The important skill is recognizing that this section applies to the program as a whole and comes before declarations and executable work.

File Declarations

The file declaration section identifies a file or device resource the program may use.
In the shared example, samplePrint appears in this section.

A file declaration prepares access to a resource; it is not the business calculation itself.
That difference matters when you scan unfamiliar code.
You can note which files exist, then continue downward to find what the program actually does with its data.

Here, samplePrint is a program-described printer file retained only to show where a file declaration belongs.
This small example does not write printer output.

Some programs have several file declarations, while others have none.
The section depends on what the program needs.

Variable Declarations

Variable declarations introduce the named pieces of data used later.
Our example keeps subtotal, taxRate, total, and message together in this section.

Declarations are near the top so a reader can learn what data is available before reaching the work.
They act like a short inventory: the starting amount, the tax rate, the calculated total, and the message that will be displayed.

For now, focus on the names and their location.
The next lesson will explain variables and data types in more detail.

Main Logic

Main logic is where the program performs its real work.
In this example, it calculates total, builds message, and displays that message.

This section is different from the declarations above it.
The declarations prepare things; the main logic uses them.
When you want to discover what a small program does, this is the section you will usually study most closely.

Real programs may contain decisions, repeated actions, and other work here.
Those topics can wait.
The important point now is that executable statements form the active part of the program.

Program End

The final section tells you that the program has completed its work.
In the sample, the last two statements form the program termination section.

You do not need a detailed explanation of those statements yet.
Simply recognize them as the clean ending of this small program.
Once you reach this section, your top-to-bottom structural walkthrough is complete.

Walk Through a Small Program

Here is the same business example used in the previous two lessons, now labeled to show its sections clearly:

**free

// Control options
ctl-opt dftactgrp(*no) actgrp(*caller);

// File declaration
dcl-f samplePrint printer(132) extfile('QPRINT');

// Variable declarations
dcl-s subtotal packed(7:2) inz(125.00);
dcl-s taxRate packed(5:4) inz(0.0750);
dcl-s total packed(7:2);
dcl-s message varchar(40);

// Executable logic
total = subtotal + (subtotal * taxRate);
message = 'Total amount: ' + %char(total);
dsply message;

// Program termination
*inlr = *on;
return;

The **free line sits at the beginning and is the fully free-form compiler directive used by this sample.
The ctl-opt line belongs to control options, where the program-wide setup is placed.

The samplePrint line belongs to file declarations.
It is retained to show that a printer file is part of the program’s setup, even though this short display example does not perform file-processing work.

The four dcl-s lines belong to variable declarations.
They prepare subtotal, taxRate, total, and message before any calculation begins.

The next three lines belong to main logic.
The first calculates the total, the second prepares the message, and the third displays it.
We are identifying the job of the section, not studying each symbol or built-in operation.

The last two lines belong to program termination.
They mark the end of the program’s work.
Read this sample again using only the section labels, and its overall shape should become easy to spot.

Common Beginner Mistakes

  • Treating every line as though it performs business logic.
  • Skipping control options because they look unfamiliar.
  • Confusing declarations with executable statements.
  • Looking for the calculation inside the file or variable sections.
  • Assuming every program must contain exactly the same sections.
  • Trying to memorize syntax before understanding why a section exists.
  • Missing the final statements that show where the program ends.

FAQ

What is the first section of an RPGLE program?

In this sample, the source begins with the **free compiler directive and then the control options appear near the top.
Control options are optional, so a program without them may begin its declarations immediately after any opening directives.

Why are declarations near the top?

They introduce files and data before the program uses them.
This lets a reader see what resources are available before reaching the executable work.

Where does the real work happen?

The real work happens in the main logic section.
That is where the sample calculates the total, creates the message, and displays the result.

Do all RPGLE programs have the same layout?

No.
The exact layout depends on the program’s purpose and the team’s standards.
However, the simple top-to-bottom pattern in this lesson is a useful starting map for many programs.

Is a file declaration executable logic?

No.
A file declaration prepares a file for use; it does not perform the program’s business work by itself.

What should I study after this?

Study RPGLE variables and data types next.
That lesson will add detail to the variable declaration section you learned to recognize here.

Key Takeaways

  • RPGLE program structure divides source into sections with different purposes.
  • Control options provide high-level setup.
  • File and variable declarations prepare resources and data.
  • Main logic is where the program performs its work.
  • Program termination marks a clean ending.
  • Recognizing sections first makes later syntax lessons easier.

Continue Your Learning

Previous: RPGLE Naming Conventions and Code Organization

Current: RPGLE Program Structure Explained

Next: RPGLE Variables and Data Types

Return to the RPGLE learning path.



Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top