Leave, Iterate, And LeaveSR

Embed Size (px)

Citation preview

  • 7/27/2019 Leave, Iterate, And LeaveSR

    1/4

    System iNetwork Head NavSubscribe Log In Contact Us AdvertiseUser loginUsername: *Password: *

    Request new passwordSearch

    Primary linksForums Archives Code Blogs Podcasts Webcasts e-Learning Guides Newsletters AboutUs Contact Us About the Network Tech Editor Profiles Editorial Calendar WritersKit Advertise Join Network

    CategoriesRPG ProgrammingOther LanguagesApplication DevelopmentDatabase/SQLAvailabilitySecurity

    Systems ManagementNetworkingIT Mgmt/CareersSite LinksSolutionsStoreEventsUK CentreJobs

    System iPortal Home ContentLeave, Iterate, and LeaveSRArticle ID: 57524Posted December 4th, 2008 in RPG ProgrammingBy:Bob CozziHoliday OpCodesI wanted a holiday theme for this issue; however, I couldn't think of any. So, Isimply called it, "Holiday OpCodes." I hope you like it.

    There are so many Structured Programming opcodes in RPG IV that sometimes we forget all the pieces. We have things like FOR, DO, DOW, DOU, IF, SELECT/WHEN and CASxx; but we also have LEAVE, ITER and LEAVESR. Most programmers know how to usethe FOR, DO, and so on; but few seem to understand how LEAVE, ITER, and LEAVESR

    work.

    Glorified GOTO--But in a Good WayWhen we started to move away from the GOTO operation in programming some 25 years ago, new structured operation codes were introduced. The DO, DOWHILE, and DOUNTIL opcodes helped countless programmers write code that was easier to read anddebug. The internal MI code that was generated (often called "IRP") used the GOTO operation under the covers to enable this capability in RPG III. But since wedidn't need to modify the MI code, who cares what it generated?

  • 7/27/2019 Leave, Iterate, And LeaveSR

    2/4

    The instruction in MI was "B" or Branch--not GOTO--and it caused the control tojump 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 exit point and were a key theme in structure programming.

    Then came the issue of nesting. To avoid running sections of a DO loop, nested IF statements were used, or in rare cases even a GOTO was inserted. In the case of the nested IF statements, it was to skip sections of the DO loop and loop backup to the top of the loop. In the case of the embedded GOTO, it was to exit theDO loop abnormally instead of at the natural, single exit point. This was a painful time for programmers; we had structured opcodes but had to resort to gruntmethods to make something work.

    Enter LEAVEIn the late 1980s, three new opcodes entered the scene. These opcodes helped reduce the use of nested IFs in DO loops and effectively (permanently) eliminated the use of GOTO in most RPG code. Those new opcodes are:

    ITER--Iterate a loop back up to the topLEAVE--Exit a loopLEAVESR--Exit a SubroutineITER--InterateThe ITER opcode (does anyone else wish they would spell it out in RPG IV?) causes 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 the ITER opcode to be skipped. Therefore, conditioning the ITER opcode is virtually mandatory.

    A few things happen when the ITER is performed, depending on which opcode is looping. Branching to the top of the loop is the same for any of them, but individually here are the differences:

    DO--The ITER opcode causes the increment counter (result field of the DO opcodeitself) to be incremented. Thus, if you have a simple "DO" opcode with a limit in Factor 2, then ITER branches to the top of the Do loop and increments the counter. The Do loop checks the counter to see if it has exceeded the value in Factor 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. Butthe counter will be incremented first.

    DOU (Do Until)--The ITER opcode causes a branch to the top of the Do loop, the conditional statement is tested and, if met, the program branches to the statement following the ENDDO opcode. Although logically the test for the DOU is performed at the bottom of the DOU loop, it is actually performed at the top of the loop. This is because the difference between DOU and DOW in RPG is that DOU is always performed once before its condition is tested. The other difference is that DOU means continue while the condition is NOT met, whereas DOW means continue while 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 statement following the ENDDO opcode.

    FOR--The ITER opcode causes a branch to the top of the FOR loop, the counter isincremented, and the limit condition is tested. If the limit condition has not been met, then the next loop through the FOR loop begins. Otherwise, control branches to the ENDFOR opcode.

    LEAVE--Exit a LoopThe LEAVE opcode causes a jump to the corresponding ENDDO or ENDFOR statements.

  • 7/27/2019 Leave, Iterate, And LeaveSR

    3/4

  • 7/27/2019 Leave, Iterate, And LeaveSR

    4/4

    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 programming languages have a way to indicate on the LEAVE opcode that you want to exit 2 or 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 Tuesday of each month for System iNetwork. He is author of "RPG TnT: 101 Dynamic Tips 'n Techniques with RPG IV" and produces RPG World, the most popular RPG IV developer conference of the year. The next RPG World conference is scheduled for May 2009 in Las Vegas. Visit RPGWorld.com for details. Be sure to use Promotion Code RPGCODER to save $100 off registration (promo code valid until February 1, 2009).

    Join Bob Cozzi and friends at noon Eastern every Friday for iWeekly, a live streaming video podcast sponsored by RPG World.

    Bookmark/Search this post with:Login to post comments Email this page Printer-friendly version Related

    LinksConvert Text to NumbersParsing XML in RPG IV

    Read Data from a Web Browser into RPG IVHow to Read a CSV File in RPGAPIs by Example: System Date and Time APIs and Utilities

    ProVIP Sponsors

    ProVIP Sponsors

    Featured LinksSponsored LinksFooter Site LinksHome Subscribe Now Advertise Contact Us Feedback Terms & Conditions Trademarks Privacy Policy Copyright Penton Media