COBOL Tutorial

Progress0 of 0 lessons

COBOL AT END-OF-PAGE

The AT END-OF-PAGE phrase is used on the WRITE statement to run code when a write would cause the current logical page to reach its end (as defined by the LINAGE clause). It is the standard way to handle page breaks in COBOL reports: when the condition triggers, you typically perform a paragraph that advances to the next page and perhaps reprints headers. This page is a quick reference for the phrase; for full report examples see END-OF-PAGE.

What is the AT END-OF-PAGE phrase?

AT END-OF-PAGE is an optional conditional phrase on WRITE. When the file has a LINAGE clause, the run time tracks how many lines have been written on the current page. When a WRITE would cause the next line to fall in the footing area or beyond, the end-of-page condition is raised and the AT END-OF-PAGE block runs. You use it to print footers, advance to the next page, and optionally reprint headers.

AT END-OF-PAGE phrases
PhraseMeaning
AT END-OF-PAGEBlock runs when this WRITE causes the page to reach the footing (end-of-page)
NOT AT END-OF-PAGEOptional block that runs when the WRITE does not cause end-of-page

Syntax

The WRITE statement can include AT END-OF-PAGE and optionally NOT AT END-OF-PAGE. The file must have LINAGE in its FD.

cobol
1
2
3
4
5
6
7
WRITE record-name AT END-OF-PAGE PERFORM PAGE-BREAK-PROCESSING PERFORM WRITE-REPORT-HEADERS NOT AT END-OF-PAGE *> optional: do something when page is not full END-WRITE.

Requirement: LINAGE clause

If you use AT END-OF-PAGE, the file must have a LINAGE clause. LINAGE defines the logical page (e.g. how many lines per page, where the footing area starts). Without LINAGE, there is no notion of a page and no end-of-page condition.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
FD REPORT-FILE LINAGE IS 50 LINES WITH FOOTING AT 45 LINES AT TOP 5 LINES AT BOTTOM 3. 01 REPORT-LINE PIC X(132). *> Now WRITE ... AT END-OF-PAGE is valid WRITE REPORT-LINE AT END-OF-PAGE PERFORM PAGE-BREAK PERFORM WRITE-HEADERS END-WRITE.

Restriction: No ADVANCING PAGE with AT END-OF-PAGE

In a single WRITE statement you cannot use both ADVANCING PAGE and the AT END-OF-PAGE phrase. Do the page advance inside the AT END-OF-PAGE paragraph (e.g. WRITE a line AFTER ADVANCING PAGE) instead of on the same WRITE that has AT END-OF-PAGE.

Step-by-step: Using AT END-OF-PAGE

  • Define the report file with LINAGE (and optionally WITH FOOTING, LINES AT TOP, LINES AT BOTTOM).
  • On each WRITE to that file, add AT END-OF-PAGE followed by imperative statements (e.g. PERFORM paragraph).
  • In that paragraph, advance to the next page (e.g. WRITE ... AFTER ADVANCING PAGE) and, if needed, write headers for the new page.
  • Optionally use NOT AT END-OF-PAGE when you need different logic when the page is not full.

Explain like I'm five

Imagine a notepad with a fixed number of lines per page. When you write a line and that line would go past the last “body” line into the footer area, the notepad says “page full.” AT END-OF-PAGE is the rule that says: “When the page is full because of this write, do this (e.g. turn the page and print the title again).”

Test Your Knowledge

1. When does the AT END-OF-PAGE block execute?

  • At the start of every page
  • When a WRITE would reach or pass the footing line defined by LINAGE
  • Only when the file is closed
  • When the program ends

2. What must be present on the file if you use AT END-OF-PAGE?

  • FILE STATUS
  • LINAGE clause
  • RECORDING MODE
  • BLOCK CONTAINS

Related Concepts

Related Pages