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

  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.

8 comments:

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

    ReplyDelete
    Replies
    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.

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

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

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

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

      dow (1 = 1) ;

      enddo ;

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

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

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

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

    ReplyDelete

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.