.st0{fill:#FFFFFF;}

RPG Old and New – Free Form Logic Example 

 October 22, 2020

By  NickLitten

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:

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;

Guess which style I prefer 🙂

RPG Old and New

NickLitten


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

  • 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

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    __CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

    Get In Touch

    I’m always looking for awesome input, feedback and critique!

    >