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.
| Concept | Looks like | Purpose |
|---|---|---|
| Nested expression | NET = (A + B) * (C - D) | Group operators in one formula |
| Nested IF | IF A IF B DISPLAY X END-IF END-IF | Layer conditional statement blocks |
| Nested PROC calls | PERFORM outer calls inner | Modular logic—not expression 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.
12NET = (GROSS - FED-TAX - STATE-TAX) * FX-RATE HOLD = ((REG-HRS + OT-HRS) * RATE) + FLAT-AMT
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.
1234567IF (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 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.
123IF (DEPT EQ 100 OR DEPT EQ 200) AND GROSS GT 5000 PERFORM EXEC-BONUS END-IF
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.
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.
12345678910* 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
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.
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.
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.
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.
1. NET = (GROSS - TAX) * RATE nests:
2. IF (A GT 0 AND B GT 0) OR C GT 0 nests:
3. Deeply nested expressions hurt:
4. ((A + B) * C) - D has nesting depth:
5. Nested IF blocks differ from nested expressions because: