October 22

6 comments

RPG Old and New – Free Form Logic Example

By NickLitten

October 22, 2020

RPG, freeformat, RPGLE

Free Form RPG ILE Logic Example

I was recording a screencast about modernizing some old RPG400 to ILE RPG this morning and had to knock up this little code example showing the difference between legacy RPG400 code and modern RPG ILE code.

Note, this is not an example of good code (it’s crappy old RPG code) but it’s just an example of the difference layouts and syntax of the three main version of RPG language – RPG2 the ancient logic cycle based stuff, RPG3/400 the old column-based stuff and RPG4 the newer/current free format stuff.

RPG Old and New

This code snippet, is an example that shows the same code, and logic in both old and new styles:

Old rpg vs new rpgle

RPG3/400

 C                   DOU       %EOF(FILENAME)

 C                   READ(E)   FILENAME

 C                   IF        %ERROR
 C                   EVAL      MSG = '* SOMETHING DREADFUL HAPPENED'
 C                   LEAVE
 C                   END

 C                   IF        %EOF
 C                   EVAL      MSG = '* End of File'
 C                   LEAVE
 C                   ENDIF

 C     KEYFLD        CHAIN     OTHERFILE
 C                   IF        %FOUND(OTHERFILE)
 C                   EVAL      FILEFIELD = OTHERFIELD
 C                   UPDATE    OTHERFILE
 C                   ENDIF


 C                   ENDDO 

RPGLE /freeformat

   dou %eof(Filename);

     read(e) Filename;

     if %error;
       msg = '* something dreadful happened';
       leave;
     endif;

     if %eof;
       msg = '* End of File';
       leave;
     endif;

     chain keyfld OtherFile;
     if %found(otherfile);
       filefield = otherfield;
       update otherfile;
     endif;

   enddo;

Note: I would further update this code to put the logic inside an if|elseif|else loop. It’s functionally the same but purely a personal style I prefer. I used to use select|when groups (which do the same thing) but tend to use if|elseif|else nowadays. No particular reason they both work the same way:

   dou %eof(Filename);

     read(e) Filename;

     if %error;
       msg = '* something dreadful happened';
       leave;
     elseif %eof;
       msg = '* End of File';
       leave;
     else;
       chain keyfld OtherFile;
       if %found(otherfile);
         filefield = otherfield;
         update otherfile;
       endif;
     endif;

   enddo;

Another style I like is using a SELECT group, rather than the if/elseif:

   dou %eof(Filename);

     read(e) Filename;

     select;
     when %error;
       msg = '* something dreadful happened';
       leave
     when %eof;
       msg = '* End of File';
       leave;
     other;
       chain keyfld OtherFile;
       if %found(otherfile);
         filefield = otherfield;
         update otherfile;
       endif;
     endsl;

   enddo;

Guess which style I prefer 🙂

  • I was using similar examples a lot when I was selling the Arcad RPG Converter tool.. “I don’t have a clue what that is to the left, but the code to the right is easy to understand” was a typica comment from an non RPG-developer…

  • DoU operations are meant for 1-n occurrences, while DoW operations are meant for 0-n occurrences.
    The correct coding for reading a database should therefore be DoW (ie. Read, DoW not %EOF, Read, EndDo) but I often see people adding four lines of code to save a single extra read ?
    BTW. The above example could maybe be rewritten in a single line by an SQL statement, ie. Update/where .. in/Select

    • I agree completely – this was just a code example to make it clear of the layout differences. DoU always goes in once, effectively just gives us a big DO loop that can be left using LEAVE;

      When coding I prefer this syntax:

      read something;
      Dow not %eof;
      //logic
      read something;
      enddo;

      🙂

  • Hi. Your program in RPG III
    *************** Beginning of data *************************************
    FFILENAM IPEAE K DISK
    FOTHER UF E K DISK
    C KEY CHAINOTHER 40
    C N40 EXCPTUPDATE
    CLR MOVE T,2 MSG 60
    OOTHER E UPD UPDATE
    O OTFF
    **
    SOMETHING DREADFUL HAPPENED
    END OF FILE

  • but if the as/400 donts install FTP, the sql is dude…. and if the chief dont understand it…. this is real in Mexico especial in MTY

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips

    >