You are on page 1of 3

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 Time for a DO Over Article ID: 57327Posted October 15th, 2008 in RPG Programming By:Bob Cozzi Time FOR a DO Over I started writing in RPG IV /FREE syntax about two years ago. Why so long after /FREE was available? Because too few users were on v5.1 yet or, if they were, th ey were not using /FREE syntax, so using /FREE in my articles would have been po intless. When I started using it, I had the usual grief cycle associated with fr ee format. But as I grew used to its oddities, I began narrowing down my dislike s and eventually realized that I like it as much as the so called hybrid version of RPG IV. The hybrid version is when you use the EVAL, IF, DOW, WHEN, etc. in the traditional syntax along with the Extended Factor 2--where you can specify e xpressions. Once you except it on that basis, it's pretty easy to use. As time went on, I discovered one annoying area. Only one? No, there were many, such as the fact that MOVE/MOVEL aren't supported, that numeric assignments caus e an overflow error instead of being truncated, and initially that you had to dr op into fixed format and then enter embedded SQL to include SQL in your code. With the exception of embedded SQL, none of those issues has been fixed, althoug h I published a workaround for the truncating number issue a few weeks ago. Now

in free format, I can do embedded SQL and just about anything else I want, excep t one thing: DO. DO It! Come On, Just Do It! Sorry, couldn't help using the Arnold line from Predator. The DO opcode is sadly not included in the /FREE syntax of RPG IV. It simply does not port over at all . The irony is you can actually convert the ENDDO opcode to /FREE syntax and do something goofy, like the following, and it works/compiles. /END-FREE C /FREE arr(x) = ' '; enddo; /END-FREE The idea is, you drop out of free format, specify the old fixed-format DO, and t hen drop back into free format, specifying the ENDDO opcode in free format. Yes, it compiles and yes, it works. Oh my! But there is more serious solution to this issue. IBM replaced the DO opcode wit h the more flexible and powerful FOR opcode. This opcode does everything the DO opcode does and a lot more. The FOR opcode is available in free format as well as the hybrid/extended Factor 2 syntax. The FOR opcode supports incrementing by 1 or more than 1. The FOR opcode supports decrementing by 1 or more than 1. Yes, you can start at a high value and reduce the counter by 1 or some other value each pass through t he FOR loop. The FOR opcode supports variables and built-in functions for its "parameters". The basic idea is to replace a traditional DO opcode with the FOR opcode and the n move it to the free-format syntax. For example, suppose you have the following DO loop in your fixed-format code, a nd you want to convert it to free-format syntax: C 1 DO 100 X C eval arr(x) = ' ' C ENDDO 1 The biggest challenge is changing the DO opcode into a FOR opcode, and that woul d be as easy, as the following shows: C FOR X = 1 to 100 by 1 C eval arr(x) = ' ' C endfor The counter variable (X in my example) is initialized to 1 by the equal sign. Th en the TO parameter identifies the maximum value that X may contain before it ba ils out of the loop. The BY parameter is optional and indicates the increment va lue for X on each pass through the FOR loop. Yes, this value may be a larger val ue, and it works great. Now, once this is converted to a FOR loop, it can be easily ported to free- form at syntax, as follows: /free X = 1 to 100 by 1; arr(x) = ' '; endfor; /end-free Note that the EVAL opcode is optional in free format, so I've removed it. FOR DO 100 X

I also mentioned that the FOR opcode can count down as well as count up while lo oping. To do that, you change the TO parameter to the DOWNTO and adjust the star ting value for X (or whatever your counter variable is). /free X = 50 downto 1; arr(x) = ' '; endfor; /end-free This starts out with X set to 50 and loops through the FOR loop, decrementing X by 1 each time. Yes, the default increment and decrement value is 1, so the BY c lause is often not needed. So there you have it, yet another stumbling block removed for moving to free for mat. Enjoy! -------------------------------------------------------------------------------Bob Cozzi lectures on RPG IV and System i development at corporate customers and user groups all year long. He is author of " RPG TnT: 101 Dynamic Tips 'n Techn iques with RPG IV" and hosts i a weekly video podcast for RPG developers and man agers aired live on RPGWorld.com at Noon Eastern time every Friday (16:00 GMT). Bob also produces RPG World the most popular RPG IV Developer conference of the year. The next RPG World conference is planned for May 2009 in Las Vegas. Start planning now for your 2009 trip to RPG World and join Bob Cozzi, Bruce Hoffman, Greg Veal, Aaron Bartell, and more for the best RPG and System i developer train ing you can find. Details are posted at RPGWorld.com Bob Cozzi's RPG World 2009 Conference and ShowCase is planned for May 6 to 9, 20 09, in Las Vegas. Start planning and setting up your budgets for the best RPG tr aining you can find. Our attendees are coming back year after year because they enjoy not only the lectures and training but also the interaction with colleague s and vendors. Ask your manager to budget for RPG World 2009 now so you won't ha ve to later. Bookmark/Search this post with: Login to post comments Email this page Printer-friendly version Relate d Links RPG for the Web Build True Date Values Grab Data from the Screen Keylists in Free Format Goofy Qualified Named Constants 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 FOR

You might also like