The IF statement reference page teaches syntax—conditional expressions, END-IF pairing, field class tests. This chapter page teaches what beginners do with IF inside JOB: skip deleted employees, route exceptions to error files, accumulate audit counters, branch on DUPLICATE after sort, and pair SEARCH presence tests with default MOVE paths. Batch reporting is a stream of records; IF is how you stop treating every row identically. ELSE-IF chains express grade bands and status families without nested towers; nested IF expresses decisions that only matter when an outer condition already succeeded. GOTO JOB after IF remains a pragmatic bypass when remaining statements should not run for the current record. Master IF control flow and your programs become readable maintenance assets instead of copy-pasted JOB blocks.
12345678910JOB INPUT PAYROLL NAME UPDATE-RUN IF STATUS EQ 'D' GOTO JOB END-IF IF GROSS-PAY LT 0 ADD 1 TO NEG-CNT WRITE ERRFILE GOTO JOB END-IF PRINT DETAIL-RPT
Each GET loads PAYROLL fields into the file buffer. First IF bypasses deleted rows entirely. Second IF tallies negative pay and writes an error extract without printing detail. GOTO JOB jumps to the next input iteration—skipping PRINT for bad rows. Both IF blocks need END-IF; GOTO does not replace END-IF.
123456789IF REGION EQ '10' MOVE 'EAST' TO WS-REGION-NAME ELSE-IF REGION EQ '20' MOVE 'WEST' TO WS-REGION-NAME ELSE-IF REGION EQ '30' MOVE 'CENTRAL' TO WS-REGION-NAME ELSE MOVE 'UNKNOWN' TO WS-REGION-NAME END-IF
Mutually exclusive region codes map to names in one flat structure. Only one branch runs. ELSE catches unlisted codes. Contrast with three separate IF statements—which could overwrite WS-REGION-NAME if multiple conditions were ever true. ELSE-IF implies prior conditions failed.
Use nesting when inner logic applies only if outer is true: IF DEPT EQ 903 then IF GROSS GT LIMIT then FLAG-HIGH = Y. Outer false skips inner entirely. Each IF needs END-IF—two END-IF for two levels. Deep nesting beyond three levels signals time to extract a procedure or use ELSE-IF if conditions are actually parallel.
| Condition | Typical batch use |
|---|---|
| Field relational (AMT GT 1000) | Threshold edits and routing |
| IF EOF file | After GET loops and manual reads |
| IF DUPLICATE key | Post-sort duplicate detection |
| IF MATCHED sync file | Synchronized file processing |
| IF TABLENAME | After SEARCH success |
| IF NOT TABLENAME | After SEARCH miss—assign default |
1234567SEARCH DEPTTAB WITH DEPT GIVING WS-DEPT-NAME IF DEPTTAB PRINT DETAIL-RPT ELSE ADD 1 TO BAD-DEPT-CNT WRITE ERR-DEPT END-IF
Valid department prints; invalid routes to error file with tally. IF DEPTTAB is presence test, not field comparison. Beginners who PRINT without IF risk stale GIVING from prior row.
BEFORE-LINE and screen procedures use IF for formatting guards—print bonus line only IF BONUS GT 0. Procedure IF follows same END-IF rules. Restricted contexts (sort BEFORE) forbid some statements inside IF bodies—verify prescreen allowed list before coding GET inside IF in sort procedures.
IF runs a block zero or one time per evaluation. DO repeats while or until condition changes. Walking an array uses DO with IF inside each pass. Do not wrap a single PRINT in DO—use IF when repetition is unnecessary.
Whichever branch executed—IF, ELSE-IF, or ELSE—control always continues at the first statement after END-IF unless GOTO, EXIT, or STOP redirected. Branches do not fall through into each other. Indentation in source is for humans; only END-IF delimits structure for the compiler.
IF is a question at each desk in a classroom. If the kid raised a hand, you give them a sticker. ELSE-IF is the next question only if the first answer was no: if not red, is it blue? ELSE is everyone left gets a plain pencil. END-IF is putting the sticker box away before the class moves to recess. GOTO JOB is telling one kid to skip show-and-tell and line up for lunch while the rest of the class keeps going.
1. In a JOB loop, IF PAY-STATUS EQ 9 typically:
2. ELSE-IF is preferable to nested IF when:
3. IF EOF PERSNL after GET means:
4. IF without ELSE and false condition:
5. GOTO JOB after IF on bad record: