MainframeMaster

COBOL Tutorial

COBOL LINE-COUNTER Special Register - Quick Reference

Progress0 of 0 lessons

Overview

The LINE-COUNTER special register is used in COBOL to track the current line number on a page during report generation. It works in conjunction with the LINAGE clause and LINAGE-COUNTER to provide automatic page formatting and line counting capabilities.

Purpose and Usage

  • Line tracking - Automatically tracks current line number on page
  • Page formatting - Works with LINAGE clause for page layout
  • Report generation - Essential for formatted report output
  • Automatic management - System-managed special register
  • Page breaks - Automatically resets when new page begins

LINE-COUNTER Concept

Page 1: LINE-COUNTER = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...
Page Break: LINE-COUNTER resets to 1
Page 2: LINE-COUNTER = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...
Automatic line counting within page boundaries

LINE-COUNTER automatically tracks line numbers and resets on page breaks.

Syntax and Usage

LINE-COUNTER is a special register that can be used directly in COBOL statements without declaration. It works with the LINAGE clause for proper page formatting.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
* LINE-COUNTER is used directly as a special register * No declaration needed - it's built into COBOL * Example usage in conditional statements IF LINE-COUNTER > 50 PERFORM WRITE-HEADER END-IF * Example usage in calculations COMPUTE REMAINING-LINES = LINAGE-COUNTER - LINE-COUNTER * Example usage in display statements DISPLAY "Current line: " LINE-COUNTER * Example usage with LINAGE clause FD REPORT-FILE LINAGE IS 60 LINES WITH FOOTING AT 55 LINES AT TOP 5 LINES AT BOTTOM 5.

LINE-COUNTER is a built-in special register that requires no declaration.

Complete Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
* Complete example using LINE-COUNTER IDENTIFICATION DIVISION. PROGRAM-ID. LINE-COUNTER-DEMO. 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 LINAGE IS 60 LINES WITH FOOTING AT 55 LINES AT TOP 5 LINES AT BOTTOM 5. 01 REPORT-LINE. 05 LINE-CONTENT PIC X(80). WORKING-STORAGE SECTION. 01 DATA-RECORD. 05 RECORD-ID PIC 9(6). 05 RECORD-NAME PIC X(30). 05 RECORD-VALUE PIC 9(8)V99. 01 PAGE-NUMBER PIC 9(3) VALUE 1. 01 REMAINING-LINES PIC 9(2). PROCEDURE DIVISION. MAIN-LOGIC. OPEN OUTPUT REPORT-FILE PERFORM WRITE-REPORT-HEADER PERFORM PROCESS-DATA CLOSE REPORT-FILE STOP RUN. WRITE-REPORT-HEADER. * Write header at top of page MOVE "REPORT HEADER - PAGE " TO LINE-CONTENT MOVE PAGE-NUMBER TO LINE-CONTENT(20:3) WRITE REPORT-LINE * LINE-COUNTER is now 1 MOVE "DATE: " TO LINE-CONTENT MOVE FUNCTION CURRENT-DATE TO LINE-CONTENT(7:8) WRITE REPORT-LINE * LINE-COUNTER is now 2. PROCESS-DATA. * Process data records PERFORM UNTIL END-OF-DATA READ DATA-FILE AT END SET END-OF-DATA TO TRUE END-READ * Check if we need a new page IF LINE-COUNTER > 50 PERFORM NEW-PAGE END-IF * Write data line PERFORM WRITE-DATA-LINE END-PERFORM. NEW-PAGE. * Start new page ADD 1 TO PAGE-NUMBER PERFORM WRITE-REPORT-HEADER * LINE-COUNTER is automatically reset to 1. WRITE-DATA-LINE. * Write data line MOVE RECORD-ID TO LINE-CONTENT(1:6) MOVE RECORD-NAME TO LINE-CONTENT(8:30) MOVE RECORD-VALUE TO LINE-CONTENT(40:10) WRITE REPORT-LINE * LINE-COUNTER is automatically incremented.

This example shows how LINE-COUNTER is used with LINAGE for page formatting.

LINAGE Clause Integration

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
* LINAGE clause with LINE-COUNTER FD REPORT-FILE LINAGE IS 60 LINES * Total lines per page WITH FOOTING AT 55 * Footer starts at line 55 LINES AT TOP 5 * Header area (lines 1-5) LINES AT BOTTOM 5. * Footer area (lines 56-60) * LINE-COUNTER behavior with this LINAGE: * - Starts at 1 for each page * - Increments with each WRITE statement * - Resets to 1 when page is full (line 60) * - Can be used to check current position * Example usage: IF LINE-COUNTER = 1 PERFORM WRITE-PAGE-HEADER END-IF IF LINE-COUNTER = 55 PERFORM WRITE-PAGE-FOOTER END-IF IF LINE-COUNTER > 50 PERFORM NEW-PAGE END-IF

The LINAGE clause defines page layout parameters that LINE-COUNTER works within.

Common Use Cases

LINE-COUNTER is essential in various scenarios where proper page formatting and line tracking are critical for report generation.

Report Generation

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
* Report generation with LINE-COUNTER IDENTIFICATION DIVISION. PROGRAM-ID. REPORT-GENERATOR. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT REPORT-FILE ASSIGN TO "SALES-REPORT.TXT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD REPORT-FILE LINAGE IS 66 LINES WITH FOOTING AT 60 LINES AT TOP 6 LINES AT BOTTOM 6. 01 REPORT-LINE. 05 LINE-CONTENT PIC X(132). WORKING-STORAGE SECTION. 01 SALES-RECORD. 05 SALES-DATE PIC 9(8). 05 SALES-AMOUNT PIC 9(8)V99. 05 SALES-REGION PIC X(20). 01 PAGE-NUMBER PIC 9(3) VALUE 1. 01 TOTAL-SALES PIC 9(10)V99 VALUE ZERO. PROCEDURE DIVISION. MAIN-LOGIC. OPEN OUTPUT REPORT-FILE PERFORM WRITE-REPORT-HEADER PERFORM PROCESS-SALES-DATA PERFORM WRITE-REPORT-FOOTER CLOSE REPORT-FILE STOP RUN. WRITE-REPORT-HEADER. * Write header only at top of page IF LINE-COUNTER = 1 MOVE "SALES REPORT" TO LINE-CONTENT WRITE REPORT-LINE MOVE "PAGE " TO LINE-CONTENT(1:5) MOVE PAGE-NUMBER TO LINE-CONTENT(6:3) WRITE REPORT-LINE MOVE SPACES TO LINE-CONTENT WRITE REPORT-LINE END-IF. PROCESS-SALES-DATA. * Process sales data with page breaks PERFORM UNTIL END-OF-SALES-DATA READ SALES-FILE AT END SET END-OF-SALES-DATA TO TRUE END-READ * Check if page is getting full IF LINE-COUNTER > 55 PERFORM NEW-PAGE END-IF * Write sales data PERFORM WRITE-SALES-LINE END-PERFORM. NEW-PAGE. * Start new page ADD 1 TO PAGE-NUMBER PERFORM WRITE-REPORT-HEADER. WRITE-SALES-LINE. * Write sales data line MOVE SALES-DATE TO LINE-CONTENT(1:8) MOVE SALES-AMOUNT TO LINE-CONTENT(10:10) MOVE SALES-REGION TO LINE-CONTENT(22:20) WRITE REPORT-LINE ADD SALES-AMOUNT TO TOTAL-SALES.

Report generation uses LINE-COUNTER for proper page formatting and headers.

Page Break Management

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
* Page break management with LINE-COUNTER IDENTIFICATION DIVISION. PROGRAM-ID. PAGE-BREAK-MANAGER. DATA DIVISION. WORKING-STORAGE SECTION. 01 CURRENT-LINE PIC 9(2). 01 LINES-PER-PAGE PIC 9(2) VALUE 60. 01 FOOTER-START PIC 9(2) VALUE 55. 01 HEADER-LINES PIC 9(2) VALUE 5. PROCEDURE DIVISION. MAIN-LOGIC. * Check current line position MOVE LINE-COUNTER TO CURRENT-LINE * Determine if page break is needed IF CURRENT-LINE >= FOOTER-START PERFORM FORCE-PAGE-BREAK END-IF * Check if we're at the top of a page IF CURRENT-LINE <= HEADER-LINES PERFORM WRITE-HEADER END-IF STOP RUN. FORCE-PAGE-BREAK. * Force a page break WRITE REPORT-LINE FROM SPACES * This will cause LINE-COUNTER to reset to 1 DISPLAY "Page break occurred, LINE-COUNTER reset to: " LINE-COUNTER. WRITE-HEADER. * Write header information DISPLAY "Writing header at line: " LINE-COUNTER * Write header content here.

Page break management uses LINE-COUNTER to determine when to start new pages.

Conditional Formatting

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
* Conditional formatting based on LINE-COUNTER IDENTIFICATION DIVISION. PROGRAM-ID. CONDITIONAL-FORMATTER. DATA DIVISION. WORKING-STORAGE SECTION. 01 FORMAT-TYPE PIC X(10). 01 LINE-POSITION PIC 9(2). PROCEDURE DIVISION. MAIN-LOGIC. * Get current line position MOVE LINE-COUNTER TO LINE-POSITION * Apply conditional formatting based on line position EVALUATE LINE-POSITION WHEN 1 MOVE "HEADER" TO FORMAT-TYPE PERFORM APPLY-HEADER-FORMAT WHEN 2 THRU 5 MOVE "SUBHEADER" TO FORMAT-TYPE PERFORM APPLY-SUBHEADER-FORMAT WHEN 55 THRU 60 MOVE "FOOTER" TO FORMAT-TYPE PERFORM APPLY-FOOTER-FORMAT WHEN OTHER MOVE "BODY" TO FORMAT-TYPE PERFORM APPLY-BODY-FORMAT END-EVALUATE STOP RUN. APPLY-HEADER-FORMAT. * Apply header formatting DISPLAY "Applying header format at line: " LINE-COUNTER. APPLY-SUBHEADER-FORMAT. * Apply subheader formatting DISPLAY "Applying subheader format at line: " LINE-COUNTER. APPLY-FOOTER-FORMAT. * Apply footer formatting DISPLAY "Applying footer format at line: " LINE-COUNTER. APPLY-BODY-FORMAT. * Apply body formatting DISPLAY "Applying body format at line: " LINE-COUNTER.

Conditional formatting uses LINE-COUNTER to apply different styles based on line position.

Best Practices and Tips

Following these best practices ensures effective use of LINE-COUNTER in COBOL applications.

LINE-COUNTER Best Practices

  • Use with LINAGE - Always use LINE-COUNTER with LINAGE clause for proper page formatting
  • Check before writing - Check LINE-COUNTER before writing to avoid page overflow
  • Plan page breaks - Plan page breaks before reaching the footer area
  • Don't modify directly - Never try to modify LINE-COUNTER directly
  • Test page boundaries - Test your program with different page sizes
  • Document assumptions - Document any assumptions about page layout

Common LINAGE Configurations

Page TypeLINAGE ConfigurationUse Case
Standard Report66 LINES, FOOTING AT 60Business reports
Compact Report50 LINES, FOOTING AT 45Dense data reports
Detailed Report80 LINES, FOOTING AT 75Detailed analysis reports
Letter Format60 LINES, FOOTING AT 55Letter-style documents
Invoice Format40 LINES, FOOTING AT 35Invoice and billing documents

Performance Considerations

  • Minimal overhead - LINE-COUNTER has minimal performance impact
  • Automatic management - System handles line counting automatically
  • Efficient page breaks - Page breaks are handled efficiently by the system
  • Memory usage - LINE-COUNTER uses minimal memory
  • I/O optimization - Proper use can optimize I/O operations
  • Testing - Test with large datasets to ensure performance

When to Use LINE-COUNTER

Use CaseLINE-COUNTER SuitabilityReasoning
Report generationExcellentEssential for proper page formatting
Page break managementExcellentAutomatic page break handling
Header/footer positioningExcellentPrecise positioning control
Data processingPoorNot designed for data processing
File operationsPoorNot designed for file operations

LINE-COUNTER Quick Reference

OperationSyntaxExample
Check current lineIF LINE-COUNTER conditionIF LINE-COUNTER > 50
Page break checkIF LINE-COUNTER >= footer-lineIF LINE-COUNTER >= 55
Header positioningIF LINE-COUNTER = 1IF LINE-COUNTER = 1
Footer positioningIF LINE-COUNTER = footer-lineIF LINE-COUNTER = 55
Display current lineDISPLAY LINE-COUNTERDISPLAY "Line: " LINE-COUNTER

Test Your Knowledge

1. What is the primary purpose of the LINE-COUNTER special register in COBOL?

  • To count characters in a string
  • To track line numbers for report generation and page formatting
  • To count records in a file
  • To track program execution steps

2. How is LINE-COUNTER typically used in COBOL programs?

  • As a data field in calculations
  • In conjunction with LINAGE and LINAGE-COUNTER for page formatting
  • To count file records
  • To track program performance

3. What happens to LINE-COUNTER when a page break occurs?

  • It continues counting from the previous value
  • It is automatically reset to 1
  • It stops counting
  • It becomes undefined

4. Which COBOL clause is most closely related to LINE-COUNTER?

  • OCCURS clause
  • LINAGE clause
  • REDEFINES clause
  • PICTURE clause

5. How do you access the current value of LINE-COUNTER in a COBOL program?

  • By declaring it as a data item
  • By using it directly as a special register
  • By calling a system function
  • By reading it from a file

Frequently Asked Questions