WHEN to DO AND IF OR END – conditional selection in RPGLE

AS400

Oct 10

Conditional selection in RPGLE

In RPGLE 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 in RPGLE

If we want some RPGLE Statement(s) to be executed conditionaly. We put these statements inside an If-EndIf block. We specify the condition in the factor 2 of the If opcode. The following example will clarify the If-EndIf in ILE RPG.

Simple If Example in RPGLE

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords
DW@Char S 1A Inz('A')
**
CL0N01Factor1+++++++Opcode&ExtExtended-factor2
C If W@Char = 'A'
C W@Char Dsply
C EndIf
**
C Return

The output of the above program is as expected ‘A’

DSPLY A

The If-Else-EndIf in RPGLE

If we want some RPGLE Statement(s) to be executed conditionaly and different set of statement if the condition fails, we use If-Else-EndIf blocks.
The following example will clarify it further.

Simple If-Else Example in RPGLE

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++
DW@Char S 1A Inz('A')
DW@Msg1 S 20A Inz('W@Char is A')
DW@Msg2 S 20A Inz('W@Char not ''A''')
**
CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++
C If W@Char = 'A'
C W@Msg1 Dsply
C Else
C W@Msg2 Dsply
C EndIf
**
C Return

The above program displays the text of W@Msg1. You can try to run the else condition by initializing W@Char with ‘B’ or some other character.

DSPLY W@Char is A

The If-ElseIf-Else-EndIf in RPGLE

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! Anyways The following example will illustrate the If-ElseIf-Else-EndIf syntax.

Simple If-ElseIf-Else-EndIf Example in RPGLE

DW@Char S 1A Inz('A')
DW@Msg S 20A
**
CL0N01Factor1+++++++Opcode&ExtExtended-factor2++++++++++
C
If W@Char = 'B'
C Eval W@Msg = 'Inside If'
**
C ElseIf W@Char = 'A'
**
C Eval W@Msg = 'Inside ElseIf'
**
C Else
**
C Eval W@Msg = 'Inside Else'
C EndIf
**
C W@Msg Dsply
**
C Return

The above program displays the text of W@Msg at the end of the If-ElseIf-Else-EndIf block. Try to see the display value of message by changing the value of W@Char.

DSPLY Inside ElseIf

Nesting in RPGLE

Just like any other language, RPGLE also, allows nesting of several If_ElseIf-Else-EndIf. For all practical purposes, you can rest assured there will not be any problem in streching the nesting into multiple levels. However, using Select-When_EndSl is recommended if the nesting extends more than 3 levels.The follwoing example shows a typical nesting of ILE RPG If-Else.

Example of nested conditions in RPGLE

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++
DW@Char1 S 1A Inz('A')
DW@Char2 S 1A Inz('B')
DW@Msg S 20A
**
CL0N01Factor1+++++++Opcode&ExtExtended-factor2++++++++++++
C If W@Char1 = 'A'
**
C If W@Char2 = 'B'
C Eval W@Msg = 'Inside nested if'
C Else
C Eval W@Msg = 'Inside nested Else'
C EndIf
**
C EndIf
**
C W@Msg Dsply
**
C Return

The output of the above program is given below. Try to run different conditions of the above program.

Follow

About the Author

IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Alpha Nerd and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and in the words of the most interesting man in the world: Stay thirsty my friend.

%d bloggers like this: