Tuesday, July 2, 2013

FOR replaces DO in RPGLE

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
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 ;
03       dou (Count = 10) ;
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 ;
03       endfor ;

The FOR operation code has three parts.

  1. The starting value of the Count field.
  2. How much to increment by. This is optional, if this is not given 1 is assumed.
  3. 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.


  1. 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.

    The manuals have been on line for years, but you can't make them download them (to say nothing about reading them).

    1. 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.

  2. Hello folks -- a caveat I learned recently. The final iteration increments and then tests the value. So...
    For Counter = 1 to 999;
    I defined Counter as 3.0 but it needs to be 4.0

    1. Chipper: Better yet, declare the loop index as an integer variable, not decimal. Integer arithmetic is more efficient than decimal.

  3. My favorite;
    DOW 0=0
    Total control in the loop.

    1. I do code my DO loops in that way, as:

      dow (1 = 1) ;

      enddo ;

    2. Yep... I wish you could do this...

      Dow (*On) ;

      EndDo ;

  4. What 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.

  5. I use "FOR" for looping through Arrays!

    Pseudo Code:
    for X = 1 to %Elem(MyArray);
    // Process...

    When I expand my array, no more HARD-CODE for the size! Awesome!

  6. RPG is an "evolving" language.
    It 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.

    1. I agree with you. If you making a "quick mod" it is certainly a good idea to leave the existing code in place.

      If you are adding looping logic to an existing then or writing a new program using the FOR is better.

    2. 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.

  7. I saw a program the other day using the DOU DONE. I have not seen this before. I always have been using DOU %EOF.

  8. This "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...


To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.