RPG/400 Help


Structured Programming Operations

The structured programming operations are:

The DO operation allows the processing of a group of calculations zero or more times starting with the value in factor 1, incrementing each time by a value on the associated ENDDO operation until the limit specified in factor 2 is reached.

The DOUxx operation allows the processing of a group of calculations one or more times based on the results of comparing factor 1 and factor 2. The end of a DOUxx operation is indicated by an ENDDO operation.

The DOWxx operation allows the processing of a group of calculations zero or more times based on the results of comparing factor 1 and factor 2. The end of a DOWxx operation is indicated by an ENDDO operation.

The LEAVE operation interrupts control flow prematurely and transfers control to the statement following the ENDDO operation of an iterative structured group. The ITER operation causes the next loop iteration to occur immediately.

An IFxx operation allows the processing of a group of calculations based on the results of comparing factor 1 and factor 2. The ELSE operation allows you to specify a group of calculations to be processed if the IFxx condition is not satisfied. The end of an IFxx group is indicated by ENDIF.

The SELEC, WHxx, and OTHER group of operations are used to conditionally process one of several alternative sequences of operations. The beginning of the select group is indicated by the SELEC operation. The WHxx operations are used to choose the operation sequence to process. The OTHER operation is used to indicate an operation sequence that is processed when none of the WHxx conditions are fulfilled. The end of the select group is indicated by the ENDSL operation.

The ANDxx and ORxx operations are used with the DOUxx, DOWxx, WHxx, and IFxx operations to specify a more complex condition than the comparison of a single factor 1 and factor 2 pair. The ANDxx operation has higher precedence than the ORxx operation.

Figure 5. Example of AND/OR Precedence

*...1....+....2....+....3....+....4....+....5....+....6....+....7...
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++
C*
C* In the following example, indicator 25 will be set on only if the
C* first two conditions are true or the third condition is true.
C* Algebraically, this would be represented as:
C* ( ( (FIELDA > FIELDB) & (FIELDA >= FIELDC) ) | (FIELDA < FIELDD) )
C*
C           FIELDA    IFGT FIELDB
C           FIELDA    ANDGEFIELDC
C           FIELDA    ORLT FIELDD
C                     SETON                     25
C                     ENDIF

The CASxx operation allows a subroutine to be conditionally selected for processing. An ENDCS operation ends a CASxx group. For more information about the CASxx operation, see Compare Operations.

A DO, DOUxx, DOWxx, IFxx, or SELEC operation (with or without ANDxx or ORxx operations), and an ENDyy operation, delimit a structured group. The ENDDO operation ends each DO, DOUxx, and DOWxx group or causes the structured group to be reprocessed until the specified ending conditions are met. The SELEC must end with an ENDSL. An IFxx operation and an IFxx operation with an ELSE operation must end with an ENDIF operation. Using END gives you the same results as using ENDIF, ENDSL, or ENDDO.

The rules for making the comparison on the ANDxx, DOUxx, DOWxx, IFxx, ORxx and WHxx operation codes are the same as those given under Compare Operations.

In the ANDxx, CASxx, DOUxx, DOWxx, IFxx, ORxx, and WHxx operations, xx can be:

xx
Meaning
GT
Factor 1 is greater than factor 2.
LT
Factor 1 is less than factor 2.
EQ
Factor 1 is equal to factor 2.
NE
Factor 1 is not equal to factor 2.
GE
Factor 1 is greater than or equal to factor 2.
LE
Factor 1 is less than or equal to factor 2.
Blanks
Unconditional processing (CASxx only).

In the ENDyy operation, yy can be:

yy
Meaning
CS
End for CASxx operation.
DO
End for DO, DOUxx, and DOWxx operation.
IF
End for IFxx operation.
SL
End for SELEC operation.
Blanks
End for any structured operation.
Note:
The yy in the ENDyy operation is optional.

If a structured group, in this case a do group, contains another complete structured group, together they form a nested structured group. Structured groups can be nested to a maximum depth of 100 levels. The following is an example of nested structured groups, three levels deep:


*---------DO
|     *---DO
|     *---ENDDO
|  *------IFxx
|  |  *---SELEC
|  |  |   WHxx
|  |  *---ENDSL
|  *------ELSE
|  *------ENDIF
*---------ENDDO

Remember the following when specifying structured groups:


[ Top of Page | Previous Page | Next Page | Table of Contents ]