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.
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.
| Phrase | Meaning |
|---|---|
| AT END-OF-PAGE | Block runs when this WRITE causes the page to reach the footing (end-of-page) |
| NOT AT END-OF-PAGE | Optional block that runs when the WRITE does not cause end-of-page |
The WRITE statement can include AT END-OF-PAGE and optionally NOT AT END-OF-PAGE. The file must have LINAGE in its FD.
1234567WRITE 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.
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.
12345678910111213FD 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.
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.
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).”
1. When does the AT END-OF-PAGE block execute?
2. What must be present on the file if you use AT END-OF-PAGE?