It's important to be aware of COBOL's reserved words and their usage as they can affect the way you write and understand COBOL programs, and can help ensure compatibility with COBOL compilers like GnuCOBOL. When using an IDE or compiler, remember that "LINE" will be recognized as a reserved word, which may affect variable naming and syntax highlighting.
This example demonstrates how to use LINE for positioning text on the screen. It clears the screen, displays a greeting and waits for user input to update the message, and then redisplays the updated content.
In COBOL, particularly when dealing with outputs like reports or screen displays, you might encounter both LINE and LINES, each serving different but related purposes. Here's a breakdown of their use:
LINE
-
Usage: Specifies a particular line on the screen or page.
-
Context: Often used in the SCREEN SECTION or REPORT SECTION.
-
Example: When you specify LINE in a SCREEN SECTION, you determine the precise line number on the display where the text or field should appear.
SCREEN SECTION.
01 MY-SCREEN.
05 LINE 5 COLUMN 10 VALUE "Hello, World!".
In this context, "Hello, World!" will appear specifically on line 5 of the screen.
LINES
-
Usage: Specifies the number of lines to advance or skip, typically used in a WRITE statement.
-
Context: Commonly found in the PROCEDURE DIVISION when controlling the output flow to the screen or a printer.
-
Example: When using LINES in a WRITE statement, it determines how many lines are advanced before or after writing.
PROCEDURE DIVISION.
MAIN-LOGIC.
WRITE REPORT-LINE AFTER ADVANCING 3 LINES.
Here, the output advances three lines down the page or screen before writing REPORT-LINE.
Differences
- LINE: Specifies the exact line position on the screen or page.
- LINES: Controls the amount of vertical movement (i.e., number of lines to skip) when writing output.
Example Demonstrating Both
Here's an example program illustrating the usage of LINE in a screen layout and LINES in a procedural context for output handling:
IDENTIFICATION DIVISION.
PROGRAM-ID. LineVsLinesExample.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-REPORT-LINE PIC X(50) VALUE "This is a report line.".
SCREEN SECTION.
01 MY-SCREEN.
05 LINE 3 COLUMN 10 VALUE "Welcome to the Report".
05 LINE 5 COLUMN 10 PIC X(50) VALUE "Report starts below".
PROCEDURE DIVISION.
MAIN-PROCEDURE.
DISPLAY MY-SCREEN.
WRITE WS-REPORT-LINE AFTER ADVANCING 3 LINES.
STOP RUN.
Explanation:
- Screen Setup with LINE: Text is positioned on specific lines (
LINE 3 and LINE 5).
- Output Control with LINES: The report line is output with
WRITE WS-REPORT-LINE AFTER ADVANCING 3 LINES, which means it skips three lines from the last output position before writing WS-REPORT-LINE.
Understanding this distinction helps in effectively managing text output in your COBOL applications, especially when precise layout and formatting are crucial.
In COBOL, the reserved word PHYSICAL is typically used within the context of file handling and report generation, particularly when dealing with page control in reports. It's part of a few statements or clauses concerning the physical aspects of output files or reports, such as physical pages as opposed to logical concepts.
Usage Context of PHYSICAL
-
Page Control in Reports:
- The
PHYSICAL keyword is often used to specify that an operation should be performed with respect to the physical pages rather than logical ones. For example, when handling reports, you might use PHYSICAL to refer explicitly to the physical end of a page.
-
WRITE Statement:
- In a report section or when writing to files,
AFTER ADVANCING PAGE or similar constructs can be complemented by PHYSICAL to ensure the operation respects the physical structure of the document (e.g., a new physical piece of paper is required).
Example
Here's a simple conceptual idea of how PHYSICAL might be used, particularly when creating a report that deals with physical page advancement:
IDENTIFICATION DIVISION.
PROGRAM-ID. PhysicalExample.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-REPORT-LINE PIC X(60) VALUE "This is a line in the report.".
FD REPORT-FILE.
01 REPORT-RECORD PIC X(60).
PROCEDURE DIVISION.
MAIN-PROCEDURE.
OPEN OUTPUT REPORT-FILE
PERFORM WRITING-REPORT LINES 3
CLOSE REPORT-FILE
STOP RUN.
WRITING-REPORT.
MOVE WS-REPORT-LINE TO REPORT-RECORD
WRITE REPORT-RECORD AFTER ADVANCING PHYSICAL PAGE.
Explanation
-
Output Operations: The WRITE statement with AFTER ADVANCING PHYSICAL PAGE explicitly instructs the program to advance to a new physical page before performing the write operation.
-
Physical vs. Logical Pages: The distinction between physical and logical pages is crucial in environments where a physical page can contain multiple logical pages (common in certain formatted reporting). Using PHYSICAL ensures that page breaks adhere to physical media constraints.
Conclusion
The PHYSICAL keyword is essential in COBOL when exact control over page formatting is required, particularly in printed reports or complex file outputs where the distinction between physical and logical document elements must be maintained. Understanding its role helps in precise document layout control, especially in traditional batch processing systems.
Certainly! Below is a detailed example of a COBOL program that uses a report file to demonstrate the use of the PHYSICAL keyword in the context of file and report handling. The program will create a report file and write lines with specific page advancements:
IDENTIFICATION DIVISION.
PROGRAM-ID. PhysicalPageReport.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT REPORT-FILE ASSIGN TO "report.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD REPORT-FILE.
01 REPORT-RECORD PIC X(80).
WORKING-STORAGE SECTION.
01 WS-HEADLINE PIC X(80) VALUE "---- Report Headline ----".
01 WS-DETAIL-LINE PIC X(80) VALUE "Detail Line: Page ".
01 WS-PAGE-NUMBER PIC 9(3) VALUE 1.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
OPEN OUTPUT REPORT-FILE
PERFORM GENERATE-REPORT
CLOSE REPORT-FILE
DISPLAY "Report generated successfully."
STOP RUN.
GENERATE-REPORT.
MOVE WS-HEADLINE TO REPORT-RECORD
WRITE REPORT-RECORD AFTER ADVANCING PHYSICAL PAGE
PERFORM WITH TEST AFTER
VARYING WS-PAGE-NUMBER FROM 1 BY 1
UNTIL WS-PAGE-NUMBER > 5
PERFORM WRITE-DETAIL-LINE
END-PERFORM.
WRITE-DETAIL-LINE.
MOVE WS-DETAIL-LINE TO REPORT-RECORD
STRING REPORT-RECORD DELIMITED BY SIZE
WS-PAGE-NUMBER DELIMITED BY SIZE
INTO REPORT-RECORD
END-STRING
WRITE REPORT-RECORD.
Explanation:
Output:
The program will generate a file named report.txt, containing the headline at the top of each physical page followed by several detail lines with incremental page numbers. Each new headline in the output starts on a new physical page.
This example demonstrates how COBOL can be used to generate structured reports with explicit control over page breaks, ensuring content is organized according to physical constraints. Remember that the behavior regarding physical pages might be more symbolic when working with text files than actual print media where page breaks have more physical meaning.