Thursday, 4 September 2014

COBOL Decision Making: IF, EVALUATE, and PERFORM Examples

A batch program reads an account record and must choose one path: accept it, reject it, route it to review, or skip it because the file reached end. COBOL decision making is the set of statements that makes that choice visible in the program: IF, EVALUATE, and PERFORM.

COBOL decision making diagram showing IF, EVALUATE, and PERFORM control flow
Pick the clear branch.

What is decision making in COBOL?

Decision making means testing data and running the matching statements. In a mainframe program, the tested data might be a file status, transaction code, account type, return code, amount, date, or user-selected action.

IBM groups IF and EVALUATE as statements for selecting program actions. Use them with explicit scope terminators such as END-IF and END-EVALUATE so the next developer can see where each decision ends.

When to use IF

Use IF when the program has one condition or two simple choices. IBM describes IF ... ELSE as the normal form for choosing between two processing actions. The word THEN is optional in COBOL, but many teams omit it for a cleaner style.

IF WS-FILE-STATUS = '00'
   PERFORM PROCESS-CUSTOMER
ELSE
   PERFORM WRITE-FILE-ERROR
END-IF

This is direct and easy to test. The branch names also tell the reader what business action happens, not only what low-level condition was true.

When to use EVALUATE

Use EVALUATE when there are three or more choices. IBM describes EVALUATE as a way to avoid nested IF statements and to code a case structure or decision table.

EVALUATE WS-ACTION-CODE
   WHEN 'A'
      PERFORM ADD-CUSTOMER
   WHEN 'C'
      PERFORM CHANGE-CUSTOMER
   WHEN 'D'
      PERFORM DELETE-CUSTOMER
   WHEN OTHER
      PERFORM WRITE-INVALID-ACTION
END-EVALUATE

WHEN OTHER should handle values that do not match a known action. It is also a good place to write a clear message, set a return code, or route the record to an error report.

IF vs EVALUATE at a glance

Need Best COBOL statement Reason
One test with one action IF Smallest clear form.
Two alternate actions IF ... ELSE Shows both paths without extra structure.
Menu code, status code, or many values EVALUATE Avoids long nested IF blocks.
Several conditions together EVALUATE TRUE or carefully written IF Keeps rule order visible.

Using EVALUATE TRUE for business rules

EVALUATE TRUE is useful when each WHEN contains a full condition. This reads like a rule list. Put the most specific rules first so a broad rule does not catch the record too early.

EVALUATE TRUE
   WHEN WS-FILE-STATUS NOT = '00'
      PERFORM WRITE-FILE-ERROR
   WHEN WS-ACCOUNT-BALANCE < ZERO
      PERFORM ROUTE-CREDIT-REVIEW
   WHEN WS-CUSTOMER-TYPE = 'VIP'
      PERFORM PROCESS-VIP-CUSTOMER
   WHEN OTHER
      PERFORM PROCESS-STANDARD-CUSTOMER
END-EVALUATE

IBM notes that WHEN phrases are tested in source order. That order is part of the program logic, so treat it like business code rather than formatting.

Where PERFORM fits

PERFORM does not choose by itself. It runs a paragraph, section, or inline block after a choice has already been made. A clear decision block often calls short named paragraphs with PERFORM.

IF WS-INPUT-VALID
   PERFORM UPDATE-CUSTOMER
ELSE
   PERFORM PRINT-REJECT-DETAIL
END-IF

That style keeps the decision near the data test and moves longer processing into named routines. IBM also recommends structured programming statements such as EVALUATE and inline PERFORM because they make control flow easier to follow.

Avoid hidden period bugs

Old COBOL often used periods to end scope. Modern COBOL is easier to review when each decision uses explicit endings. A period in the wrong place can end more than the reader expects.

IF WS-FOUND
   PERFORM PRINT-DETAIL
END-IF

PERFORM READ-NEXT-RECORD

Use END-IF, END-EVALUATE, END-PERFORM, END-READ, and similar scope terminators. The compiler accepts older styles, but a support team needs code that can be read quickly during an abend call.

CONTINUE vs NEXT SENTENCE

CONTINUE is a no-operation statement. Control moves to the next statement after the current scope. NEXT SENTENCE jumps to the statement after the next period. IBM warns that the two can behave very differently depending on where the next period appears.

IF WS-SKIP-RECORD
   CONTINUE
ELSE
   PERFORM PROCESS-RECORD
END-IF

Prefer CONTINUE when you need an empty branch. Avoid NEXT SENTENCE in new code unless your site standard has a very specific reason for it.

Common mistakes

Nesting too many IF statements

Three or four nested IF statements are easy to misread. If the code checks a menu value, status code, or action code, rewrite it as EVALUATE.

Forgetting WHEN OTHER

A new action code can arrive from a file or screen before the program is ready for it. WHEN OTHER gives the program a controlled reject path instead of silent fall-through.

Mixing decisions and long processing

A decision block should show the choice. Long update logic, reporting logic, and error handling usually belong in named paragraphs called by PERFORM.

Review checklist

  • Use IF for one or two choices.
  • Use EVALUATE for action codes, file statuses, menu choices, and decision tables.
  • Code WHEN OTHER for unexpected values.
  • Use explicit scope terminators such as END-IF and END-EVALUATE.
  • Keep branch bodies short and call named paragraphs with PERFORM.
  • Prefer CONTINUE over NEXT SENTENCE for empty branches.

Related Mainframe Forum guides

For connected COBOL topics, read COBOL PERFORM Statement, COBOL IF THEN ELSE, COBOL Complex Conditions, COBOL COMPUTE Statement, COBOL EXIT Verb, and COBOL Application Structure.

External references

IBM documents this topic in coding a choice of actions, using the EVALUATE statement, conditional statements, and structured programming guidance.

FAQ

What is decision making in COBOL?

Decision making is the use of statements such as IF and EVALUATE to choose which processing path should run.

Should I use IF or EVALUATE in COBOL?

Use IF for one or two choices. Use EVALUATE when a program has three or more choices, such as action codes or file statuses.

Why is END-IF useful?

END-IF makes the scope of the condition clear. It helps prevent mistakes caused by misplaced periods or nested conditions.

Is NEXT SENTENCE the same as CONTINUE?

No. CONTINUE does nothing and moves to the next statement. NEXT SENTENCE jumps to the statement after the next period.

No comments:

Post a Comment

New In-feed ads