ILE RPG, RPG IV Opcode Select [Conditional Blocks]
Similar to the switch statements in PHP, RPGLE has Select statements. But the Select in RPGLE differs a little with switch-case of PHP. The syntax of a Select Statement in RPGLE is as given below.
Select When Condition1 ….Statement1 ….Stement2 When Condition2 ….Statement3 ….Statement4 Other …Default Statements EndSl
Here the Program scans the conditions given in factor 2 of the When opcode one by one. First of all the condition1 is checked. If it is true then the statements1 and statements 2 are executed. After the execution of all the statements, the control goes to the statement immediately following the opcode EndSl. Unlike "Switch – Case" there is no need of any break statement. Only one "When" block (the condition of which is true) is executed inside a select statement in RPGLE.
If the first condition fails then condition 2 is evaluated. If the condition is true this block is executed. If none of the conditions is found to be true, then the "other" block is executed. Here, It is important to note that, the "other" opcode is optional in RPG ILE "Select". If you do not write this opcode then and all the "When conditions" fail, then the entire "Select-EndSl" block will be skipped and no "When" block will be executed.
The following program will clarify the select concept.
Example of RPGLE Select:
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++ D@Entry S 15P 5 DW@Msg S 50A CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result+++++++ C *Entry PList C Parm @Entry ** Demo of select OpCode. C Select C When @Entry < 100 C Eval W@Msg = 'Less than hundred' ** C When @Entry > 100 C Eval W@Msg = 'More than hundred' ** C Other C Eval W@Msg = 'Exactly hundred!' C EndSl ** C W@Msg Dsply C Return
The output of the above example is given below.
DSPLY Less than hundred //Entered 5 DSPLY More than hundred //Enter 105 DSPLY Exactly hundred! //Guess?
Note:The opcode "Other" is unconditional.
- 5709 reads
