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 IBM i (OS400) V5R3.
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.
Even though RPG programmers tend to be creatures of habit, I'll bet you dollars to donuts they didn't even realize the FOR operation code was avaiable. There's still times when I write software in RPG for a customer and one of their staff RPG programmers says, "Wow, I didn't know you could do that". Or even better yet, asks me what language it's written in.
ReplyDeleteThe manuals have been on line for years, but you can't make them download them (to say nothing about reading them).
The thing I hate though is I like consistency. You've written all this code with DO. Now FOR comes along so you start using FOR. Now you have all this old code of DO and new code of FOR. There's never enough time to go change the old code. Argh.. OCD I guess.
DeleteHello folks -- a caveat I learned recently. The final iteration increments and then tests the value. So...
ReplyDelete/free
For Counter = 1 to 999;
EndFor;
/end-free
I defined Counter as 3.0 but it needs to be 4.0
Chipper: Better yet, declare the loop index as an integer variable, not decimal. Integer arithmetic is more efficient than decimal.
DeleteMy favorite;
ReplyDeleteDOW 0=0
...
...
Enddo
Total control in the loop.
I do code my DO loops in that way, as:
Deletedow (1 = 1) ;
enddo ;
Yep... I wish you could do this...
DeleteDow (*On) ;
EndDo ;
That is how CL does its DO loops, see CL does DO
DeleteWhat does it look like in fixed-format? With RPG IV I am a creature of habit. Been doing it over 30 years and I lke the fixed format. Heck, I still take advantage of the cycle & indicators as needed. That's what they're there for & make sense to me. Thank goodness I can retire in a little over 2.5 years.
ReplyDeleteI use "FOR" for looping through Arrays!
ReplyDeletePseudo Code:
for X = 1 to %Elem(MyArray);
// Process...
EndFor;
When I expand my array, no more HARD-CODE for the size! Awesome!
RPG is an "evolving" language.
ReplyDeleteIt wasn't designed and created perfectly like so many new languages of today.
Are you re-writing code so it looks pretty or uses the latest op code? Bad idea. It may look ugly but if it's doing the job you'd be better served to leave it alone. I learned long ago not to do that.
I agree with you. If you making a "quick mod" it is certainly a good idea to leave the existing code in place.
DeleteIf you are adding looping logic to an existing then or writing a new program using the FOR is better.
I date back to S/36 RPG. I've kept up fairly well, meaning all new code has been /free (now **free) for a few years. I have to support many old programs that are migrated RPGiii. When I have to touch one, I convert it fully to **free -- no more GOTOs in any of its forms including the allowed iter and leave. There is some risk, but overall I find and fix more bugs than I create. It forces me to research intent behind much of the code. I've found and removed stuff that was obsolete years ago, but still ran. The result is streamlined code that new-hires can make sense of. It's so much easier and less risky to add features after the code is cleaned up than to clean up little bits *while* adding features. And the insight I've gained into what the programs do has boosted my career with the company.
DeleteI saw a program the other day using the DOU DONE. I have not seen this before. I always have been using DOU %EOF.
ReplyDeleteThis "DONE" is probably a variable that gets set up somewhere inside the loop or just another way of having that total control of the loop...
ReplyDelete