TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

Conditional Instructions


After completing this chapter, you will be able to describe the conditional statements and it's different types with examples.


There are two types of conditional instructions. IF/THEN/ELSE can direct the execution of an exec to one of two choices. SELECT/WHEN/OTHERWISE/END can direct the execution to one of many choices.

IF/THEN/ELSE Instructions:

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 separate the THEN clause from the ELSE clause with a semi-colon.

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
      SAY 'Would you like to play tennis or golf?'
      PULL answer
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 'Shall we play tennis?'
    ELSE NOP
  END
ELSE
    SAY 'Shall we 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?


Example Using Missing Instructions:

One way to format output is to use variables and concatenation operators as in the following example. A more sophisticated way to format information is with parsing and templates.

/********************************* REXX *******************************/
/* This exec demonstrates what can happen when you do not include DOs,*/
/* END's and ELSEs in nested IF/THEN/ELSE instructions.	*/
/**********************************************************************/
weather = 'fine'
equipment = 'ball'
tenniscourt = 'occupied'
IF weather = 'fine' THEN
    SAY 'What a lovely day!'
    IF tenniscourt = 'free' THEN
        SAY 'Shall we play tennis?'
ELSE
    SAY 'Shall we take our raincoats?'

By looking at the exec you might assume the ELSE belongs to the first IF. However, the language processor associates an ELSE with the nearest unpaired IF. The outcome is as follows:

Output:

What a lovely day!

Shall we take our raincoats?


SELECT/WHEN/OTHERWISE/END Instruction:

To select one of any number of choices, use the SELECT/WHEN/OTHERWISE/END instruction.

The syntax of SELECT/WHEN/OTHERWISE/END instruction as follows:

sasasass
SELECT
      WHEN	expression	THEN	instruction
      WHEN	expression	THEN	instruction
      WHEN	expression	THEN	instruction
      .
      .
      .
      OTHERWISE
          instruction(s)
END

The language processor scans the WHEN clauses starting at the beginning until it finds a true expression. After it finds a true expression, it ignores all other possibilities, even though they might also be true. If no WHEN expressions are true, it processes the instructions following the OTHERWISE clause.

As with the IF/THEN/ELSE instruction, when you have more than one instruction for a possible path, begin the set of instructions with a DO and end them with an END. However, if more than one instruction follows the OTHERWISE keyword, DO and END are not necessary. Each SELECT must end with an END. Indenting each WHEN makes an exec easier to read.

Example Using SELECT/WHEN/OTHERWISE/END:

/*************************** REXX ************************************/
/* This exec receives input with a person's age and sex. In reply it */
/* display a person's status as follows                              */
/*	BABIES – under 5.                                                */
/*	GIRLS – female 5 to 12                                           */
/*	TEENAGERS – 13 through 19                                        */
/*	WOMEN – female 20 and up                                         */
/*	MEN – male 20 and up                                             */
/*********************************************************************/
PARSE ARG age sex . '
SELECT
      WHEN age < 5 THEN           /* person younger than 5 */
            status = 'BABY'
      WHEN age < 13 THEN          /* person between 5 and 12 */
            DO
              IF sex = 'M' THEN   /* boy between 5 and 12 */
                  status = 'BOY'
              ELSE
                  status = 'GIRL'  /* girl between 5 and 12 */
            END
      WHEN age < 20 THEN
            status = 'TEENAGER'     /* person between 13 and 19 */
      OTHERWISE
            IF sex = 'M' THEN       /* man 20 or older */
                status = 'MAN'
            ELSE                    /* woman 20 or older */
                status = 'WOMEN'
END
SAY 'This person should be counted as a' status '.'

Summary:

  • SELECT case structure and IF..ELSE conditional statements are used for conditional processing.

  • DO..END are used to execute a block of statements.


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal