Sunteți pe pagina 1din 4

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 simpl est way to do this is to copy TUTR003 in the QRPGLESRC source object and then mo dify 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. 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 . . . . . . . . . QRPGLESRC 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: 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 TUTR002 TUTR003 Type RPGLE RPGLE Text Change the first record in CUST file 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 . . . . . . . : From library . . . . : QRPGLESRC USER999

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

To rename copied member, type New Name, press Enter. Member TUTR003 New Name TUTR005

Hit ENTER and you should now see the copied program in your list. You may want t o change the description of the program so you'll remember which program does wh at. 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 AND C DoW *in90 = *off *in90 DOWEQ *off

Not only is the second form more readable, it is more free form. You can put spa ces 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 C C C

*in90 STATUS STATUS CITY

IFEQ ANDEQ OREQ ANDEQ

*off 'D' 'A' 'ATHENS'

with the newer style C C If (In90 = *off) and ((STATUS = 'D') or (STATUS = 'A')) and

(CITY = 'ATHENS')

The newer style is not only more readable but you can actually figure out how th e conditions are checked. One of the other big changes in form from RPG III to RPG IV is that the subrouti ne names can be only 6 characters in RPG III. In RPG IV, the subroutine names ca n be 14 characters. These longer names make the programs many times easier to fo llow. 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 C C C C C DoW Read If Exsr EndIf EndDo not %eof(CUST) CUST not %eof(CUST) UpdateCSREC

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

If you have ever used Visual Basic, the EOF function will look familiar.

That's it!

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