General RPG IV Program Cycle

Figure 1 shows the specific steps in the general flow of the RPG IV program cycle. A program cycle begins with step 1 and continues through step 7, then begins again with step 1.

The first and last time a program goes through the RPG IV cycle differ somewhat from the normal cycle. Before the first record is read the first time through the cycle, the program resolves any parameters passed to it, writes the records conditioned by the 1P (first page) indicator, does file and data initialization, and processes any heading or detail output operations having no conditioning indicators or all negative conditioning indicators. For example, heading lines printed before the first record is read might consist of constant or page heading information or fields for reserved words, such as PAGE and *DATE. In addition, the program bypasses total calculations and total output steps on the first cycle.

During the last time a program goes through the cycle, when no more records are available, the LR (last record) indicator and L1 through L9 (control level) indicators are set on, and file and data area cleanup is done.

Figure 1. RPG IV Program Logic Cycle
This figure illustrates the RPG IV program logic cycle
 1 
All heading and detail lines (H or D in position 17 of the output specifications) are processed.
 2 
The next input record is read and the record identifying and control level indicators are set on.
 3 
Total calculations are processed. They are conditioned by an L1 through L9 or LR indicator, or an L0 entry.
 4 
All total output lines are processed. (identified by a T in position 17 of the output specifications).
 5 
It is determined if the LR indicator is on. If it is on, the program is ended.
 6 
The fields of the selected input records are moved from the record to a processing area. Field indicators are set on.
 7 
All detail calculations are processed (those not conditioned by control level indicators in positions 7 and 8 of the calculation specifications) on the data from the record read at the beginning of the cycle.