Sunteți pe pagina 1din 5

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 cha nge 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 tha t 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 someth ing like: --------------------------------------------------------------------AS/400 Programming Development Manager (PDM) Select one of the following: 1. 2. 3. 4. 5. 6. Work Work Work Work Work Work with with with with with with libraries objects members projects groups 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 . . . . . . . . . . Library . . . . . . . . Member: Name . . . . . . . . . Type . . . . . . . . . QRPGSRC user999 *ALL *ALL Name, F4 for list *LIBL, *CURLIB, name *ALL, name, *generic* *ALL, type, *generic*,

----------------------------------------------------------------------Of course, change the USER999 to your user ID. Hit ENTER now.

Your screen should look like:

File . . . . . . Library . . . .

QRPGSRC 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 TUTR001 RPG Text 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 . . . . . . . : From library . . . . : QRPGSRC USER999

Type the file name and library name to receive the copied members To file . . . . . . . . To library . . . . . QRPGSRC USER999 Name, F4 for list

To rename copied member, type New Name, press Enter. Member TUTR001 New Name 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 wha t. Your screen should look like: Remember from your PDM lesson that this is trying to show you a list of all of t he RPG Work with Members Using PDM File . . . . . . Library . . . . QRPGSRC 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 TUTR001 TUTR004 Type RPG RPG Text Change the first record in CUST file 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 w rite a loop. We want a loop that will read every record in the CUST file and cha nge 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 t urn 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 C C C C C C C C C C UF E READIT *IN90 DISK TAG READ CUST IFEQ *OFF MOVE *BLANK CSADR1 MOVEL'456 OAK' CSADR1 UPDATCSREC GOTO READIT ENDIF MOVE *ON *INLR RETRN

90

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 Up date Go back to the first line of the program Turn on the "magic" LR indicator E nd the program This will work. The only problem is that for about 15 years, it has been conside red bad style to use GOTO statements. This is an industry trend that includes al l 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 b ecause 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 D O UNTIL. This still leaves us with 2 good ways to write the loop. I prefer the f irst because it looks a lot like the GOTO loop but without the GOTO: FCUST C C C C C C C C C C UF E *IN90 *IN90 DISK DOWEQ*OFF READ CUST IFEQ *OFF MOVE *BLANK CSADR1 MOVEL'456 OAK' CSADR1 UPDATCSREC ENDIF ENDDO MOVE *ON *INLR RETRN

90

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 t he 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+ reco rds 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 no t 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 C C C C C C C C C UF E *IN90 DISK READ CUST DOWEQ*OFF MOVE *BLANK CSADR1 MOVEL'456 OAK' CSADR1 UPDATCSREC READ CUST ENDDO MOVE *ON *INLR RETRN

90

90

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 stat e 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 C C C CSSTE IFEQ 'TN' MOVE 987650000 CSZIP UPDATCSREC ENDIF

So, the whole program looks like: FCUST C C C C C C C C C C C UF E *IN90 *IN90 CSSTE DISK DOWEQ*OFF READ CUST IFEQ *OFF IFEQ 'TN' MOVE 987650000 CSZIP UPDATCSREC ENDIF ENDIF ENDDO MOVE *ON *INLR RETRN

90

This is considered good code by most programmers. But it really helps to move th e 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 h as the name of the subroutine. They end with ENDSR (END SUBROUTINE). They are ru n 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 t ype, 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 p retty 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 F2 0 (actually Shift and F8) you will see that you changed the zip in all of the re cords 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 versi on. You will see how much prettier ILE code is than RPG III.

S-ar putea să vă placă și