In the post CL does DO I said that the DOFOR was similar to the FOR operation code in RPGLE. I use the FOR operation in RPGLE, but having looked at the code created by my colleagues I appear to the only one who does. The FOR operation allows us to "loop", perform the same section of code a specified number of times.
When programming in fixed format RPGLE if I needed to perform a section of code ten times many of us would code:
01 C 1 do 10 02 03 C enddo
Why do I do that? I always code RPGLE in lower case.
In RPG/free it comes more complicated as the DO operation not supported. Therefore, I would need to do something like this:
01 Count = 1 ; 02 03 dou (Count = 10) ; 04 05 Count += 1 ; // Increment Count by 1 06 enddo ;
IBM introduced the FOR operation, to replace the DO, in OS i (OS/400) 5.3.
If I want to perform a section of code ten times I now just:
01 for Count = 1 to 10 ; 02 03 endfor ;
The FOR operation code has three parts.
- The starting value of the Count field.
- How much to increment by. This is optional, if this is not given 1 is assumed.
- The value at which to stop.
I could have coded the FOR as:
01 for Count = 1 by 1 to 10 ;
This allows you to increment Count by any value you desire.
I can also count down:
01 for Count = 10 by 1 downto 1 ;
More information on the FOR operation can be found on the IBM website here»
This article was written for IBM i 7.1, and it should work with earlier releases too.