Thursday, September 16, 2010

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



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  = 987650000                      
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!

  Notes:


RPG 3 : C     *in90         DOWEQ     *off  
RPG4  : C     *in90         DOWEQ     *off
Or C                   DoW       (*in90    =    *off)

--------
RPG3:

C     *in90         IFEQ      *off        
C     STATUS        ANDEQ     'D'        
C     STATUS        OREQ      'A'        
C     CITY          ANDEQ     'ATHENS'

RPG4:

C                   If        (In90 = *off)    and                  
C                             ((STATUS = 'D') or (STATUS = 'A'))  and
C                             (CITY = 'ATHENS')

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

RPG III - Adding a Loop, IF and Subroutine



TexAS400 Tutorial



RPG III - Adding a Loop, IF and Subroutine
In a previous topic, you should have written a very short RPG III program to change the first record in the CUST file.
Let's add so more statements to the program so that it will read all records in the file and change the zip code for everyone in Tennessee.
You may want to read this very short article on data navigation first:
Data Navigation
Your program will still reference the CUST file, so, use DSPLIBL to display your library list. If you don't see your user library in the list, use the CHGCURLIB USER999 command. This will change your current library to USER999. Remember that when I say USER999, I mean your user ID.
You can look at your data file with the command: RUNQRY QRYFILE(CUST)
Now, use PDM to copy your old program. 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  . . . . . . . . . .   QRPGSRC      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:

                                                                              
File  . . . . . .   QRPGSRC                                                   
  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 module..
                                                                              
Opt  Member      Type        Text                                             
        TUTR001     RPG         Change the first record in CUST file                                                
Key a 3 as the option next to TUTR001. Hit ENTER and then fill in the screen to copy the program and name it TUTR004:

                                 Copy Members                     
                                                                  
 From file . . . . . . . :   QRPGSRC                              
   From library  . . . . :     USER999                            
                                                                  
 Type the file name and library name to receive the copied members
                                                                  
   To file . . . . . . . .   QRPGSRC      Name, F4 for list       
     To library  . . . . .     USER999                            
                                                                  
 To rename copied member, type New Name, press Enter.             
                                                                  
 Member         New Name                                          
 TUTR001        TUTR004                                           
Hit ETER 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. Your screen should look like:Remember from your PDM lesson that this is trying to show you a list of all of the RPG
Work with Members Using PDM
                                                                    
 File  . . . . . .   QRPGSRC                                        
   Library . . . .     USER000              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                                  
      TUTR001     RPG         Change the first record in CUST file  
      TUTR004     RPG         Change all TN records in CUST file    
Key a 2 next to TUTR004 so you can change the source statements to the program.Before we make the changes to the program we need to discuss different ways to write a loop. We want a loop that will read every record in the CUST file and change the records that have TN (Tennessee) as the state.
I always think of a loop as saying:

 Try to Read a record from the file
 If there was a record then
  change it
  update it
 Go back and try to read the next record
Remember that when RPG III reads a record, you must specify which indicator to turn on it there are no more records; that is, it is END OF FILE.You could write the instructions above in RPG III like this:

FCUST    UF  E                    DISK                 
C           READIT    TAG                              
C                     READ CUST                     90 
C           *IN90     IFEQ *OFF                        
C                     MOVE *BLANK    CSADR1            
C                     MOVEL'456 OAK' CSADR1            
C                     UPDATCSREC                       
C                     GOTO READIT                      
C                     ENDIF                            
C                     MOVE *ON       *INLR             
C                     RETRN                            
This program says to define a file named CUST.Then, name the first line of the Calculations READIT. The, try to read a record (if END OF FILE, move ON to indicator# 90) If indicator# 90 is *OFF Move … Move … Update … Go back to the first line of the program Turn on the "magic" LR indicator End the program
This will work. The only problem is that for about 15 years, it has been considered bad style to use GOTO statements. This is an industry trend that includes all languages and all computers.
The reason is that long programs are hard to understand when there are a lot of GOTO statements. Programs with lots of GOTO's are nicknamed spaghetti programs because the statements can be as hard to follow as a strand of spaghetti.
SO, the industry prefers that we use DO statements. We can use a DO WHILE or a DO UNTIL. This still leaves us with 2 good ways to write the loop. I prefer the first because it looks a lot like the GOTO loop but without the GOTO:

FCUST    UF  E                    DISK                
C           *IN90     DOWEQ*OFF                       
C                     READ CUST                     90
C           *IN90     IFEQ *OFF                       
C                     MOVE *BLANK    CSADR1           
C                     MOVEL'456 OAK' CSADR1           
C                     UPDATCSREC                      
C                     ENDIF                           
C                     ENDDO                           
C                     MOVE *ON       *INLR            
C                     RETRN                           
This program defines the file and then starts a DO loop.The DOWEQ statement says that as long as indicator # 90 is OFF, execute all of the statements until the ENDDO (End DO) statement.
Then, start back at the top (a lot like a GOTO). If indicator # 90 is still OFF, execute the statements again. Eventually, the program will read all 1,000+ records in the file. When it does, indicator # 90 will be turned ON.
When indicator # 90 is ON, the program will not update the record and it will not continue to loop.
The other way to code the loop is with what is called a priming read statement. This method needs 2 READ statements but does not need the IF statement.
If looks like:

FCUST    UF  E                    DISK                    
C                     READ CUST                     90    
C           *IN90     DOWEQ*OFF                           
C                     MOVE *BLANK    CSADR1               
C                     MOVEL'456 OAK' CSADR1               
C                     UPDATCSREC                          
C                     READ CUST                     90    
C                     ENDDO                               
C                     MOVE *ON       *INLR                
C                     RETRN                               
Here, the first record is read before the DO begins. The statements are executed and the READ statement is at the end of the DO loop.Either of these 2 methods is considered good code. I prefer the first.
Using the first acceptable loop, add the IF statement to test to see if the state is TN. Also, replace the code so that it changes the ZIP to 987650000.
First, do this by putting in an IF statement like this:

C           CSSTE     IFEQ 'TN'                      
C                     MOVE 987650000 CSZIP           
C                     UPDATCSREC                     
C                     ENDIF                          

So, the whole program looks like:

FCUST    UF  E                    DISK                
C           *IN90     DOWEQ*OFF                       
C                     READ CUST                     90
C           *IN90     IFEQ *OFF                       
C           CSSTE     IFEQ 'TN'                       
C                     MOVE 987650000 CSZIP            
C                     UPDATCSREC                      
C                     ENDIF                           
C                     ENDIF                           
C                     ENDDO                           
C                     MOVE *ON       *INLR            
C                     RETRN 
This is considered good code by most programmers. But it really helps to move the update logic to a separate place in the program as a subroutine. This is done here only to help readability. It doesn't change the outcome.In RPG, subroutines begin with a BEGSR (BEGIN SUBROUTINE) statement which also has the name of the subroutine. They end with ENDSR (END SUBROUTINE). They are run with the EXSR (EXECUTE SUBROUTINE) command.
If we move the update logic to a subroutine named UPD, the program looks cleaner. Also, we can add "COMMENTS" records. Any statement with a * after the record type, is a "COMMENT". In fact you can even remove the record type (the C or F in column 6).
With a few comment lines added to define the sections of the program, we get a pretty readable program:

FCUST    UF  E                    DISK                 
 *-----------------------------------------------------
C           *IN90     DOWEQ*OFF                        
C*
C                     READ CUST                     90 
C           *IN90     IFEQ *OFF                        
C                     EXSR UPD                         
C                     ENDIF                            
C                     ENDDO                            
C*                                                     
C                     MOVE *ON       *INLR             
C                     RETRN                            
 *-----------------------------------------------------
C           UPD       BEGSR                            
C*                                                     
C           CSSTE     IFEQ 'TN'                        
C                     MOVE 987650000 CSZIP             
C                     UPDATCSREC  
C                     ENDIF       
C*                                
C                     ENDSR        
 *------------------------------------------------------
Now, compile this (remember… option 14) and run it (CALL TUTR004).Then use RUNQRY QRYFILE(CUST) to see the data. If you scroll right by hitting F20 (actually Shift and F8) you will see that you changed the zip in all of the records with TN as CSSTE.
Are you starting to have fun? I hope so.
The next lesson will be short. It will put these statements in the RPG ILE version. You will see how much prettier ILE code is than RPG III.


Note:

  • Logic to read and update the file


        Try to Read a record from the file
If there was a record then
change it
update it
Go back and try to read the next record


  • Long programs are hard to understand when there are a lot of GOTO statements.So Do while and So Until loops are used.



  • To scroll right in the file F20:Shift+F8





 

RPG Lesson 2 - Converting RPG-III to RPG IV

First RPG IV Program
Hopefully you were able to complete the previous topic and compile a simple RPG III program. Though introduced in 1994, RPG IV is known as the "new" RPG. Because of the huge number of programs written in RPG III, the industry has been slow to adopt RPG IV. It is undoubtedly the way of the future on the AS/400.
RPG III is more accurately known as RPG/400 when it runs on an AS/400. RPG IV supports the "Integrated Language Environment" concept introduced in 1994. This is known as ILE and allows program modules to be bound together at compile time so that an RPG IV program can use routines written in C or any other language. So RPG III is essentially the same as RPG/400 and RPG IV is essentially the same as RPG ILE.
Before you go any further, read this short article:
Converting RPG-III to RPG-ILE
Now you are emotionally ready to convert your program TUTR001 to RPG IV.
First you must create a place for your program statements. Since RPG IV statements are longer than other program source statements, the command to create the source physical file is slightly different. To create the appropriately named place for RPG IV source:
CRTSRCPF FILE(USER999/QRPGLESRC) RCDLEN(112)
Of course, change the USER999 to your user ID. This command creates a source physical file named QRPGLESRC to hold your programs written in RPG IV. The name is the AS/400 standard for RPG IV or RPG ILE source code. It starts with "Q" followed by RPGLE (short for RPG ILE) and then SRC for source.
Now, use the AS400 command to convert the old program to RPG ILE. Key CVTRPGSRC (Convert RPG Source) and hit F4 to see the prompts. Your screen should look like:


                         Convert RPG Source (CVTRPGSRC)                 
                                                                        
 Type choices, press Enter.                                             
                                                                        
 From file  . . . . . . . . . . .                 Name                  
   Library  . . . . . . . . . . .     *LIBL       Name, *LIBL, *CURLIB  
 From member  . . . . . . . . . .                 Name, generic*, *ALL  
 To file  . . . . . . . . . . . .   QRPGLESRC     Name, *NONE, QRPGLESRC
   Library  . . . . . . . . . . .     *LIBL       Name, *LIBL, *CURLIB  
 To member  . . . . . . . . . . .   *FROMMBR      Name, *FROMMBR        
Now fill in the values to indicate that you want to convert the program located in source physical file QRPGSRC in library USER999 that is named TUTR001 and that you want the converted program to be in the same library with the name TUTR002.

                         Convert RPG Source (CVTRPGSRC
                                                      
 Type choices, press Enter.                           
                                                      
 From file  . . . . . . . . . . .   qrpgsrc       Name
   Library  . . . . . . . . . . .     user999     Name
 From member  . . . . . . . . . .   tutr001       Name
 To file  . . . . . . . . . . . .   QRPGLESRC     Name
   Library  . . . . . . . . . . .     user999     Name
 To member  . . . . . . . . . . .   tutr002       Name
Hit ENTER and the command will copy and modify your program.You can now use PDM to look at the converted program. From the PGM screen key in 3 and hit ENTER. Then fill in the screen to look like:


                       Specify Members to Work With
                                                   
 Type choices, press Enter.                        
                                                   
   File  . . . . . . . . . .   QRPGLESRC    Name, F
                                                   
     Library . . . . . . . .     USER999    *LIBL, 
                                                   
   Member:                                         
     Name  . . . . . . . . .   *ALL         *ALL
     Type  . . . . . . . . .   *ALL         *ALL
Hit enter and you will see your converted program in the list of programs:

                          Work with Members Using PDM       
                                                            
File  . . . . . .   QRPGLESRC                               
  Library . . . .     USER000              Position to  . . 
                                                            
Type options, press Enter.                                  
 2=Edit         3=Copy  4=Delete 5=Display       6=Print    
 8=Display description  9=Save  13=Change text  14=Compile  
                                                            
Opt  Member      Type        Text                           
     TUTR002     RPGLE       Change the first record in CUST
Notice that the "Type" is RPGLE. That tells the AS400 to compile this as RPG ILE instead of RPG III.Key a 2 next to the program name (TUTR002) and hit ENTER and you will see the source code:


0001.00 FCUST      UF   E             DISK                                     
0002.00 C                   READ      CUST                                   90
0003.00 C                   MOVE      *BLANK        CSADR1                     
0004.00 C                   MOVEL     '456 OAK'     CSADR1                     
0005.00 C                   UPDATE    CSREC                                    
0006.00 C                   MOVE      *ON           *INLR                      
0007.00 C                   RETURN                                             
        ****************** End of data ****************************************
 
You can see that not much has changed. RPG IV has more room for operand names so the UPDAT and RETRN are now spelled out. The other values are spread out a bit more.You can compile and run this program. You should try that now. Remember that you compile by using option 14. You can then CALL TUTR002 to run it.
Now copy this program and modify it to reflect the new coding style of RPG IV. Copy the program using PDM. Simply key 3 as the option:


                           Work with Members Using PDM                
                                                                      
 File  . . . . . .   QRPGLESRC                                        
   Library . . . .     USER000              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                                    
  3   TUTR002     RPGLE       Change the first record in CUST file    



When you hit ENTER, fill in TUTR003 as the new name:

                                 Copy Members                      
                                                                   
 From file . . . . . . . :   QRPGLESRC                             
   From library  . . . . :     USER000                             
                                                                   
 Type the file name and library name to receive the copied members.
                                                                   
   To file . . . . . . . .   QRPGLESRC    Name, F4 for list        
     To library  . . . . .     USER000                             
                                                                   
 To rename copied member, type New Name, press Enter.              
                                                                   
 Member         New Name                                           
 TUTR002        TUTR003                                            
Now hit ENTER again and PDM will copy TUTR002 and name it TUTR003.Edit TUTR003 by keying 2 as the option.
One way RPG IV is different is that lower case letters are allowed. This doesn't change the functionality of the program but it certainly makes it feel more modern.
You must tell SEU that you want to use lower case letters. When you are editing the TUTR003 program, hit F13 (well actually Shift-F1) and you will get the options for the editor:


                              Change Session Defaults                    
                                                                         
 Type choices, press Enter.                                              
                                                                         
   Amount to roll . . . . . . . . . . .   C            H=Half, F=Full    
                                                       C=Cursor, D=Data  
                                                       1-999             
   Uppercase input only . . . . . . . .   N            Y=Yes, N=No       
   Tabs on  . . . . . . . . . . . . . .   N            Y=Yes, N=No       
   Increment of insert record . . . . .   0.01         0.01-999.99       
   Full screen mode . . . . . . . . . .   N            Y=Yes, N=No       
                                                                         
   Source type  . . . . . . . . . . . .   RPGLE                          
   Syntax checking:                                                      
     When added/modified  . . . . . . .   Y            Y=Yes, N=No       
     From sequence number . . . . . . .                0000.00-9999.99   
     To sequence number . . . . . . . .                0000.00-9999.99   
                                                                         
                                                                         
   Set records to date  . . . . . . . .     /  /       YY/MM/DD 
It is best to change the first value to C. This affects how the screen scrolls when you PAGE-UP and PAGE-DOWN. Make sure the "Uppercase input only" selection is N. This allows you to key lowercase characters.For now, make 2 changes in the style. First, use lower case characters when you want. Second, use the EVAL operand in place of MOVE and MOVEL. EVAL is short for EVALUATE and like the old BASIC "LET". It is not only more like other modern languages, it has other enhancements that make string handling easy.
Also, since there is more room, change the address to 456 OAK STREET.
The modified program should look like:


0001.00 FCUST      UF   E             disk                                     
0002.00 C                   Read      CUST                                   90
0004.00 C                   Eval      CSADR1 = '456 OAK STREET'                
0005.00 C                   Update    CSREC                                    
0006.00 C                   Eval      *inlr = *on                              
0007.00 C                   Return                                             
Since the EVAL instruction works a little differently from the MOVE, it is no longer necessary to move blanks to CSADR1 before moving 456 OAK STREET to it.Now compile and run the program. After running TUTR003 you can look at the file with:
RUNQRY QRYFILE(CUST)
and see that the address has changed to 456 OAK STREET.
Boy, in just 10 minutes you have learned a new language!
You should be getting more comfortable with PDM, SEU, compiling programs and looking at spool files. If so, you are on your way to being a productive programmer.Remember that all examples are in the USER000 library for your viewing pleasure.


Note :

  1. RPG3 allows only uppercase.RPG 4 has an option to change the case(F13:Shift+F1)
  2. RPG3 uses Move and moveL(variable should be blank before assignng value).RPG4 uses EVAL(Variable no need to be blank)

Tuesday, September 14, 2010

Lesson 1 : Creating RPG 3 program for read and update of a file




TexAS400 Tutorial





RPG Lesson 1 - A 7 statement RPG-III program
It is important to understand the history behind the RPG language. The language was originally developed in the 1960's. It was a very high-level language meaning that a lot could be done with few programming statements. It was originally intended to be used as a report writer, hence the name Report Program Generator.


This original form of RPG was unique is several ways:

 1 - 5 different record types were used to do everything
  "F" was for file definitions
  "I" was for input definitions (record layouts)
  "C" was for calculations
  "O" was for output definitions
  "E" was for exception processing

 2 - There were many automatic processing features like the "cycle"
  The cycle processing automatically read the input file and 
  created the output files… that is, no read or write statements were 
  needed.

 3 - Fixed column locations for values of each programming statement. That is, 
  unlike COBOL, C, Java, or Basic, the code is not free form.

 4 - The use of Indicators or Switches was painfully required. A switch with 
  a value of '0' is off and a value of '1' is on. Switches were used to 
  determine the results of I-O operations and needed for simple compare 
  instructions. For example, instead of a statement that read like:

   IF CTR > 0, Do SOMETHING

  RPG read more like

   COMPARE CTR to 0 and turn Indicator 90 on if it is >
   If  Indicator 90 is on, Do SOMETHING
The programs were sometimes rather ugly. The use of indicators made many programs very hard to understand.RPG has evolved nicely to look more and more like other modern languages. While indicators are still used for Input-Output (read and write) operations, standard conditional operations are now available. IF/THEN, DoWhile, Do Until and Select Case operands exist. While still not completely freeform, the statements have portions that are free form. Also, powerful string handling functions like %trim, %subst, %len and %scan are available. If you are familiar the concepts of structured programming you will see that RPG has everything required for structured code.
So, while RPG is about 40 years old, its newest form (RPG IV) is modern and structured. The problem is that many programs are still in use that have very old programming styles. So, you really must be familiar with both RPG/400 (also known as RPG III) and RPG IV (also known as RPG ILE).
RPG is a full featured language that can do all functions of data processing: reading files, randomly retrieving data, navigating through indexes, reading and writing to data terminal or display files, creating print reports and database files.
If you are still concerned that RPG is too old to be useful, consider that Visual Basic and ASP are really just new versions of BASIC. BASIC is as old as RPG. Also, you should know that RPG ILE can create compiled objects that bind with modules written in other languages (like C) and can be used to process CGI scripts on the internet and output dynamic HTML. No kidding.
If you have an RPG book, I suggest you use it in addition to (or in place of) this tutorial. It is a challenging task to describe such a complex language in short, readable topics.
To proceed you need to be familiar with PDM (see topic 4) and should have completed the exercise in topic 5 that copies a file.
Since it important to know the old way of doing things (RPG/400) and the new way (RPG/ILE), I will present each program we write in both languages. Learn the RPG/400 style so you can get a job. Learn the RPG/ILE so you can see how modern the language has become.
Topic 5 showed you how to copy a file named CUST into your user library. Since this will be the input file to our program, make sure that your library list includes your user library. I prefer to have this library as my current library.
So, use DSPLIBL to display your library list. If you don't see your user library in the list, use the CHGCURLIB USER999 command. This will change your current library to USER999. Remember that when I say USER999, I mean your user ID.
You can look at your data file with the command:
RUNQRY QRYFILE(CUST)
If this doesn't show your file, either you didn't copy the file (see Topic 5) or your library list does not include your user library. You should see that the address for every record is "123 MAIN STREET". We will write a program to read the first record of the file and change the address to "456 OAK". In the next tutorial we will convert the program to RPG-ILE (RPG IV). In later topics we will change the program to read every record in the file and change the address to "456 OAK" in every record.
We need to enter program source statements, so 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  . . . . . . . . . .   QRPGSRC      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.If you get an error message that QRPGSRC was not found in USER999, you need to create the file QRPGSRC to hold your RPG-III (RPG/400) source code. For a refresher on this, review the topic on PDM. The command to create QRPGSRC in your library is:
CRTSRCPF USER999/QRPGSRC Your screen should look like:

                                                                              
File  . . . . . .   QRPGSRC                                                   
  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 module..
                                                                              
Opt  Member      Type        Text                                             
                                                                              
  (No members in file)                                                        
Remember from your PDM lesson that this is trying to show you a list of all of the RPG programs that you have entered. Since you haven't entered any, there is nothing to show.You need to add a program. Let's call the program TUTR001. This name could be any 10 characters but if you don't follow a naming convention you will confused quickly. This name is derived from TUT (for tutorial), R for RPG and 001.
Hit F6 to add the program. Key in the name (the source member), RPG for the source type and a short description. Your screen should look like:


---------------------------------------------------------------------------
                       Start Source Entry Utility (STRSEU)              
                                                                        
 Type choices, press Enter.                                             
                                                                        
 Source file  . . . . . . . . . . > QRPGSRC       Name, *PRV            
   Library  . . . . . . . . . . . >   USER000     Name, *LIBL, *CURLIB, 
 Source member  . . . . . . . . .   tutr001       Name, *PRV, *SELECT   
 Source type  . . . . . . . . . .   rpg           Name, *SAME, BAS, BASP
 Text 'description' . . . . . . .   Change the first record in CUST 
                                                                    
-----------------------------------------------------------------------    
Now hit enter and you will see a blank screen where you can enter RPG source code.Back in the seventies, programmers had to keep track of what column each operand of the RPG statement was in. It was a mess.
With PDM (actually SEU, the Source Entry Utility), things are easier. Not easy, but easier. The problem is that RPG is a fixed column language. It is not a free format language. SEU provides prompting to help you but even it is a bit complicated at first.
The problem is that the different records types (F, I, E, C, O) do multiple things. That is, the I record type can be used several different ways so it has several different ways of prompting.
The first statement of this program is a FILE specification. That is, it will name the file that we will read and update. Since we want to read the CUST file, we will enter an F record to define the file CUST.
To do this, hit F23. Since your PC keyboard doesn't have an F23 key, the Telnet5250 product you are using has mapped the SHIFT-F11 key as an F23 key. So, hold down SHIFT and hit F11. The top of your screen should look like:


                                Select Prompt                          
                                                                       
 Type choice, press Enter.                                             
                                                                       
  Prompt type . . . . . . . . . . . .         Values listed below      
                                                                       
    RPG/400:        H,F,FC,FK,FX,U,E,L,I,IX,J (I cont),JX,DS,SS,SV,C,O,
                    OD,P (O cont),N,* (Comment)                        
Wow. This means that there are 21 different formats to choose from. This is an easy one. Since this is a FILE specification, we will use the F prompt. So key F for the Prompt and hit ENTER.You should now see:


0001.00                                                                
        ****************** End of data ********************************
 Prompt type . . .    F      Sequence number . . .  0001.00            
                                                                       
               File        File          End of                   File 
 Filename      Type     Designation       File      Sequence     Format
                                                                       
 Record      Mode of        Length of         Record                   
 Length     Processing      Key Field      Address Type                
                                                                       
     File            Overflow       Key Field      Extn                
 Organization        Indicator      Start Loc      Code     Device     
                                                                       
                                         File            File          
 Continuation     Exit     Entry      Addition         Condition       
What a mess! A couple of things have happened. The first is that the display shows your first line of the program as line 0001.00. Not only that, the SEU editor has highlighted it because it is blank, RPG-III does not allow blank lines! The rest of the screen shows all of the fields that might be used to define a file. Don't panic yet. All you need to enter is:
 
 CUST for Filename
 I for File Type
 F for File Designation
 E for File Format
 DISK for Device (2nd line from the bottom on the right)
Hit ENTER and your values will go in the right places. Amazingly, the one value that you can't enter here is the F that tells RPG that this is a FILE specification. So hit enter, your screen looks like:
 
FMT F  .....FFilenameIPEAF....RlenLK1AIOvKlocEDevice+
        *************** Beginning of data ************
0001.00      CUST    IF  E                    DISK   
        ****************** End of data ***************
It also displays an error that "The Type entry is not H, F, I, C ……"So put your cursor on the character right before CUST and key an F.
Now you have a valid file specification:


FMT F  .....FFilenameIPEAF....RlenLK1AIOvKlocEDevice+
        *************** Beginning of data ************
0001.00     FCUST    IF  E                    DISK   
        ****************** End of data ***************
Now, calm down. This may be the only time in your life that you actually key in a FILE specification. In the world of programming, everything is copied and changed, cut and paste. Normally, you will simple copy an existing program and then change and add statements to it.So, what did you just define? You told the program that you will be using the file CUST, that the file is being used for Input (you will read the file), that the file is Fixed length (almost all files are fixed length), that the file is Externally defined (that is, it was defined by the database language of the AS/400) and finally that the file is a DISK file (not a PRINTER or Display Screen).
Imagine for a moment a program where the user enters a customer number on the display screen. The program reads the customer record, looks up the customer's most recent order and prints it. Such a program will have at least 4 files: Display screen file (a WORKSTN file) Customer file (a DISK file) Order file (a DISK file) Printer file (a PRINTER file)
This is a simple program. It has only one file.
Now we are ready for the processing logic of the program. The total logic of this program will be only 6 statements. Processing logic in RPG is known as CALCULATION statements and uses the C specification and the C prompt type.
Soon, you will get more comfortable with PDM and SEU. Now, we need to add a line to after the F statement. Do this by keying an I (for insert) on top of the 0001.00.
Your screen should look like:


FMT FX .....FFilenameIPEAF........L..I........Device+
        *************** Beginning of data ************
I001.00      FCUST    IF  E                    DISK   
When you hit ENTER, the editor will create a blank line for you and even place your cursor in column 6 where you can enter the C for CALCULATION. With the cursor on the line with the C, hit Shift-F11 to prompt and this time use C for the prompt type.Your screen will now show:

 
Prompt type . . .    C      Sequence number . . .  0002.00         
                                                                    
 Level    N01N02N03  Factor 1     Operation      Factor 2     Result
                                                                    
             Decimal                                                
 Length     Positions    H/N/P   HI   LO   EQ    Comment            
Now finally some good news. Most of the RPG programming you do is using the C specification. This type of statement is not too messy.
The first processing step of the program is to read the first record of the CUST 
file. RPG needs to know 2 things about a read statement:
     1) what file to read
 2) what indicator to turn on if there was not a "next" record in the file
  (that is, what indicator will tell you that there is no more data in 
                the file)

Indicators are numbered from 01 to 99. There are some other special ones too.

I like to use indicators 90 thru 99 for the read statements. So enter 
 READ for the Operation
 CUST for FACTOR 1
 90 for EQ
Your screen should look like:

0001.00      FCUST    IF  E                    DISK                   
0002.00      C                                                        
'''''''                                                               
        ****************** End of data *******************************
                                                                      
                                                                      
                                                       
                                                                      
 Prompt type . . .    C      Sequence number . . .  0002.00           
                                                                      
 Level    N01N02N03  Factor 1     Operation      Factor 2     Result  
                                    READ         CUST                 
             Decimal                                                  
 Length     Positions    H/N/P   HI   LO   EQ    Comment              
                                           90                           

                                                                          
   When you hit ENTER, it will look like:

FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++En
        *************** Beginning of data ***************************
0001.00      FCUST    IF  E                    DISK                  
0002.00      C                     READ CUST                     90  
        ****************** End of data ******************************                                                                       
There are 3 places to enter indicators for statements. Unfortunately, it is not always easy to figure out which place to use. The HI, LO and EQ headings come from the obsolete way indicators were first used. In old RPG, there were no IF statements. Instead, you used a COMP (Compare) statement to compare the values of FACTOR1 and FACTOR2 and then would set on an indicator if FACTOR1 was higher than FACTOR2, a different indicator if FACTOR1 was lower that FACTOR2 and finally an indicator if they were equal. We will NEVER do this.The lesson here is simple. For a READ instruction, the indicator mentioned in the EQ field will be turned on when there are no more records in the file.
Let's pause for a minute and look at all that is happening with these 2 statements.
The F specification tells the AS400 to set up a way to read the CUST file. It also tells it to find the fields and attributes of the fields using the database definition of the CUST file. In most other languages, you would need a statement to OPEN the file. Such a statement tells the computer that you are ready to use the file. RPG assumes that you want to read the file and automatically opens the file for you. There is a simple way to tell the AS400 that you want to OPEN and CLOSE the file explicitly but there is no reason to do that.
The READ statement reads the first record of the file CUST and populates an area in your program's memory with the data that is in the first field. You program has each field defined and now has data in each field.
After the READ, the field CSNBR has a value of 1002, the field CSNAME has a value of E LUMPKIN, the field CSADR1 has a value of 123 MAIN STREET.
We now want to change the value in CSADR1. Unlike other languages, if we move only a few characters to a field that is 30 characters long, RPG will change only the first 3 characters. So we will first blank out the field and then move 456 OAK to the address.

Insert a line for a new C statement. One way to do this is to key an I over the 
0002.00 and hit ENTER. Then key C in column 6 and hit Shift-F11. Key C for prompt 
type and hit ENTER. We want to blank out the CSADR1 field so we will move blanks 
to it. RPG has some system values defined that are named like:
 *BLANK or *BLANKS
 *ZERO or *ZEROES
 *ON or *OFF
 *IN90 (for indicator 90)
As you might guess, we want to move the value *BLANK to CSADR1. So fill in your screen to look like:
                                                                  
 Prompt type . . .    C      Sequence number . . .  0003.00          
                                                                     
 Level    N01N02N03  Factor 1     Operation      Factor 2     Result 
                                    MOVE         *BLANK       CSADR1 
             Decimal                                                 
 Length     Positions    H/N/P   HI   LO   EQ    Comment             
                                                                     
Hit ENTER and your program should look like:

        *************** Beginning of data *****************************
0001.00      FCUST    IF  E                    DISK                    
0002.00      C                     READ CUST                     90    
0003.00      C                     MOVE *BLANK    CSADR1               
        ****************** End of data ********************************

Now insert another line to MOVEL '456 OAK' to CSADR1. This says to move the characters
in between the quotes to the field named CSADR1. The operand is MOVEL for MOVE LEFT. 
This means that since '456 OAK' is only 7 characters and CSADR1 is 30 characters, 
move '456 OAK' to the left 

You program should look like:

        *************** Beginning of data **************************
0001.00      FCUST    IF  E                    DISK                 
0002.00      C                     READ CUST                     90 
0003.00      C                     MOVE *BLANK    CSADR1            
0004.00      C                     MOVE '456 OAK' CSADR1            
        ****************** End of data *****************************

At this moment, the data on the disk has the value '123 MAIN STREET' in the area of 
the record for CSADR1. If you ran this program, at this spot in the program, the 
value of the field CSADR1 in memory would be '123 OAK'. But if you stopped here, 
the new value ('123 OAK') would not get stored in the file. To do that you must 
UPDATE the record. In RPG-III verbs or operands can be only 5 characters so the 
verb is UPDAT. Also, you must use the record name for the file. This is too 
complicated to explain here but trust me that the record name for the CUST file 
is CSREC.

So add the statement to update the record CSREC. Now that we see that we are using 
the file for Update (not just Input), it is time to change the I in the FILE 
definition from I to U.

0001.00      FCUST    UF  E                    DISK                  
0002.00      C                     READ CUST                     90  
0003.00      C                     MOVE *BLANK    CSADR1             
0004.00      C                     MOVEL'456 OAK' CSADR1             
0005.00      C                     UPDATCSREC                        

Now all that is left to do is to tell the program to stop. 

First, we have to discuss a housekeeping item left over from the very early days 
of RPG. We know that RPG will automatically open the file. However, it will only 
close the file if the "LAST RECORD" indicator is ON. For now, just make sure the 
last 2 statements to execute in a program are:
  MOVE *ON *INLR
  RETRN

The last statement is a RETRN which is an abbreviation of RETURN.

So the final program is:

0001.00      FCUST    UF  E                    DISK                    
0002.00      C                     READ CUST                     90    
0003.00      C                     MOVE *BLANK    CSADR1               
0004.00      C                     MOVEL'456 OAK' CSADR1               
0005.00      C                     UPDATCSREC                          
0006.00      C                     MOVE *ON       *INLR                
0007.00      C                     RETRN                               
Now that you are finished, hit F3 and ENTER to save your statements.So you now have the source statements to compile an RPG program. You compile it just like you compiled the CL program. Key a 14 on the line next to the program name in PDM and hit ENTER. Like:


                           Work with Members Using PDM            
                                                                  
 File  . . . . . .   QRPGSRC                                      
   Library . . . .     USER999           Position to  . . . . .
                                                                  
 Type options, press Enter.                                       
  2=Edit         3=Copy  4=Delete 5=Display       6=Print     7=Re
  8=Display description  9=Save  13=Change text  14=Compile  15=Cr
                                                                  
 Opt  Member      Type        Text                                
 14   TUTR001     RPG         Change the first record in CUST file
By the way, this program is in the USER000 library. You can see it using PDM.After compiling your program, you will want to look at the compiled listing. Remember, you do this with the command:
WRKSPLF
You will see something like:


                          Work with All Spooled Files             
                                                                  
 Type options, press Enter.                                       
   1=Send   2=Change   3=Hold   4=Delete   5=Display   6=Release  
   8=Attributes        9=Work with printing status                
                                                                  
                              Device or                     Total 
 Opt  File        User        Queue       User Data   Sts   Pages 
      TUTR001     DAVIDMOUNT  PRTDGM                  RDY       5 
      TUTR001     DAVIDMOUNT  PRTDGM                  RDY       5
Put your cursor next to the last one and key in a 5 and hit ENTER.You will see your compiler listing. If you key in a B at the top and hit ENTER, the AS400 will take you to the bottom of your listing. If your compile worked, it will look like:


                           F i n a l   S u m m a r y               
 Message Count:  (by Severity Number)                              
            TOTAL    00     10     20     30     40     50         
              2       2      0      0      0      0      0         
 Program Source Totals:                                            
    Records . . . . . . . . . . :   7                              
    Specifications  . . . . . . :   7                              
    Table Records . . . . . . . :   0                              
    Comments  . . . . . . . . . :   0                              
 PRM has been called.                                              
 Program TUTR001 is placed in library USER000. 00 highest severity.
          * * * * *   E N D   O F   C O M P I L A T I O N   * * * *
The phrase "Program TUTR001 is placed in library USER???" means the AS400 understood your statements well enough to crate an executable program.If your compile didn't work, start by making sure that you have your user library in your library list. The easy way to do this is:
CHGCURLIB USER999
All that is left is to run the program. There are several ways to do this but the most straightforward way is to use this command:
CALL TUTR001
The program will run almost instantly. Then if you look at the CUST file the address of the first record will have changed:
RUNQRY QRYFILE(CUST)
Boy what a long topic! Each lesson will build on this program. It is important that you get familiar with entering programs, compiling them and looking at the compile listings.Give me some feedback on this lesson. It is hard to cover all the details and still make it fun.


Note :

  1. RPG 3 commands :  UPDAT,RETRN
  2. For a READ instruction, the indicator mentioned in the EQ field will be turned on when there are no more records in the file.
  3. In file specification : 
CUST for Filename
I for File Type F for File Designation E for File Format DISK for Device (2nd line from the bottom on the right)

You told the program that you will be using the file CUST, that the file is being used for Input (you will read the file), that the file is Fixed length (almost all files are fixed length), that the file is Externally defined (that is, it was defined by the database language of the AS/400) and finally that the file is a DISK file (not a PRINTER or Display Screen).

  1. Shift + F11 for specifications