From: Doug Payton To: All
Perhaps many RPG programmers already know of the following technique. I think it is worthwhile because it makes screen error checking and function key use very readable.
6 contains an example of a screen-handling program which must validate the month and year entered. For this example, the year must be the current year.
Figure 6 contains an example of a screen-handling program which must validate the month and year entered. For this example, the year must be the current year.
Before the ITER and LEAVE operation codes were introduced, I would use IF and ELSE. As a result, I would often end up with more than 10 ENDs at the bottom of the loop. Now the ITER and LEAVE allow me to put the ENDIF directly after the ITER. Control reverts to the end of the DO loop I am currently in. LEAVE exits me from the loop entirely. The LEAVE right before the ENDDO is optional. For instance, if you want to redisplay the screen after the submit, remove LEAVE.
If someone has a better approach, please share.
From: Ted Holt To: Doug Payton
First of all, I do not like the LEAVE and ITER operation codes. My understanding of structured programming is that every structure, which can be a loop or a subroutine, or just a related group of calculations, should have only one entry point and one exit point. LEAVE and ITER violate this concept.
Besides that, trying to figure out which loop will be affected by the LEAVE or ITER gets awfully confusing. I realize that I'm asking for trouble, but I believe that GOTO and TAG, used in a structured way, are clearer than LEAVE and ITER.
7 illustrates how you would write the same procedure without LEAVE and ITER.
Figure 7 illustrates how you would write the same procedure without LEAVE and ITER.
I like to put the verification calculations in their own subroutine, since verifying the keyed data is a task of its own. The ERROR variable functions like a parameter passed back from the routine, since RPG subroutines can't accept parameters.
From: Doug Payton To: Ted Holt
I see your point about LEAVE and ITER. I just happen to like them and use them quite a lot. I hate GOTOs because you can GOTO practically anywhere in the program you'd like, which is nearly impossible to read after enough of them have been implemented. When I use ITER and LEAVE, I place a comment on the same line documenting which DO loop I'm in.
Figure 6 Sample Interactive RPG Program
*. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 C 1 DOWEQ1 C EXFMTFORMAT1 C *IN03 IFEQ *ON =F3 C LEAVE =Exit program C ENDIF * C IBMO IFLT 1 C IBMO ORGT 12 * Your error routine goes here C ITER =Re-send screen C ENDIF * C IBYR IFNE UYEAR * Your error routine goes here C ITER =Re-send screen C ENDIF * . * No errors, submit. * . C LEAVE =Exit, no errors, * and job was submitted C ENDDO =1DOWEQ1 C MOVE *ON *INLR *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7
Figure 7 Eliminating LEAVE and ITER with Structured Code*. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 C ERROR DOUEQ'0' C CANCEL OREQ '1' C EXFMTFORMAT1 C MOVE *IN03 CANCEL 1 C CANCEL IFEQ '0' C EXFMTVFY1 verify input C ERROR IFEQ '0' * . * No errors, submit. * . C ENDIF C ENDIF C ENDDO C MOVE '1' *INLR * C VFY1 BEGSR * C MOVE '0' ERROR 1 assume all OK C IBMO IFLT 1 C IBMO ORGT 12 C IBYR ORNE UYEAR C MOVE '1' ERROR C ENDIF * C ENDSR *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7