Nested if...then...else instructions – IBM SC34-5764-01 User Manual

Page 56

Advertising
background image

As a REXX instruction, the flowchart example looks like:

IF expression THEN instruction

ELSE instruction

You can also arrange the clauses in one of the following ways to enhance readability:

IF expression THEN

instruction

ELSE

instruction

or

IF expression

THEN

instruction

ELSE

instruction

When you put the entire instruction on one line, you must use a semicolon before the ELSE to separate
the THEN clause from the ELSE clause.

IF expression THEN instruction; ELSE instruction

Generally, at least one instruction should follow the THEN and ELSE clauses. When either clause has no
instructions, it is good programming practice to include NOP (no operation) next to the clause.

IF expression THEN

instruction

ELSE NOP

If you have more than one instruction for a condition, begin the set of instructions with a DO and end them
with an END.

IF weather = rainy THEN

SAY 'Find a good book.'

ELSE

DO

PULL playgolf

/* Gets data from input stream */

If playgolf='YES' THEN SAY 'Fore!'

END

Without the enclosing DO and END, the language processor assumes only one instruction for the ELSE
clause.

Nested IF...THEN...ELSE Instructions

Sometimes it is necessary to have one or more IF...THEN...ELSE instructions within other
IF...THEN...ELSE instructions. Having one type of instruction within another is called nesting. With nested
IF instructions, it is important to match each IF with an ELSE and each DO with an END.

IF weather = fine THEN

DO

SAY 'What a lovely day!'
IF tenniscourt = free THEN

SAY 'Let''s play tennis!'

ELSE NOP

END

ELSE

SAY 'We should take our raincoats!'

Not matching nested IFs to ELSEs and DOs to ENDs can have some surprising results. If you eliminate
the DOs and ENDs and the ELSE NOP, as in the following example, what is the outcome?

Control Flow within a Program

34

CICS TS for VSE/ESA: REXX Guide

Advertising