COBOL Tutorial

Progress0 of 0 lessons

COBOL scope terminators

Scope terminators mark where a COBOL statement’s scope ends. The explicit form is END- followed by the statement name: END-IF, END-EVALUATE, END-PERFORM, END-READ, and so on. Using them makes blocks clear and avoids bugs that come from relying on a period (.) to end scope. This page explains how they work and when to use them.

What is scope?

Many COBOL statements have a “scope”: the range of code that belongs to that statement. An IF has a scope (the THEN part and optionally the ELSE part). A READ with AT END has a scope (the READ, the AT END block, and the NOT AT END block). That scope has to end somewhere. It can end at an explicit scope terminator (e.g. END-IF, END-READ) or at a period (.), which ends the current sentence and also closes every open scope.

Explicit scope terminators

Explicit scope terminators are reserved words that end exactly one kind of statement. They follow the pattern END-statement-name. The compiler knows that the block for that statement ends there, and nothing else is closed.

Common scope terminators
Statement / constructScope terminator
IF ... ELSE ... END-IFEND-IF
EVALUATE ... WHEN ... END-EVALUATEEND-EVALUATE
PERFORM ... END-PERFORMEND-PERFORM
READ ... AT END ... END-READEND-READ
WRITE ... END-WRITEEND-WRITE
ADD ... END-ADDEND-ADD
COMPUTE ... END-COMPUTEEND-COMPUTE
CALL ... END-CALLEND-CALL

Example: IF with END-IF

Without END-IF, the IF scope ends at the next period. That can make nested IFs or multiple statements after IF confusing. With END-IF, the boundary is explicit.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
*> Explicit: IF scope ends at END-IF IF WS-COUNT > 0 ADD 1 TO WS-TOTAL PERFORM UPDATE-RECORD ELSE MOVE ZEROS TO WS-TOTAL END-IF. *> Next statement is outside the IF *> Risky: period ends the IF here; easy to misplace IF WS-COUNT > 0 ADD 1 TO WS-TOTAL. *> This MOVE runs even when WS-COUNT is not > 0 (it is after the period) MOVE 1 TO WS-OTHER.

In the first block, END-IF clearly ends the IF. In the second, the period after WS-TOTAL ends the IF, so the next MOVE is always executed. Explicit terminators help you avoid that kind of mistake.

Example: READ with END-READ

READ can have AT END and NOT AT END. The scope of the READ (including those clauses) ends at END-READ. That keeps the next statement clearly outside the READ.

cobol
1
2
3
4
5
6
7
READ INFILE AT END SET WS-EOF TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ. *> Execution continues here after READ (and possibly PROCESS-RECORD)

Implicit termination: the period

A period (.) ends the current sentence. In standard COBOL it also terminates every open scope (every unterminated IF, EVALUATE, PERFORM, READ, etc.). So one period can close several blocks at once. That is “implicit” termination. It is legal but easy to get wrong when you have nested blocks. Prefer explicit END-IF, END-EVALUATE, END-PERFORM, END-READ, and so on, and use the period mainly to end a paragraph or a standalone sentence.

Step-by-step: using scope terminators

  • For every IF, write a matching END-IF at the end of the IF/ELSE block.
  • For every EVALUATE, write END-EVALUATE after the last WHEN or OTHERWISE.
  • For in-line PERFORM (PERFORM ... END-PERFORM), write END-PERFORM after the last statement in the loop.
  • For READ/WRITE/REWRITE with exception clauses, use END-READ, END-WRITE, END-REWRITE so the next statement is clearly outside the I/O.
  • Keep periods for ending paragraphs or single sentences that are not inside a block.

Explain like I'm five

Scope terminators are like closing brackets: they say “this block stops here.” END-IF means “the IF block stops here.” Without them, the only “close” is a period, which can close more than you meant. Using END-IF and the others is like putting the right lid on the right box.

Test Your Knowledge

1. What does END-IF do?

  • Starts an IF statement
  • Marks the end of the IF statement’s scope
  • Marks the end of the program
  • Marks the end of a paragraph

2. Why are explicit scope terminators preferred over a period?

  • They are faster
  • They make scope clear and avoid accidentally ending nested blocks
  • They are required in all COBOL versions
  • They reduce program size

Related concepts

Related Pages