The EJECT clause is a standalone statement used to send a form feed character to the output device, causing a page break or new page. It is commonly used for printer control and report formatting to ensure proper page separation.
EJECT sends a form feed character to cause a page break.
The EJECT clause follows a simple syntax pattern and is used as a standalone statement in the PROCEDURE DIVISION.
123456789101112131415161718* Basic EJECT syntax EJECT * In context with other statements DISPLAY "End of page content" EJECT DISPLAY "Start of new page content" * With conditional logic IF PAGE-NUMBER > 50 EJECT MOVE 1 TO PAGE-NUMBER END-IF * In report generation DISPLAY "Report Header" EJECT DISPLAY "Report Body"
EJECT is a simple standalone statement that requires no parameters.
Feature | EJECT | ADVANCING PAGE |
---|---|---|
Usage | Standalone statement | Clause with WRITE |
Context | General output control | File writing operations |
Effect | Immediate page break | Page break with data |
Flexibility | Simple and direct | Integrated with file I/O |
123456789* EJECT sends form feed character * ASCII: 12 (decimal) * Hex: 0C * Control character: FF (Form Feed) * Equivalent operations: EJECT * COBOL EJECT statement DISPLAY X"0C" UPON PRINTER * Manual form feed WRITE RECORD AFTER ADVANCING PAGE * With WRITE
EJECT sends a form feed character (0x0C) to the output device.
These examples demonstrate how to use the EJECT clause effectively in different output scenarios.
123456789101112131415161718192021222324252627282930313233343536373839404142IDENTIFICATION DIVISION. PROGRAM-ID. BASIC-REPORT-EJECT. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-PAGE-NUMBER PIC 9(3) VALUE 1. 01 WS-LINE-COUNT PIC 9(2) VALUE 0. 01 WS-MAX-LINES PIC 9(2) VALUE 60. PROCEDURE DIVISION. MAIN-PROCESS. * Print report header DISPLAY "SALES REPORT" UPON PRINTER DISPLAY "Page: " WS-PAGE-NUMBER UPON PRINTER DISPLAY "Date: " FUNCTION CURRENT-DATE UPON PRINTER DISPLAY " " UPON PRINTER * Print report content PERFORM PRINT-REPORT-CONTENT * Eject to new page for summary EJECT DISPLAY "REPORT SUMMARY" UPON PRINTER DISPLAY "Total pages: " WS-PAGE-NUMBER UPON PRINTER STOP RUN. PRINT-REPORT-CONTENT. * Simulate printing report lines PERFORM VARYING WS-LINE-COUNT FROM 1 BY 1 UNTIL WS-LINE-COUNT > 50 DISPLAY "Line " WS-LINE-COUNT " content" UPON PRINTER * Check if page is full IF WS-LINE-COUNT = WS-MAX-LINES EJECT ADD 1 TO WS-PAGE-NUMBER MOVE 0 TO WS-LINE-COUNT DISPLAY "Page: " WS-PAGE-NUMBER UPON PRINTER END-IF END-PERFORM.
Basic report generation using EJECT for page breaks and formatting.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556IDENTIFICATION DIVISION. PROGRAM-ID. MULTI-SECTION-REPORT. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-SECTION-COUNT PIC 9(2) VALUE 0. PROCEDURE DIVISION. MAIN-PROCESS. * Print title page DISPLAY "COMPANY ANNUAL REPORT" UPON PRINTER DISPLAY "Year: 2024" UPON PRINTER DISPLAY " " UPON PRINTER DISPLAY "Prepared by: Finance Department" UPON PRINTER * Eject to start first section EJECT * Print financial summary section PERFORM PRINT-FINANCIAL-SUMMARY * Eject to start second section EJECT * Print operational summary section PERFORM PRINT-OPERATIONAL-SUMMARY * Eject to start third section EJECT * Print conclusions section PERFORM PRINT-CONCLUSIONS STOP RUN. PRINT-FINANCIAL-SUMMARY. DISPLAY "FINANCIAL SUMMARY" UPON PRINTER DISPLAY "==================" UPON PRINTER DISPLAY "Revenue: $1,000,000" UPON PRINTER DISPLAY "Expenses: $750,000" UPON PRINTER DISPLAY "Net Profit: $250,000" UPON PRINTER. PRINT-OPERATIONAL-SUMMARY. DISPLAY "OPERATIONAL SUMMARY" UPON PRINTER DISPLAY "===================" UPON PRINTER DISPLAY "Employees: 150" UPON PRINTER DISPLAY "Locations: 5" UPON PRINTER DISPLAY "Projects: 25" UPON PRINTER. PRINT-CONCLUSIONS. DISPLAY "CONCLUSIONS" UPON PRINTER DISPLAY "===========" UPON PRINTER DISPLAY "The company performed well in 2024." UPON PRINTER DISPLAY "Recommendations for 2025:" UPON PRINTER DISPLAY "- Expand operations" UPON PRINTER DISPLAY "- Increase marketing budget" UPON PRINTER.
Multi-section report using EJECT to separate different report sections.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051IDENTIFICATION DIVISION. PROGRAM-ID. CONDITIONAL-EJECT. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-LINE-COUNT PIC 9(3) VALUE 0. 01 WS-PAGE-NUMBER PIC 9(3) VALUE 1. 01 WS-MAX-LINES PIC 9(3) VALUE 55. 01 WS-RECORD-COUNT PIC 9(5) VALUE 0. PROCEDURE DIVISION. MAIN-PROCESS. * Print header PERFORM PRINT-PAGE-HEADER * Process records PERFORM PROCESS-RECORDS UNTIL WS-RECORD-COUNT > 1000 STOP RUN. PRINT-PAGE-HEADER. DISPLAY "CUSTOMER LISTING" UPON PRINTER DISPLAY "Page: " WS-PAGE-NUMBER UPON PRINTER DISPLAY "Date: " FUNCTION CURRENT-DATE UPON PRINTER DISPLAY " " UPON PRINTER DISPLAY "Customer ID | Name | Balance" UPON PRINTER DISPLAY "------------|------|--------" UPON PRINTER MOVE 0 TO WS-LINE-COUNT. PROCESS-RECORDS. ADD 1 TO WS-RECORD-COUNT ADD 1 TO WS-LINE-COUNT * Print record (simulated) DISPLAY "CUST" WS-RECORD-COUNT " | Customer " WS-RECORD-COUNT " | $1000" UPON PRINTER * Check if page is full IF WS-LINE-COUNT >= WS-MAX-LINES EJECT ADD 1 TO WS-PAGE-NUMBER PERFORM PRINT-PAGE-HEADER END-IF * Check if we need a new page for new section IF WS-RECORD-COUNT = 500 EJECT DISPLAY "MID-YEAR SUMMARY" UPON PRINTER DISPLAY "Records processed: " WS-RECORD-COUNT UPON PRINTER EJECT PERFORM PRINT-PAGE-HEADER END-IF.
Conditional page ejection based on line count and record processing.
12345678910111213141516171819202122232425262728293031323334353637383940414243IDENTIFICATION DIVISION. PROGRAM-ID. ERROR-HANDLING-EJECT. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-ERROR-COUNT PIC 9(3) VALUE 0. 01 WS-SUCCESS-COUNT PIC 9(3) VALUE 0. 01 WS-PAGE-NUMBER PIC 9(3) VALUE 1. PROCEDURE DIVISION. MAIN-PROCESS. * Print success report DISPLAY "SUCCESS REPORT" UPON PRINTER DISPLAY "Page: " WS-PAGE-NUMBER UPON PRINTER PERFORM PRINT-SUCCESS-DETAILS * Eject to error section if errors exist IF WS-ERROR-COUNT > 0 EJECT DISPLAY "ERROR REPORT" UPON PRINTER DISPLAY "Page: " WS-PAGE-NUMBER UPON PRINTER PERFORM PRINT-ERROR-DETAILS END-IF * Eject to summary page EJECT DISPLAY "PROCESSING SUMMARY" UPON PRINTER DISPLAY "Successful operations: " WS-SUCCESS-COUNT UPON PRINTER DISPLAY "Errors encountered: " WS-ERROR-COUNT UPON PRINTER STOP RUN. PRINT-SUCCESS-DETAILS. DISPLAY "Successful operations:" UPON PRINTER DISPLAY "Operation 1: Completed" UPON PRINTER DISPLAY "Operation 2: Completed" UPON PRINTER ADD 2 TO WS-SUCCESS-COUNT. PRINT-ERROR-DETAILS. DISPLAY "Error details:" UPON PRINTER DISPLAY "Error 1: File not found" UPON PRINTER DISPLAY "Error 2: Invalid data" UPON PRINTER ADD 2 TO WS-ERROR-COUNT.
Error handling using EJECT to separate success and error reports.
Understanding best practices ensures proper page control and report formatting.
Pitfall | Problem | Solution |
---|---|---|
Excessive ejection | Too many blank pages | Use line counting |
Wrong device | No effect on non-page devices | Check device capabilities |
Poor timing | Breaks in wrong places | Plan page breaks carefully |
Missing headers | Pages without identification | Print headers after EJECT |
Inconsistent usage | Poor report formatting | Use consistent page control |
1234567891011121314* Printer devices (most compatible) EJECT * Works with most printers * Terminal/console devices EJECT * May clear screen or have no effect * File output EJECT * May insert form feed character in file * Network printers EJECT * Depends on printer driver * Virtual devices EJECT * Behavior varies by implementation
EJECT behavior varies depending on the output device and implementation.
Usage | Syntax | Example |
---|---|---|
Basic page break | EJECT | EJECT |
After content | DISPLAY content; EJECT | DISPLAY "End"; EJECT |
Conditional | IF condition EJECT END-IF | IF LINE-COUNT > 50 EJECT END-IF |
Section separator | EJECT; DISPLAY new-section | EJECT; DISPLAY "NEW SECTION" |
Report formatting | EJECT; PRINT-HEADER | EJECT; PERFORM PRINT-HEADER |
1. What is the primary purpose of the EJECT clause in COBOL?
2. Where is the EJECT clause typically used?
3. What happens when EJECT is executed?
4. Which output devices typically respond to EJECT?
5. How does EJECT differ from ADVANCING PAGE?
ADVANCING PAGE clause for page control with WRITE statements.
WRITE statement for file output operations.
Report writing techniques and formatting.
Output control and data movement operations.
Printer control and file processing techniques.