When a system processes data, it must do the processing in a particular
order. This logical order is provided by:
- The ILE RPG compiler
- The program code
The logic the compiler supplies is called the program
cycle. When you let the compiler provide the logic for your programs,
it is called cycle programming.
The program cycle is a series of steps that your program repeats until
an end-of-file condition is reached. Depending on the specifications you code,
the program may or may not use each step in the cycle.
If you want to have files controlled by the cycle, the information that
you code on RPG specifications in your source program need not specify when
records for these files are read. The compiler supplies the logical order
for these operations, and some output operations, when your source program
is compiled.
If you do not want to have files controlled by the cycle, you must end
your program some other way, either by creating an end-of-file condition by
setting on the last record (LR) indicator, by creating a return condition
by setting on the return (RT) indicator, or by returning directly using the
RETURN operation.
Note: No cycle code is generated for
subprocedures or when MAIN or NOMAIN is specified on the control specification.
See
Figure 1 shows the specific steps in the general flow of the
RPG program cycle.
Figure 1. RPG Program Logic Cycle
- 1
- RPG processes all heading and detail lines (H or D in position 17 of
the output specifications).
- 2
- RPG reads the next record and sets on the record identifying and control
level indicators.
- 3
- RPG processes total calculations (conditioned by control level indicators
L1 through L9, an LR indicator, or an L0 entry).
- 4
- RPG processes all total output lines (identified by a T in position
17 of the output specifications).
- 5
- RPG determines if the LR indicator is on. If it is on, the program ends.
- 6
- The fields of the selected input records move from the record to a processing
area. RPG sets on field indicators.
- 7
- RPG processes all detail calculations (not conditioned by control level
indicators in positions 7 and 8 of the calculation specifications). It uses
the data from the record at the beginning of the cycle.
The first cycle
The first and last time through the program cycle differ somewhat from
other cycles. Before reading the first record the first time through the cycle,
the program does three things:
- handles input parameters, opens files, initializes program data
- writes the records conditioned by the 1P (first page) indicator
- processes all heading and detail output operations.
For example, heading lines printed before reading the first record might
consist of constant or page heading information, or special fields such as
PAGE and *DATE. The program also bypasses total calculations and total output
steps on the first cycle.
The last cycle
The last time a program goes through the cycle, when no more records are
available, the program sets the LR (last record) indicator and the L1 through
L9 (control level) indicators to on. The program
processes the total calculations and total output, then all files are closed,
and then the program ends.