CODING DECISIONS - IBM Mainframe

For coding decisions between one course of action and another, choose from these COBOL statements:

  • PERFORM statement
  • IF statement
  • IF statement with a null branch
  • Nested IF statements
  • EVALUATE statement
  • SEARCH .statement

We have seen the PERFORM statements in the previous sections. We will see the different forms of IF statement and the EVALUATE statement in the following sections.

IF Statement

Use IF . . . ELSE to code a choice between two processing actions. (The word THEN is optional.) For example:

IF Statement

IF Statement with a Null Branch

One of the two processing choices for an IF statement can be no action. There are two ways you can code an IF statement when one of the processing choices is no action. Because the ELSE clause is optional, you can code the following:

IF Statement with a Null Branch

This coding is suitable for simple programming cases. However, if the logic in you program is complex (for example, you have nested IF constructs with action for only one of the processing choices), you may want to use the ELSE clause and code the null branch of the IF statement with the CONTINUE statement:

IF statement with the CONTINUE statement

Nested IF Statements

When an IF statement has another IF statement as one of its possible processing branches, these IF statements are called nested IF statements. Theoretically, there is no limitation on the depth of nested IF statements. However, when the program has to test a variable for more than two values, using an EVALUATE statement is a better choice. Use nested IF statements sparingly; the logic can be difficult to follow, although proper indentation helps.

When you code a nested IF structure, use the END-IF to close the inner nested IF because a period would terminate the outer IF structure as well. When IF statements are nested, readability and debugging will be easier if each IF statement has its own END-IF scope-terminator and if proper indentation is used. So instead of coding like this:

Nested IF Statements

In the above code the ELSE PERFORM D can be the ELSE part of IF A=1 or IF B= 2. If the logic makes it the ELSE part of IF B = 2 code the statements as follows:

2 code the statements as follows

2 code the statements as follows

If the intent is to have ELSE PERFORM D depend on IF A = 1, the code would look like this:

2 code the statements as follows

EVALUATE Statement

The EVALUATE statement is an expanded form of the IF statement. An IF statement allows your program to act on one of two conditions: TRUE or FALSE. When the condition is true, one action is performed. When the condition is not true, a different action is performed (the ELSE clause). If you had three or more possible conditions instead of just two, and you were limited to using IF statements, you would need to nest or cascade the IF statements. Such nested IF statements are a common Source of logic errors and debugging problems.With the EVALUATE statement, you can test any number of conditions in a single statement and have separate actions for each. In structured programming terms, this is a "case" construct. It can also be thought of as a form of decision table.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM Mainframe Topics