Testing for a range, a list, or specific select/omit values is a tradition in programming. The modern RPG language provides limited support for these operations with the IF operation.
For a simple range, traditional RPG programs have used various techniques (as shown in Figure 7.17 and 7.18). Whenever possible, the modern RPG language programmer avoids using indicators and uses the technique illustrated in Figure 7.17. The COMP operation, as shown in Figure 7.18, is less desirable.
.....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq C Do 35 X 3 0 C Move Name(x) CHAR 1 C If Char >= 'a' and Char <= 'z' C BitOn '1' NAME(x) C Endif C EndDo
.....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq... C LOOP tag C Add 1 X 3 0 C Move NAME(X) char 1 C char comp 'a' 21 21 C 21char comp 'z' 2323 C 21 CAN 23 BitOn '1' Name(x) C X comp 35 56 C n56 GOTO LOOP
The code in Figure 7.17 states that:
If CHAR is Greater than or Equal to 'a' AND it is Less than or Equal to 'z', then...
Figure 7.17 is the preferred technique because it says exactly what it does. The technique shown in Figure 7.18 performs exactly the same function, but it is convoluted and inflexible. At best, maintenance is complicated. Testing a list of values requires the use of the IF operation along with the OR operator. For example, to test for the occurrence of the letters R, P, G, or the number 4 in the NAME array, the IF operation is used in conjunction with the OR operation. See Figure 7.19 and 7.20.
.....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq... C DO 35 X 3 0 C Move NAME(x) char 1 C If Char='R' OR char='P' OR char='G' or Char='4' C EXSR RPG4 C EndIf C EndDo
.....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq C LOOP tag C ADD 1 X 3 0 C Move NAME(x) char 1 C CHAR COMP 'R' 75 C N75CHAR COMP 'P' 75 C N75CHAR COMP 'G' 75 C N75CHAR COMP '4' 75 C 75 EXSR RPG4 C X COMP 35 75 C 75 GOTO LOOP
Figure 7.19 shows the preferred technique. Its straightforward approach is concise and can be modified easily. On the other hand, the technique shown in Figure 7.20 is complex and cumbersome. Care should be taken when modifications are made.