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

October 10, 2008

By  NickLitten

## 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.

### NickLitten

IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad 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 remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

## Swap Homes and Travel the World â€“ Join Home Exchange

Swap Homes and Travel the World – Join Home Exchange

## Surf the Web Securely with OPEN DNS

Surf the Web Securely with OPEN DNS

## IBM i Software Change Management â€“ CMS vs ALM â€“ Whatâ€™s in a Name?

IBM i Software Change Management – CMS vs ALM – What’s in a Name?

## Want to learn CL Programming?

Want to learn CL Programming?
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

### Get In Touch

Iâ€™m always looking for awesome input, feedback and critique!

## 20% Discount

I have partnered with SNUG CBD givING you Organic CBD
20% discount code "NL20"