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.

No comments:

Post a Comment