IBM gave RPG a CASE structure of sorts when it introduced the CASxx operation many years ago. CASxx has one great disadvantage, however: it forces you to create subroutines for each branch of the CASE structure, even if you wanted to execute one statement only.
With V2R1 we now have the SELEC/WHxx/OTHER structure (let's call it SELEC for short), which can be thought of as a multi-path decision fork, just like CASxx. Both SELEC and CASxx execute only the first path of the fork that has a true condition.
SELEC, however, is far superior to CASxx. Here are the reasons why you should embrace the new operations:
The program can contain immediate code to be executed in each branch; you're not forced to execute subroutines.
The WHxx operation can be followed by ANDxx and ORxx, making it possible to select the correct branch depending on more than one condition.
Each WHxx branch, and the OTHER branch, can have another SELEC/WHxx/OTHER structure, which is great when you need to select a branch based on combinations of two conditions.
To give you an example, look at 4; it represents the typical processing of an interactive program. This program presents screen PANEL, which has F3, F6, and F12 enabled. Our RPG program must perform different tasks depending on what key was pressed. Each block of code is delimited and labeled in the figure.
To give you an example, look at Figure 4; it represents the typical processing of an interactive program. This program presents screen PANEL, which has F3, F6, and F12 enabled. Our RPG program must perform different tasks depending on what key was pressed. Each block of code is delimited and labeled in the figure.
Block A is the main interactive loop: it repeats processing for as long as ENDPGM is 'N' (no). Screen PANEL is presented. Then, block B decides what path to take. If F3 or F12 are pressed, 'Y' is moved to ENDPGM, signaling the end of the program. If F6 is pressed, subroutine CREATE is executed. Otherwise, we assume that the user pressed the Enter key.
This application program uses a cursor-sensitive key, which only assigns different values to CELL, dividing the panel in nine parts, like a tic-tac-toe board. Block C selects different paths according to the row number of the cursor: it can be less than or equal to 25, less than or equal to 50, or other. In each case we must use another SELEC (blocks D, E, and F) to select the path depending on the column number of the cursor.
Now you can start using the SELEC/WHxx/OTHER structure. I think you'll agree with me that it's much more flexible than CASxx.
TechTalk: A CASE for RPG/400
Figure 4 Illustration of the SELEC structure
Figure 4: Illustrating the SELEC Structure ....1....+....2....+....3....+....4....+....5....+....6....+....7 C ENDPGM DOWEQ'N' A----------+ C EXFMTPANEL | * | C SELEC B-------+ | C *IN03 WHEQ *ON || C *IN12 OREQ *ON || C MOVE 'Y' ENDPGM || C *IN06 WHEQ *ON || C EXSR CREATE || C OTHER || C SELEC C----+ || C ROW# WHLE 8 || || C SELEC D-+ || || C COL# WHLE 25 || || || C MOVE '7' CELL || || || C COL# WHLE 50 || || || C MOVE '8' CELL || || || C OTHER || || || C MOVE '9' CELL || || || C ENDSL --+ || || C ROW# WHLE 16 || || C SELEC E-+ || || C COL# WHLE 25 || || || C MOVE '4' CELL || || || C COL# WHLE 50 || || || C MOVE '5' CELL || || || C OTHER || || || C MOVE '6' CELL || || || C ENDSL --+ || || C OTHER || || C SELEC F-+ || || C COL# WHLE 25 || || || C MOVE '1' CELL || || || C COL# WHLE 50 || || || C MOVE '2' CELL || || || C OTHER || || || C MOVE '3' CELL || || || C ENDSL --+ || || C ENDSL -----+ || C ENDSL --------+ | C ENDDO -----------+ * C MOVE *ON *INLR
LATEST COMMENTS
MC Press Online