Easytrieve Nested Expressions

Nesting means operators inside operators—parentheses inside parentheses, subtraction inside multiplication, AND group inside OR, comparison wrapping arithmetic that itself contains addition. Easytrieve evaluates innermost grouped subexpressions first, then works outward until one final value or true-false decision remains. Nested expressions are legal and common in compact payroll formulas; they are also where precedence mistakes hide. A missing parenthesis pair changes who gets a bonus. An extra closing parenthesis fails compile. Beginners confuse nested expressions with nested IF blocks—the former groups math on one line; the latter stacks control flow across multiple lines. This page teaches nest depth, evaluation order inside nests, readable grouping strategies, when to flatten to work fields, and patterns from Broadcom training examples for tax, net pay, and exception routing.

Progress0 of 0 lessons

Nesting vs Nested IF

Two different nesting concepts
ConceptLooks likePurpose
Nested expressionNET = (A + B) * (C - D)Group operators in one formula
Nested IFIF A IF B DISPLAY X END-IF END-IFLayer conditional statement blocks
Nested PROC callsPERFORM outer calls innerModular logic—not expression nesting

Arithmetic Nesting

Arithmetic nesting groups terms before applying outer operators. NET = (GROSS - DEDUCT) * ADJ-RATE subtracts inside parens first, then multiplies by adjustment rate. Without parens, multiply might bind before subtract depending on operand positions—NET = GROSS - DEDUCT * ADJ-RATE may subtract DEDUCT * ADJ-RATE from GROSS if multiply binds tighter, which is different business math. Triple nest: RESULT = ((A + B) * C) / D adds A and B, multiplies by C, divides by D—each paren layer one evaluation step.

text
1
2
NET = (GROSS - FED-TAX - STATE-TAX) * FX-RATE HOLD = ((REG-HRS + OT-HRS) * RATE) + FLAT-AMT

Comparison Nesting with Arithmetic

IF (GROSS - TOTAL-DEDUCT) LT 0 nests subtraction before LT. IF (A + B) GT (C + D) nests arithmetic on both sides before compare. IF GROSS GT (LIMIT + BUFFER) adds buffer to limit inside nest before testing gross. Each open parenthesis demands matching close—editor highlighting helps; paper audits count pairs manually on critical payroll lines.

text
1
2
3
4
5
6
7
IF (GROSS - DEDUCT) LT 0 DISPLAY 'NEGATIVE NET' EMPL-NUM END-IF IF (REG-HRS + OT-HRS) GT (STD-HRS + OT-ALLOW) PERFORM OT-EXCEPTION END-IF

Logical Nesting

Logical nesting groups conditions before connecting with outer AND OR. IF (STATUS EQ A OR STATUS EQ P) AND GROSS GT LIMIT requires status match and gross threshold—parens force OR to bind before AND. IF A GT 0 AND (B GT 10 OR C GT 10) nests OR inside AND. NOT applies to immediately following condition or grouped unit per grammar—IF NOT (EOF) clearer than ambiguous NOT placement. Deep logical nesting mirrors spreadsheet boolean formulas—flatten when stakeholders must sign off line by line.

text
1
2
3
IF (DEPT EQ 100 OR DEPT EQ 200) AND GROSS GT 5000 PERFORM EXEC-BONUS END-IF

Evaluation Order Inside Nests

Innermost parentheses evaluate first. Within a group, exponentiation before multiply-divide before add-subtract. After arithmetic resolves, relational operators compare. Logical NOT then AND then OR bind last unless outer parens override. Trace IF A + B * C GT D + E: no parens means B * C first, A + product, D + E on right, then GT—different from IF (A + B) * (C GT D) + E which may be invalid or nonsense if C GT D is boolean in numeric context. Type rules still apply inside nests.

Depth and Readability Limits

Two paren levels on one line is normal. Three levels raises eyebrows in code review. Four or more signals refactor: assign each inner layer to WS-HOLD1, WS-HOLD2 with names reflecting business meaning TAXABLE-GROSS, ADJUSTED-TAX. Auditors tracing tax law want TAXABLE-GROSS = GROSS - PRETAX-DEDUCT visible on its own line, not buried in quintuple parens. Compiler accepts deep nests; humans do not.

text
1
2
3
4
5
6
7
8
9
10
* Dense nest (hard to audit): * IF ((GROSS - DED) * RATE) GT (LIMIT + BUF) AND STATUS EQ 'A' * Flattened equivalent: TAXABLE = GROSS - DED TAX-AMT = TAXABLE * RATE THRESH = LIMIT + BUF IF TAX-AMT GT THRESH AND STATUS EQ 'A' PERFORM TAX-TIER-2 END-IF

Nesting in Assignment Chains

Multiple assignments can build nested computation across lines without nesting parens on one line—preferred style in many shops. LINE1 = A + B. LINE2 = LINE1 * C. LINE3 = LINE2 - D. Equivalent to LINE3 = ((A + B) * C) - D as nested expression split for clarity. Choose based on shop standards and whether intermediate values need logging.

Nesting with ROUNDED and TRUNCATED

ROUNDED applies to assignment result storage—NET = (GROSS - TAX) * RATE ROUNDED rounds after full nested expression evaluates. Nesting ROUNDED inside parens as if it were operator is wrong syntax—keyword follows entire right side. When policy requires rounded intermediate tax before subtract, nest via separate assignment: TAX = GROSS * RATE ROUNDED; NET = GROSS - TAX.

Nesting in DO WHILE Conditions

DO WHILE (COUNT LT MAX) AND (GROSS GT 0) nests comparison groups inside AND. Condition re-evaluates each iteration—nested arithmetic inside condition recalculates when operands change. Keep DO WHILE nest shallow; complex nest belongs in IF inside loop body setting DONE flag.

Common Nested Expression Mistakes

  • Mismatched parenthesis count—compile error.
  • Assuming left-to-right inside unparenthesized nest.
  • Confusing nested IF with nested expression.
  • Five-level nest unmaintainable—refactor to holds.
  • Boolean compare result used in arithmetic nest illegally.
  • Copy-paste from spreadsheet without paren audit.

Explain It Like I'm Five

Nested expressions are math problems inside math problems. You solve the small problem in parentheses first, like figuring out how many cookies you and your friend have together, then multiply by price in the bigger problem. Nested IF is different—that is putting a smaller decision box inside a bigger decision box. Parentheses are the small boxes inside one sentence; nested IF is boxes inside boxes for whole instructions.

Exercises

  1. Evaluate ((2 + 3) * 4) - 1 by hand; confirm result 19.
  2. Flatten triple-nested IF arithmetic to three assignment lines.
  3. Add parens to change IF A + B * C GT 10 grouping.
  4. Count parenthesis pairs on a complex line from a manual.
  5. Write logical nest (A OR B) AND C with test values.

Quiz

Test Your Knowledge

1. NET = (GROSS - TAX) * RATE nests:

  • Subtraction inside multiplication group
  • FILE statement
  • Only THRU
  • JCL

2. IF (A GT 0 AND B GT 0) OR C GT 0 nests:

  • Logical AND group inside OR
  • Arithmetic only
  • Assignment
  • MACRO

3. Deeply nested expressions hurt:

  • Readability and maintenance
  • Compile speed only
  • JCL
  • FILE DCB

4. ((A + B) * C) - D has nesting depth:

  • Three levels of grouped subexpressions
  • Zero
  • One only
  • Invalid

5. Nested IF blocks differ from nested expressions because:

  • IF blocks structure statements; nested expressions group operators in one condition or formula
  • They are identical
  • Nested IF is invalid
  • Expressions cannot nest
Published
Read time15 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve 11.6 nested subexpression groupingSources: Broadcom Easytrieve 11.6 Language Reference expressionsApplies to: Easytrieve nested expressions