Sunteți pe pagina 1din 4

System iNetwork Head Nav Subscribe Log In Contact Us Advertise User login Username: * Password: * Request new password

Search Primary links Forums Archives Code Blogs Podcasts Webcasts e-Learning Guides Newsletters About Us Contact Us About the Network Tech Editor Profiles Editorial Calendar Writers Kit Advertise Join Network Categories RPG Programming Other Languages Application Development Database/SQL Availability Security Systems Management Networking IT Mgmt/Careers Site Links Solutions Store Events UK Centre Jobs System iPortal Home Content Leave, Iterate, and LeaveSR Article ID: 57524Posted December 4th, 2008 in RPG Programming By:Bob Cozzi Holiday OpCodes I wanted a holiday theme for this issue; however, I couldn't think of any. So, I simply called it, "Holiday OpCodes." I hope you like it. There are so many Structured Programming opcodes in RPG IV that sometimes we for get all the pieces. We have things like FOR, DO, DOW, DOU, IF, SELECT/WHEN and C ASxx; but we also have LEAVE, ITER and LEAVESR. Most programmers know how to use the FOR, DO, and so on; but few seem to understand how LEAVE, ITER, and LEAVESR work. Glorified GOTO--But in a Good Way When we started to move away from the GOTO operation in programming some 25 year s ago, new structured operation codes were introduced. The DO, DOWHILE, and DOUN TIL opcodes helped countless programmers write code that was easier to read and debug. The internal MI code that was generated (often called "IRP") used the GOT O operation under the covers to enable this capability in RPG III. But since we didn't need to modify the MI code, who cares what it generated?

The instruction in MI was "B" or Branch--not GOTO--and it caused the control to jump to another location (identified by a tag or label) in the program. The DO, DOWHILE, and DOUNTIL opcodes were looping opcodes that had one entry and one exi t point and were a key theme in structure programming. Then came the issue of nesting. To avoid running sections of a DO loop, nested I F statements were used, or in rare cases even a GOTO was inserted. In the case o f the nested IF statements, it was to skip sections of the DO loop and loop back up to the top of the loop. In the case of the embedded GOTO, it was to exit the DO loop abnormally instead of at the natural, single exit point. This was a pai nful time for programmers; we had structured opcodes but had to resort to grunt methods to make something work. Enter LEAVE In the late 1980s, three new opcodes entered the scene. These opcodes helped red uce the use of nested IFs in DO loops and effectively (permanently) eliminated t he use of GOTO in most RPG code. Those new opcodes are: ITER--Iterate a loop back up to the top LEAVE--Exit a loop LEAVESR--Exit a Subroutine ITER--Interate The ITER opcode (does anyone else wish they would spell it out in RPG IV?) cause s a jump to the logical top of the Do loop, whether it is a DO, DOW, or DOU. It also works with the FOR opcode. This causes the code in the DO loop following th e ITER opcode to be skipped. Therefore, conditioning the ITER opcode is virtuall y mandatory. A few things happen when the ITER is performed, depending on which opcode is loo ping. Branching to the top of the loop is the same for any of them, but individu ally here are the differences: DO--The ITER opcode causes the increment counter (result field of the DO opcode itself) to be incremented. Thus, if you have a simple "DO" opcode with a limit i n Factor 2, then ITER branches to the top of the Do loop and increments the coun ter. The Do loop checks the counter to see if it has exceeded the value in Facto r 2; if it has, it exits the DO loop. Obviously, if you specify "DO 1" (do one t ime) or don't put a value in Factor 2, an ITER results in the loop exiting. But the counter will be incremented first. DOU (Do Until)--The ITER opcode causes a branch to the top of the Do loop, the c onditional statement is tested and, if met, the program branches to the statemen t following the ENDDO opcode. Although logically the test for the DOU is perform ed at the bottom of the DOU loop, it is actually performed at the top of the loo p. This is because the difference between DOU and DOW in RPG is that DOU is alwa ys performed once before its condition is tested. The other difference is that D OU means continue while the condition is NOT met, whereas DOW means continue whi le the condition is met. DOW (Do While)--The ITER opcode causes a branch to the top of the Do loop, the c onditional statement is tested and, if not met, the program branches to the stat ement following the ENDDO opcode. FOR--The ITER opcode causes a branch to the top of the FOR loop, the counter is incremented, and the limit condition is tested. If the limit condition has not b een met, then the next loop through the FOR loop begins. Otherwise, control bran ches to the ENDFOR opcode. LEAVE--Exit a Loop The LEAVE opcode causes a jump to the corresponding ENDDO or ENDFOR statements.

Conditioning the LEAVE opcode is virtually mandatory because it makes little sen se to have an unconditioned LEAVE opcode. However, for testing/debug purposes it can make sense. Unlike the ITER opcode, the LEAVE opcode branches to the ENDDO or ENDFOR stateme nt directly. Therefore, no further testing or incrementing of counter variables is performed. LEAVESR--Exit Subroutine For eons RPG programmers specified a value in Factor 1 of the ENDSR opcode. This was one way to insert a "TAG" or "LABEL" as the target of a GOTO from within th e corresponding subroutine. When you needed to exit the subroutine, issue a GOTO ENDMYSR or whatever the label in Factor 1 of the ENDSR opcode was and you basic ally did a return from that subroutine. There was talk about adding the ability for the GOTO opcode to branch to the vir tual *ENDSR tag that would be implied in each subroutine. Then when the program was compiled, the *ENDSR would be converted into a virtual TAG inserted by the c ompiler. Fortunately, someone got the bright idea of adding the LEAVESR opcode t o the language. LEAVESR is clearer and much more readable than other techniques in use, and it lets you exit the subroutine immediately. As with the LEAVE and I TER opcodes, it makes little sense to code an unconditioned LEAVESR statement, b ut you can. /free begSR WhyUseMe; dou %EOF(custmast); for i = 1 to 30; read custmast if %EOF(custmast); leave; endif; custarr(i) = custno; endfor; exsr submitCustNo; if %eof(custmast); leaveSR; endif; enddo; endSR; /end-free On line 1, the WHYUSEME subroutine begins. I call it WHYUSEME because I find lit tle use for Subroutines since the introduction of subprocedures more than 10 yea rs ago. But, for example purposes, it will work just fine. The DOU (do until) loop begins on line 2 and is conditioned based on %EOF for th e CUSTMAST file. The DOU means the initial conditional test won't be performed u ntil the DOU loop cycles at least one time. Then a FOR loop is performed. This FOR loop normally runs 30 times unless %EOF f or the CUSTMAST is reached (line 5). If %EOF is reached, the LEAVE opcode on lin e 6 is performed. This branches to the statement following the ENDFOR loop on li ne 9. Then another subroutine is called, and once again %EOF is tested. If there are no more records in the file, the LEAVESR opcode is performed (line 12) and the program branches to the ENDSR statement on line 15 and returns to the caller of the subroutine. If there are more records, then the DOU loop loops around to the top and continues the process again. Note that the FOR loop is inside the DOU loop. When a LEAVE or ITER is encounter 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015

ed, it applies to the innermost loop that is being performed. So, in our example , the LEAVE opcode applies to the FOR loop, not to the DOU loop. Some programmin g languages have a way to indicate on the LEAVE opcode that you want to exit 2 o r more levels at once, but RPG supports only one level of LEAVE. -------------------------------------------------------------------------------Bob Cozzi produces the popular Tuesday Tips video podcast on the 2nd and 4th Tue sday of each month for System iNetwork. He is author of "RPG TnT: 101 Dynamic Ti ps 'n Techniques with RPG IV" and produces RPG World, the most popular RPG IV de veloper conference of the year. The next RPG World conference is scheduled for M ay 2009 in Las Vegas. Visit RPGWorld.com for details. Be sure to use Promotion C ode RPGCODER to save $100 off registration (promo code valid until February 1, 2 009). Join Bob Cozzi and friends at noon Eastern every Friday for iWeekly, a live stre aming video podcast sponsored by RPG World. Bookmark/Search this post with: Login to post comments Email this page Printer-friendly version Related Links Convert Text to Numbers Parsing XML in RPG IV Read Data from a Web Browser into RPG IV How to Read a CSV File in RPG APIs by Example: System Date and Time APIs and Utilities ProVIP Sponsors ProVIP Sponsors Featured LinksSponsored Links Footer Site Links Home Subscribe Now Advertise Contact Us Feedback Terms & Conditions Trademarks P rivacy Policy Copyright Penton Media

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