WHEN to DO AND IF OR END - conditional selection

Conditional Execution in RPGLE

In RPGLE we have the following conditional opcodes available.
The If-EndIf block.
The If - Else- EndIf block.
The If - ElseIf - Else - EndIf block.
We will learn 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.



Current weather

Roanoke, Virginia USA

Overcast
  • Overcast
  • Temperature: 57.2 °F
  • Wind: West, 4.6 mph
  • Pressure: 996 hPa
  • Rel. Humidity: 59 %
  • Visibility: 10 mi
Reported on:
Sat, 03/13/2010 - 11:54

Locations of visitors to this page

Bunch of Twits

  • adds 'Thievery Corporation' to his list of groovey background music to play while programming :) 1 day 2 hours ago
  • is watching very serious blokes playing plastic darts.... wtf is that all about? 1 day 16 hours ago
  • has discovered that his mighty hammer Mjölnir is missing. 4 days 4 hours ago
  • grabs a coffee and settles down for a day of post implementation checks, project closure and user documentation... bah! 5 days 4 hours ago
  • is about to embark on an IHOP pancake eating frenzy with the sprogs... 6 days 4 hours ago
  • just watching tv, a female marine running up a beach, teagan says 'i hope shes not wearing heels because thats hard' ;) 6 days 16 hours ago
  • saturday morning, determined to do no work today just gonna hang out with the kids, walk the mutts, watch movies and vejjjjjj... hurrah! 1 week 1 hour ago
  • is al about rpg3, rpg4, php, asp, IBMi, html, xml, wsdl's and any other acronyms I can think of.... 1 week 1 day ago
  • is pretending to type something important to avoid listening to someone talking a load of codswallop. 1 week 1 day ago
  • see... DOTnetnuke.... Microsoft products make my brain shout 'Uncle!' 1 week 3 days ago

Who's online

There are currently 0 users and 34 guests online.