The Do Operations - Free-format RPG IV provides two Do operations: Dow (Do while) and Dou (Do until). For Part One of this article set read here.
Editor's Note: This article is excerpted from chapter 6 of Free-Format RPG IV: Third Edition, by Jim Martin.
Although the fixed form of the Do operation isn’t available in free format, the For operation (covered later in this chapter) contains all the functionality of Do.
Do While
The Dow operation uses a comparison expression, similar to the If operation. The expression is evaluated, and when it is found to be true, program control continues on the next line after the Dow operation. Any number of operations may follow the Dow operation.
The Dow operation has a looping control point at an Enddo operation. (Remember, no generic End operation exists in free format.) At the Enddo, program control is immediately returned to the Dow operation. The comparison expression is then checked again; if it is true, program control continues on the next line following the Dow.
As long as the Dow comparison expression resolves to true, the program continues in a loop. When the expression resolves to false, program control jumps to the first operation after the Enddo that is paired with the Dow operation, ending the loop.
With most Dow groups, a loop-control condition is set just prior to the group and near the end of the group, just before the Enddo operation. Listing 6-4 shows an example of a file read loop that uses Dow.
Listing 6-4: Using Do while (Dow) to read a file until end-of-file
Do Until
Like the If and Dow operations, the Dou operation uses a comparison expression. Dou sets up a future check but otherwise does nothing. Program control flows immediately to the next operation after the Dou. The controlling condition of the loop is usually set soon after the Dou, and an If test is placed afterward to determine whether to continue in the loop. You normally place the Endif for this If just before the Enddo. If the If resolves to true, the program continues after the If, keeps on going after the Endif, and finally comes to the Enddo. At this control point, the Dou condition is tested. If the expression resolves to true, program control resumes at the next operation after the Enddo. If the expression resolves to false, program control “jumps” back (loops back) to the Dou operation.
Listing 6-5 shows an example of a file read loop that uses Dou.
Dow and Dou Differences
The difference between Dow and Dou—and it’s a big one—lies in when the comparison expression is checked, as well as in the program-control action. Program-controlled looping is common, and programmers nearly always adopt one of these operations as their preferred method. In most situations, either approach will provide a satisfactory solution.
If you want to set the controlling condition just once, a do until is the correct form to use. A do while may be the better choice if prior programming statements have already set the controlling condition. Which do loop you choose will depend on many factors, but the biggest factor is probably personal preference.
For
A For operation and its termination control point, Endfor, define a controlled-loop group of operations. The For group uses an initial specified index value, an increment index value (or default), and a termination value. You can specify the For group indexing to either increment or decrement the current index before checking to see whether the result meets the termination condition.
A For group uses an index variable, as in the following example:
You must define the indexing variable in definition specifications as a numeric field large enough to handle the largest index value. This example specifies no increment, so 1 is used as the default. The loop termination value in the example is 10. If the value of index j is 10 or less, program control will continue to the next operation after the For. At the Endfor, control returns to the For operation, where the index is incremented (or decremented) and then compared with the termination value. If the index is greater than the termination value (if incrementing), program control jumps to the operation immediately after the Endfor operation. If the index is not greater than the termination value, control continues at the next operation after the For operation. The index used in the For group can be used within the group and changed if desired.
A For group can also start with an index value higher than the termination value and decrement until the current index is less than the termination value:
In this example, the index j has an initial value of 100 and a termination value of 1. The index in this case is 2. The first time through the For group, the value of j is 100. The next time through, j equals 98, then 96, and so on until j equals 2. When j equals 2, the For group is again performed. Control then returns to the For statement, where j is decremented by 2, yielding a j value of 0 (zero). Because j’s value is now less than the termination value (1), control passes to the next operation after the Endfor statement.
Listing 6-6 shows some additional examples of For.
Listing 6-6: Using the For operation for controlled looping
Loop Interrupt
The loop operations Dou, Dow, and For normally end when the loop’s index termination requirement is met. There are times in programming when you need to escape from a loop—either all the way out of it or just to its next iteration. RPG IV’s loop interrupt operations Leave and Iter perform these functions for us. (Remember, free-format RPG IV provides no Goto operation.)
Leave
The Leave operation causes program control to jump to the next operation after the current Dou, Dow, or For group. The effect is equivalent to a Goto, but it occurs in a structured way. You may be thinking that this operation’s purpose is primarily error handling. Not so. Let’s say you are using a For group to load 10 records from a database into a subfile. However, after five successful record reads, you come to end-of-file. To exit the subfile load routine, you can just set up an “If end-of-file” condition and leave immediately after the Read operation.
The code in Listing 6-6 (above) uses Leave after locating a correct value in a string. Listing 6-7 shows a sample subfile load routine that uses Leave.
Listing 6-7: Using Leave in a load routine to exit at end-of-file
Iter
The Iter operation is the other loop interrupt. Specifying Iter causes the program to jump to the current Enddo or Endfor operation (depending on which loop we are in), at which point the normal function of the Enddo or Endfor is performed. As with Leave, the effect is equivalent to a Goto, but, again, the action takes place in a structured way.
A good example of using Iter is what I call “one-at-a-time” error reporting on a data entry panel. If a panel allows entry of 12 different fields, any of which could be entered with invalid data, either we check them one at a time and loop back with one error message or we find all the errors and use a message subfile. To use the first method, just check each field. If the first field is okay, check the second, and so on. If an error occurs at any point, set up for the correct error message, and use Iter to skip all further error checking.
Listing 6-8 illustrates this scenario.
Listing 6-8: Using Iter to skip to the next iteration
The Select Group
The Select operation, with its corresponding operations When, Other, and Endsl, creates a procedural structure very similar to If, Elseif, Else, and Endif.
Select
You code the Select operation on a line by itself, and it starts the select group. Following the Select, you can specify a When operation with a comparison expression. If the expression resolves to false, control is passed to the next When with its comparison expression. The false jumps continue until either an Other statement or the Endsl is reached.
The Other operation is optional. If all When expressions yield false, no action is taken within the Select group unless you have specified an Other operation.
Other means “if none of the above” is true, perform the operations between the Other operation and the Endsl. If any of the When expressions is true, the operations specified between the When that is true and the next When (or Other) are performed, and control then jumps to the Endsl operation. In a Select group with no Other operation, either one set of operations is performed or no operations are performed. If the Select group has an Other operation at the end of the group, at least one set of operations will be performed.
Listing 6-9 shows an example of Select, When, Other, and Endsl.
Listing 6-9: Using Select, When, Other, and Endsl operations
Operations Absent in Free Format
Many RPG programmers are acquainted with the fixed-format Cas (Case) operation and its two-letter suffixes EQ, NE, GT, GE, LT, and LE. These operations are not available in free format, but you can easily replace them by using Select and When operations with Exsr (Perform a subroutine) operations to call the subroutines.
As you have learned, free format also lacks a Goto operation. Doing without a Goto isn’t such a bad thing. Programming style texts and magazine articles have argued nonstop for decades about the good and bad points of using a Goto operation. Rather than anguish over a loss of Goto freedom, let’s look on the bright side: No Goto means no spaghetti code! Many of us have had to sort out programs written by programmers who used Gotos—here, there, and everywhere. It was nothing short of a miracle that the code even worked. Maintaining this kind of code is a programmer’s nightmare.
In earlier versions of RPG, the use of Goto was pretty common. Now, Leave,
Iter, and even LeaveSr (Leave subroutine) let us perform Goto-like functions in a clear and orderly way. Armed with these structured operations, we don’t need a “real” Goto, just an understanding of these loop interrupters. The lack of a Goto operation forces us to think of ways to organize our program logic using structured programming techniques. The end result is programs that are easier to understand and maintain.
More of Jim's Free-Format RPG IV is coming soon in an upcoming issue of MC RPG Developer. Can't wait? You can pick up Jim Martin's book, Free-Format RPG IV: Third Edition at the MC Press Bookstore Today!
LATEST COMMENTS
MC Press Online