Looping in ILE RPG (RPG IV) [DoW, DoU, For, Do loops]
Just like any other programming language, RPGLE too provides facility for looping over. In RPGLE (Or RPG IV) you can iterate over a chunk of statements using the following RPGLE OpCodes.
- DoW – EndDo
- DoU – EndDo
- Do – EndDo
- For – EndFor
Among the above mentioned RPGLE looping opcodes, the DoW opcode is the most widely used and recommended opcode. Now Let’s learn the syntax of These opcodes one by one.
The DoW – EndDo looping.
Here, The RPGLE statements which are to be iterated over are placed inside the two opcodes DoW and EndDo. The EndDo marks the end of the DoW block. The RPGLE statements inside these opcodes are iterated till the condition remains true. The looping condition is specified as factor2 of the DoW opcode. The looping condition is checked before beginning a new iteration. Due to this reason, the statements inside the DoW-EndDo block are not executed even for once if the looping condition evaluates to false at the beginning of the very first iterate.The following program will clarify and help you understand the concept of DoW-EndDo loop.
RPGLE Looping Example: DoW-EndDo
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++ DW@Count S 2P 0 Inz ** CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++ C DoW W@Count
Output of the above program is as given below.DSPLY 0 DSPLY 1 DSPLY 2 DSPLY 3 DSPLY 4 DSPLY 5
The DoU-EndDo Looping
The DoU-EndDo loop continues until the loop condition remains false. The Do until loop is guaranteed to be executed at least once. This is because the looping condition is checked at the end of iteration. The following example will clarify this thing further.
RPGLE Looping Example: DoU-EndDoDName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++ DW@Count S 2P 0 Inz ** CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++ C DoU W@Count = 6 C W@Count Dsply C Eval W@Count = W@Count + 1 C EndDo ** C Return
The output of the above code is as given below.DSPLY 0 DSPLY 1 DSPLY 2 DSPLY 3 DSPLY 4 DSPLY 5 DSPLY 6
The Do-EndDo loop
The Do-EndDo loop is used when we have to iterate over a block of statements for specific number (say 10, 20 100) of times. The do loop takes two arguments.
1. The Starting Counter, The maximum value and the index value.
We can specify any of the above values as variables. The following examples will clarify this loop further.
Example of RPGLE Looping: Do – EndDo loopDName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++ DW@Count S 2P 0 Inz(2) DW@CurVal S 2P 0 ** CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++ C W@Count Do 6 W@CurVal C W@CurVal Dsply C EndDo ** C Return
The output of the above program is as below.DSPLY 2 DSPLY 3 DSPLY 4 DSPLY 5 DSPLY 6
In the above example, the W@Count is the starting index and 6 is the maximum value of index. This means that the loop will be executed for 6-W@Count times. Also, if the starting index value (W@Count) is not specified, the default starting index is taken as 1. The value of the index at any time is stored in the W@CurVal. This result field is also optional. If you need this counter then define it otherwise do not.
The "For" Loop in RPGLE
The For loop in RPGLE seems very much similar to any for loop in other programming language. Here the syntax of a for loop is very much clear. You specify the looping condition as a phrase consisting of 1. The initial counter, the final counter and the step value. The counter of the loop begins from 1 and is incremented/decremented by the step(2) till the coutner becomes greater than (or less than) the max value(6).
RPGLE looping Example: For LoopDName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++ DW@Count S 2P 0 Inz(2) ** CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++ C For W@Count = 1 to 6 by 2 C W@Count Dsply C EndFor ** C Return
The output of the above program is given below.DSPLY 1 DSPLY 3 DSPLY 5