How to use ‘Conditional Selection in RPGLE and CLLE’
Way back in 2008, I wrote this blog about how to do various condition selection in RPG. So, I suppose it time to modernize those code examples in modern RPGLE Free, tidy it up, and add the same code examples in CLLE.
In RPGLE (and in CLLE sorta kinda) we have the following conditional opcodes available:
- The If-EndIf block
- The If – Else- EndIf block
- The If – ElseIf – Elseif – EndIf block
- The Select Block
Let’s look at them one by one.
The If-EndIf Programming Condition
If we want some code Statement(s) to be executed conditionally. We put these statements inside an If-EndIf block:
Simple *IF* in RPGLE
dcl-s VARIABLE char(1) Inz('A'); If VARIABLE = 'A'; Dsply VARIABLE; EndIf; Return;
Simple *IF* in CLLE
DCL VAR(&VARIABLE) TYPE(CHAR) LEN(1) VALUE('A') IF COND(&VARIABLE *EQ 'A') THEN(DO) SNDPGMMSG MSG(&VARIABLE) ENDDO RETURN
The output of the above program is as expected ‘A’
The *If-Else-EndIf* Programming Condition
If we want some RPGLE Statement(s) to be executed conditionally and a different set of statements if the condition fails, we use If-Else-EndIf blocks.
Let’s look at the same logic with a little more complexity added.
Simple If-Else-End in RPGLE
dcl-s VARIABLE char(1) inz('A'); dcl-s PASSTEST char(20) Inz('variable is A') ; dcl-s FAILTEST char(20) Inz('variable not A'); If VARIABLE = 'A'; Dsply PASSTEST; Else; Dsply FAILTEST; EndIf; Return;
Simple *IF* in CLLE
PGM DCL VAR(&VARIABLE) TYPE(CHAR) LEN(1) VALUE('A') IF COND(&VARIABLE *EQ 'A') THEN(DO) SNDPGMMSG MSG(&VARIABLE) ENDDO ELSE CMD(DO) SNDPGMMSG MSG(&FAILTEST) ENDDO RETURN ENDPGM
Note the difference in CLLE is that each IF group has a corresponding ENDDO statement then the next ELSE group follows.
Alternative *IF* in CLLE
We could also do this with single statements like this:
PGM DCL VAR(&VARIABLE) TYPE(CHAR) LEN(1) VALUE('A') IF COND(&VARIABLE *EQ 'A') THEN(SNDPGMMSG MSG(&VARIABLE)) ENDDO ELSE CMD(DO) SNDPGMMSG MSG(&FAILTEST) ENDDO RETURN ENDPGM
The If-ElseIf-Else-EndIf Programming Condition
(this is the same as the SELECT condition which we will look at in the next section)
If we have multiple sets of condition-statements then we can go for If-ElseIf-Else-EndIf statements.
The different conditions are specified as factor 2 of If and ElseIf opcodes. The Else in this condition-rule is optional.
Actually, I think the If-ElseIf-Else-EndIf is the only conditional block in RPGLE where Only the first opcode ‘If’ and its ending opcode ‘EndIf’ are mandatory!
Simple *If-ElseIf-Else-EndIf* Example in RPGLE
Let’s make this a bit more interesting by adding an input parameter (‘p_input’) and checking for various conditions
dcl-pi *n; p_input char(1); end-pi; If p_input = 'A'; Dsply 'Input value was A'; elseif p_input = 'B'; Dsply 'Input value was B'; elseif p_input = 'C'; Dsply 'Input value was C'; Else; Dsply 'Input value was not A, B or C'; EndIf; Return;
The final ELSE is equivalent to saying “if all the IFELSE statements fail then do this”.
If I compile and run this from the command line it look like this:
Simple *If-ElseIf-Else-EndIf* Example in CLLE
I would like to do the same thing in CLLE but there is no ELSEIF command in CLLE (not right now anyway)
But we can easily do the ELSEIF logic by using a SELECT statement:
*SELECT* in CLLE
PGM PARM(&P_INPUT) DCL VAR(&P_INPUT) TYPE(*CHAR) LEN(1) SELECT WHEN COND(&P_INPUT *EQ 'A') THEN(DO) SNDPGMMSG MSG('Input value was A') ENDDO WHEN COND(&P_INPUT *EQ 'B') THEN(DO) SNDPGMMSG MSG('Input value was B') ENDDO WHEN COND(&P_INPUT *EQ 'C') THEN(DO) SNDPGMMSG MSG('Input value was C') ENDDO OTHERWISE CMD(DO) SNDPGMMSG MSG('Input value was not A, B or C') ENDDO ENDSELECT RETURN ENDPGM
The OTHERWISE is equivalent to saying “if all else fails do this”.
Pretty obvious stuff once you know the syntax right?
Executing this little bit of code looks like this:
*SELECT* in RPGLE
Now the exact same thing in RPGLE is marginally different but still essentially the same:
dcl-pi *n; p_input char(1); end-pi; select; when p_input = 'A'; Dsply 'Input value was A'; when p_input = 'B'; Dsply 'Input value was B'; when p_input = 'C'; Dsply 'Input value was C'; other; Dsply 'Input value was not A, B or C'; EndSl; Return;
And there you have it. 🙂