Showing posts with label END-IF. Show all posts
Showing posts with label END-IF. Show all posts

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.

Sunday, 4 August 2013

Easytrieve IF Statement: Conditions, ELSE, and Examples

IF DEPT EQ 910 selects a record when DEPT contains 910. Easytrieve runs the true branch, skips the ELSE branch, and continues after END-IF. Conditions can compare fields and literals, test ranges, combine rules, and inspect file-processing status.

Easytrieve IF statement true and false decision flow
An Easytrieve IF test selects the true path or the ELSE path for each record.

Easytrieve IF statement syntax

A block IF begins with a condition and ends with END-IF. ELSE is optional. Indentation is not the language delimiter, but consistent indentation makes the selected paths visible during review.

IF condition
  statements-when-true
ELSE
  statements-when-false
END-IF

The IF logic normally sits inside an activity. Read the separate Easytrieve JOB statement guide for JOB INPUT, NAME, automatic input, INPUT NULL, and activity termination. The Easytrieve Plus tutorial explains the complete program layout.

Relational operators

MnemonicMeaningExample
EQ or =EqualIF DEPT EQ 910
NENot equalIF STATUS-CODE NE '00'
GT or >Greater thanIF GROSS GT 50000
GE or >=Greater than or equalIF AMOUNT GE 500
LT or <Less thanIF ITEM-COUNT LT 10
LE or <=Less than or equalIF CODE LE 100
Match the data definition: quote alphanumeric literals such as 'SMITH'. Define numeric and packed fields correctly before comparing numeric values. See Easytrieve field definitions for position, length, type, and decimal places.

Compare a field with a literal

The most common condition compares one input field with one literal. This example prints active employees from department 910:

IF DEPT EQ 910 AND EMP-STATUS EQ 'A'
  PRINT ACTIVE-RPT
END-IF

Within an IF, the equals sign is a comparison. Outside an IF, a statement such as NET-PAY = GROSS-PAY - DEDUCTIONS assigns a result. The surrounding statement determines the meaning.

Test several values

Easytrieve permits a list of acceptable values after an equality test. The condition is true when STATE matches one item:

IF STATE EQ 'GA' 'SC' 'TN'
  PRINT SOUTH-RPT
END-IF

Use a value list when every item has the same action. If individual states need different processing, use separate IF blocks or a CASE structure so each path is explicit.

Use THRU for a range

THRU expresses a range between two endpoint values. The old page used department and class ranges; a clearer version is:

IF CLASS EQ 'A' THRU 'E'
  RATE = 15
ELSE
  RATE = 18
END-IF

Broadcom examples also use numeric ranges such as IF CODE 101 THRU 200. Test boundary values at both ends, plus one value below and above, when a range controls pricing, routing, or file updates.

Combine conditions with AND and OR

AND requires both tests to be true. OR accepts either test. The following record must have the expected department and an acceptable status:

IF DEPT EQ 910
  IF EMP-STATUS EQ 'A' OR EMP-STATUS EQ 'L'
    PRINT PAYRPT
  END-IF
END-IF

Use continuation syntax required by your installed release and source format. For a long expression, nested IF statements can be easier to verify than a dense mixture of AND and OR. Broadcom's conversion example demonstrates repeated range tests inside nested ELSE branches.

IF, ELSE, and END-IF

ELSE identifies the false path. END-IF closes the condition and returns execution to the statements that follow it.

IF DIVISION EQ 'A' THRU 'L'
  DEDUCTIONS = GROSS * .15
ELSE
  DEDUCTIONS = GROSS * .18
END-IF
PRINT DEDUCT-RPT

PRINT runs after either branch because it follows END-IF. The Easytrieve report calculation guide covers arithmetic, working fields, totals, and SUM.

Nested IF statements

Each nested IF needs its own END-IF. Align each END-IF with the IF it closes:

IF CODE LE 100
  RECORD-LENGTH = 40
ELSE
  IF CODE LE 200
    RECORD-LENGTH = 60
  ELSE
    RECORD-LENGTH = 80
  END-IF
END-IF

A missing END-IF can attach an ELSE to the wrong condition or produce a compile error. Keep each branch short and move repeated work into a procedure when several levels are needed.

Test SPACE and ZERO correctly

Broadcom documents SPACE and ZERO as data-attribute tests. Do not place = or NE before these keywords:

IF CUSTOMER-ID SPACE
  DISPLAY 'MISSING CUSTOMER ID'
END-IF

IF AMOUNT NOT ZERO
  PRINT PAYMENT-RPT
END-IF
UseAvoid
IF FIELD-A SPACEIF FIELD-A = SPACE
IF FIELD-A NOT SPACEIF FIELD-A NE SPACE
IF FIELD-N ZEROIF FIELD-N = ZERO
IF FIELD-N NOT ZEROIF FIELD-N NE ZERO

Check FILE-STATUS with the file name

When an activity has more than one open file, qualify FILE-STATUS so Easytrieve knows which operation you are testing. Broadcom documents both forms for release 11.6:

READ VFILE KEY CUSTOMER-ID STATUS
IF VFILE:FILE-STATUS ZERO
  DISPLAY 'CUSTOMER FOUND'
ELSE
  DISPLAY 'READ FAILED, STATUS=' VFILE:FILE-STATUS
END-IF

The alternate form is FILE-STATUS(VFILE). An unqualified status can refer to the wrong file and allow an update path to continue after a failed read. See Easytrieve VSAM file handling for READ, PUT, UPDATE, and status examples.

Test EOF and record counts

Controlled-input logic can test EOF file-name after GET. Automatic input supplies the normal record loop, so first-record logic belongs in the JOB activity rather than a START procedure. Broadcom shows file-name:RECORD-COUNT EQ 1 for a first-record check when multiple files require qualification.

IF INPUT1:RECORD-COUNT EQ 1 AND REC-TYPE EQ 'H'
  DISPLAY 'HEADER RECORD RECEIVED'
END-IF

The Easytrieve basic reporting guide shows how record selection connects to REPORT and LINE output.

Conditions for duplicate keys

Keyed automatic input can expose conditions such as DUPLICATE, FIRST-DUP, and LAST-DUP. Broadcom's key-break example processes the first record in a duplicate group, or a record with no duplicate:

JOB INPUT (FILEA KEY(CODE))
IF FIRST-DUP FILEA OR NOT DUPLICATE FILEA
  READ MASTER KEY CODE STATUS
END-IF

Test sorted order and key definitions before relying on group conditions. See Easytrieve sorting for input-order considerations.

Complete record-selection example

FILE PERSNL FB(80 800)
  EMPNO       1  5 N
  EMPNAME     6 20 A
  DEPT       26  3 N
  STATUS     29  1 A
  GROSS      30  7 P 2

JOB INPUT PERSNL
  IF DEPT EQ 910 AND STATUS EQ 'A'
    IF GROSS GE 50000
      PRINT SENIOR-RPT
    ELSE
      PRINT STAFF-RPT
    END-IF
  END-IF

REPORT SENIOR-RPT LINESIZE 80
  TITLE 1 'DEPARTMENT 910 - HIGHER GROSS PAY'
  LINE EMPNO EMPNAME GROSS

REPORT STAFF-RPT LINESIZE 80
  TITLE 1 'DEPARTMENT 910 - OTHER ACTIVE STAFF'
  LINE EMPNO EMPNAME GROSS

This example separates the department/status selection from the pay branch. The structure makes it clear which END-IF closes each test and which report receives the record.

Common Easytrieve condition errors

ProblemCheck
ELSE follows the wrong IFIndent nested blocks and count every END-IF
Text comparison never matchesCheck field type, length, padding, and quoted literal
Range includes unexpected recordsTest both endpoints and confirm the field's collating/data type
SPACE or ZERO test fails to compileUse the attribute-test form without EQ or NE
EZTC0644E requests qualificationQualify FILE-STATUS when multiple files are open
Long AND/OR expression is hard to proveSplit it into nested, named, or separately tested conditions

Easytrieve IF statement checklist

  • Use one clear business decision per condition.
  • Quote alphanumeric literals and match numeric definitions.
  • Close every block IF with END-IF.
  • Test ELSE paths, range boundaries, spaces, zeros, and unexpected values.
  • Qualify FILE-STATUS and RECORD-COUNT when more than one file is involved.
  • Keep JOB activity control on the separate JOB statement URL.

Official Broadcom references

Easytrieve IF statement FAQ

How do you end an IF statement in Easytrieve?

End a block IF with END-IF. Statements before ELSE run when the condition is true; statements after ELSE run when it is false.

Can Easytrieve test several values in one IF?

Yes. A value list can follow the comparison, and THRU can represent a range. AND and OR combine separate conditions when the test needs more than one field.

How do you test for spaces or zero in Easytrieve?

Use the data-attribute forms IF field-name SPACE, IF field-name NOT SPACE, IF field-name ZERO, or IF field-name NOT ZERO. Broadcom advises against coding an equals sign before SPACE or ZERO.

Why must FILE-STATUS be qualified?

When an activity has multiple open files, Easytrieve 11.6 needs the file name to identify the correct status. Use file-name:FILE-STATUS or FILE-STATUS(file-name).

The quickest condition review is mechanical: identify the true path, identify the false path, and match every IF to its END-IF before testing the data.

New In-feed ads