TexAS400 Tutorial


RPG III - Adding a Loop, IF and Subroutine to the RPG IV Program

In the previous topic, we added a DO loop to read all records in the file, an IF statement to test the state for "TN" and a subroutine.

You should be beginning to sense that RPG III is powerful but ugly.

Let's recreate this program using the easier to read syntax of RPG IV. The simplest way to do this is to copy TUTR003 in the QRPGLESRC source object and then modify the copied program.

Use PDM to copy TUTR003. Start PDM (STRPDM). You should see something like:


------------------------------------------------------------------------------------------
                 AS/400 Programming Development Manager (PDM)  
                                                               
Select one of the following:                                   
                                                               
     1. Work with libraries                                    
     2. Work with objects                                      
     3. Work with members                                      
     4. Work with projects                                     
     5. Work with groups                                       
     6. Work with parts                                        
                                                               
     9. Work with user-defined options                         
------------------------------------------------------------------------------------------
Key in 3 and hit ENTER. Fill in the screen to look like:

                      Specify Members to Work With                        
                                                                          
Type choices, press Enter.                                                
                                                                          
  File  . . . . . . . . . .   QRPGLESRC    Name, F4 for list              
                                                                          
    Library . . . . . . . .     user999    *LIBL, *CURLIB, name         
                                                                          
  Member:                                                                 
    Name  . . . . . . . . .   *ALL         *ALL, name, *generic*          
    Type  . . . . . . . . .   *ALL         *ALL, type, *generic*,  
Of course, change the USER999 to your user ID. Hit ENTER now.

Your screen should look like:


                          Work with Members Using PDM               
                                                                    
File  . . . . . .   QRPGLESRC                                       
  Library . . . .     USER999              Position to  . . . . .   
                                                                    
Type options, press Enter.                                          
 2=Edit         3=Copy  4=Delete 5=Display       6=Print     7=Rename
 8=Display description  9=Save  13=Change text  14=Compile  15=Create
                                                                    
Opt  Member      Type        Text                                   
     TUTR002     RPGLE       Change the first record in CUST file   
     TUTR003     RPGLE       TUTR002 modified to modern style       
Key a 3 as the option next to TUTR003. Hit ENTER and then fill in the screen to copy the program and name it TUTR005:

                                 Copy Members                     
                                                                  
 From file . . . . . . . :   QRPGLESRC                              
   From library  . . . . :     USER999                            
                                                                  
 Type the file name and library name to receive the copied members
                                                                  
   To file . . . . . . . .   QRPGLESRC      Name, F4 for list       
     To library  . . . . .     USER999                            
                                                                  
 To rename copied member, type New Name, press Enter.             
                                                                  
 Member         New Name                                          
 TUTR003        TUTR005                                           
Hit ENTER and you should now see the copied program in your list. You may want to change the description of the program so you'll remember which program does what.

Key a 2 next to TUTR005 so you can change the source statements to the program.

The old syntax on the IF and DO statements is legal. So you can choose between:


C     *in90         DOWEQ     *off                    

AND
  
C                   DoW       *in90 = *off
Not only is the second form more readable, it is more free form. You can put spaces and parentheses in the conditional part of the statement like this:

C                   DoW       (*in90    =    *off)
This may not look like such a big deal, but compare this old style IF statement:


C     *in90         IFEQ      *off         
C     STATUS        ANDEQ     'D'          
C     STATUS        OREQ      'A'          
C     CITY          ANDEQ     'ATHENS'     
with the newer style

C                   If        (In90 = *off)    and                   
C                             ((STATUS = 'D') or (STATUS = 'A'))  and
C                             (CITY = 'ATHENS')                      
The newer style is not only more readable but you can actually figure out how the conditions are checked.

One of the other big changes in form from RPG III to RPG IV is that the subroutine names can be only 6 characters in RPG III. In RPG IV, the subroutine names can be 14 characters. These longer names make the programs many times easier to follow.

So using this new found knowledge about RPG IV, change the program to look like:


FCUST      UF   E             disk                                     
C*---------------------------------------------------------------------
C                   DoW       *in90 = *off                             
C                   Read      CUST                                   90
C                   If        *in90 = *off                             
C                   Exsr      UpdateCSREC                              
C                   EndIf                                              
C                   EndDo                                              
C                                                                      
C                   Eval      *inlr = *on                              
C                   Return                                             
C*---------------------------------------------------------------------
C     UpdateCSREC   BegSR                                              
C                                                                      
C                   If        CSSTE  = 'TN'                            
C                   Eval      CSZIP  = '98765-0000'
C                   Update    CSREC                                    
C                   EndIf                                              
C                                                                      
C                   EndSR                                              
Compile the program with option 14 and you are done.

If you really hate using the indicators - and you probably do - you can use one of the new built in functions to check to see if the last record has been read. That is, you are checking to see if the file is at END OF FILE or EOF.

The Do loop can be written:


C                   DoW       not  %eof(CUST)                             
C                   Read      CUST                                   
C                   If        not %eof(CUST)                             
C                   Exsr      UpdateCSREC                              
C                   EndIf                                              
C                   EndDo                                           
In fact, the %EOF function will refer to the last file used by default. So you could write:

C                   DoW       not  %eof                             
C                   Read      CUST                                   
C                   If        not %eof                             
C                   Exsr      UpdateCSREC                              
C                   EndIf                                              
C                   EndDo                                 
If you have ever used Visual Basic, the EOF function will look familiar.

That's it!

 

 

Back to Table of Contents   |   Main Page