Derive the COBOL logic to implement a "Control Break" report (summing totals by…
- User
Derive the COBOL logic to implement a "Control Break" report (summing totals by Department) given a sorted sequential file.
Assistant
The user wants to know how to implement a "Control Break" report in COBOL. Specifically, the goal is to sum totals by Department, given a sorted sequential file.
1. Understand the Core Concept:
- Control Break: A technique used in report generation where processing changes when a specific field (the "control field," in this case, Department) changes value.
- Sorted Input: The file must be sorted by the control field for this logic to work.
- Goal: Sum a value (e.g., Salary, Sales, Hours) for each department and likely print a line when the department changes.
2. Identify the Key Components of the Logic:
- Initialization: Open files, prime the read (read the first record), initialize variables.
- The Main Loop: Process records until EOF.
- Saving the Control Key: Before reading the next record, save the current Department into a "Hold" variable (e.g.,
WS-DEPT-HOLD). - The Check: Compare the new Department (just read) with the saved Department (
WS-DEPT-HOLD).- If Same: It's the same department. Accumulate the total.
- If Different: A "Control Break" occurred.
- Print/Output the totals for the previous department.
- Reset the totals for the new department.
- Update
WS-DEPT-HOLDto the new Department.
- The Finalization: After the loop ends (EOF), we still have the totals for the last group in memory. We must print those before closing.
3. Structure the COBOL Code (Mental Draft):
Identification Division: Standard stuff. Environment Division: Input and Output files. Data Division: - Input Record layout (Dept, Amount, etc.). - Output Report line. - Working-Storage: -
WS-EOFflag. -WS-DEPT-HOLD(to store previous dept). -WS-DEPT-TOTAL(accumulator). -WS-GRAND-TOTAL(optional but good practice). Procedure Division: -OPEN INPUT InputFile OUTPUT ReportFile. -PERFORM UNTIL EOF. - Inside loop: - Check ifInput-Dept NOT = WS-DEPT-HOLD. - If different AND not the first record (tricky part, handling the start), performPRINT-TOTALS. - MoveInput-DepttoWS-DEPT-HOLD. - AddInput-AmounttoWS-DEPT-TOTAL. -READ InputFile.4. Refine the Logic (The "Standard" Pattern):
Pattern A: Read at top, Check at top.
- Prime read.
- Loop.
- If
New-Dept != Hold-Dept-> PerformCONTROL-BREAK. - Process detail.
- Read next.
Pattern B: Read at bottom, Check at top.
- Prime read.
- Move
New-DepttoHold-Dept. - Loop.
- Process detail.
- Read next.
- If
New-Dept != Hold-Dept-> PerformCONTROL-BREAK. -> MoveNew-DepttoHold-Dept.
Pattern C (Single Level Break - Most Common/Simple):
READfile.PERFORMuntil EOF.- Inside loop:
- Check for break (
Current-Dept != Saved-Dept). - If break:
PERFORM WRITE-CONTROL-TOTAL.MOVE Current-Dept TO Saved-Dept.MOVE 0 TO Dept-Total.
ADD Amount TO Dept-Total.READnext record.
- Check for break (
- At EOF:
PERFORM WRITE-CONTROL-TOTAL.
Wait, there is a subtlety: The very first record triggers a break if the "Saved" field is empty/spaces (assuming valid depts aren't spaces). This usually works fine. However, if the file is empty, we need to be careful.
Let's stick to the most robust logic: The "Check Before Process" method.
Step 1: Read first record. Step 2: Move Dept to
WS-HOLD-DEPT. Step 3: Loop: - Add amount to total. - Read next. - If EOF, Break Loop. - IfInput-Dept NOT = WS-HOLD-DEPT: - Write Report Line (Dept + Total). - Reset Total. - MoveInput-DepttoWS-HOLD-DEPT. Step 4: After loop, Write Report Line for the final group.Alternative (Standard Textbook approach): Many COBOL textbooks teach a specific flow where the Control Break logic handles the printing of the previous group.
Let's try to present the Standard COBOL Control Break Pattern:
- Initialization: Open files, read first record.
- Main Loop:
- Perform
PROCESS-RECORD. - Read next.
- Perform
- PROCESS-RECORD:
- Check if
Current-Dept != Hold-Dept.- If true (and not first time? or handle via flag):
PERFORM CONTROL-BREAK.
- If true (and not first time? or handle via flag):
- Move
Current-DepttoHold-Dept. - Add
AmounttoTotal.
- Check if
- CONTROL-BREAK:
- Print Total Line.
- Reset Total.
Actually, the most intuitive logic for a single level break is this:
- Prime Read.
MOVE Input-Dept TO WS-Hold-Dept.PERFORM UNTIL EOF.ADD Value TO WS-Dept-Total.READ File. (At end set EOF).IF NOT EOF AND Input-Dept NOT = WS-Hold-Dept:PERFORM WRITE-BREAK.MOVE Input-Dept TO WS-Hold-Dept.MOVE 0 TO WS-Dept-Total.
PERFORM WRITE-BREAK(for the final group).
Critique of the above: It relies on reading ahead. Reading ahead is standard in COBOL. Problem with the above: It prints the break before processing the next record's data? No, it prints the break after the current group is finished and the next record has been read (signaling the change). Wait, if I use
PERFORM UNTIL EOF, the loop terminates when EOF is true. The totals for the last record won't be printed unless I print them after the loop.Let's draft the code clearly.
Logic Draft:
-
Data:
WS-EOF(Pic 9),WS-DEPT-HOLD(Pic X),WS-DEPT-SUM(Pic 9). -
Start:
- Open files.
- Read InFile (At End Move 1 to WS-EOF).
- If WS-EOF = 0:
- Move Dept to WS-DEPT-HOLD.
- Perform
PROCESS-FILEuntil WS-EOF = 1. - Perform
WRITE-TOTALS(Catch the last group).
- Close files.
-
Paragraph
PROCESS-FILE:- Add Amount to WS-DEPT-SUM.
- Read InFile (At End Move 1 to WS-EOF).
- If WS-EOF = 0 AND InFile-Dept NOT = WS-DEPT-HOLD:
- Perform
WRITE-TOTALS. - Move InFile-Dept to WS-DEPT-HOLD.
- Move 0 to WS-DEPT-SUM.
- Perform
-
Paragraph
WRITE-TOTALS:- Write Report-Line from WS-DEPT-HOLD and WS-DEPT-SUM.
This logic is clean and covers the start, middle, and end correctly.
5. Formatting the Output: The user asked for the "Logic" to implement it. Providing the full COBOL code structure is best. I should explain the specific "Control Break" concept (the comparison against the hold field).
I will structure the response as follows:
- Conceptual Overview: Briefly explain the "Hold" variable and the comparison.
- Data Division: Define the necessary Working Storage variables (Hold field, Counters).
- Procedure Division: The actual COBOL code skeleton.
- Explanation: Step-by-step walkthrough of the code.
6. Refining the Code Example: I'll create a mock input file (
Employee-File) sorted byEmp-Dept. The goal is to sumEmp-Salary.Variables:
WS-Hold-Dept: Stores the department we are currently summing.WS-Dept-Total: The accumulator.WS-EOF: End of file flag.
Procedure Logic:
- 1000-MAIN-CONTROL.
- Perform
2000-INITIALIZE. - Perform
3000-PROCESS-FILEUNTILWS-EOF = 'Y'. - Perform
5000-TERMINATE.
- Perform
- 3000-PROCESS-FILE.
- This is tricky. If I read at the top of the loop, I need to check the break before processing? Or process then read?
- Standard approach:
PERFORM 4000-CHECK-BREAK. (Checks if new dept is different from hold).ADD Salary TO WS-Dept-Total.PERFORM 2100-READ-FILE.
- 2100-READ-FILE.
- Read. If EOF, set flag. Else, nothing.
- 4000-CHECK-BREAK.
- If
Current-Dept != WS-Hold-Dept:PERFORM 6000-WRITE-TOTAL.MOVE Current-Dept TO WS-Hold-Dept.MOVE 0 TO WS-Dept-Total.
- If
- Corner Case: The very first record.
WS-Hold-Deptis usually spaces or low-values.Current-Dept(first record) is likely not spaces.6000-WRITE-TOTALwill trigger immediately with a total of 0. This is okay if we handle it (don't print if it's the first record), or simpler:- Initialize
WS-Hold-DepttoLOW-VALUESor just accept that the check happens after the read? - Let's go with the "Read Ahead" or "Prime Read" approach detailed in point 4. It's cleaner for a single-level break.
Let's stick to the "Prime Read" + "Check at end of loop" or "Check after read".
The logic that is easiest to explain and robust:
- Open.
- Read (Prime).
- Move Dept to Hold.
- Loop until EOF.
- Add Amount to Total.
- Read (Next).
- IF NOT EOF AND Dept != Hold:
- Perform Print-Total.
- Reset Total.
- Move Dept to Hold.
- Perform Print-Total (for the last group).
- Close.
This is the most straightforward logic. I will write the COBOL based on this.
7. Code details:
- File Status or EOF indicator? I'll use an EOF indicator
WS-EOF-FLAGvalue 'Y' or 'N'. - Input:
EMP-FILE. - Output:
RPT-FILE.
Self-Correction during drafting: What if the file is empty?
- Prime read sets EOF.
- Loop doesn't run.
- Final Print-Total runs. We should check if the file was empty to avoid printing a blank line or header. I'll keep it simple but mention this assumption.
8. Final Review of the Request: "Derive the COBOL logic". I should provide the Pseudo-code/Logic description first, then the actual COBOL Code. This helps understanding.
Logic steps:
- Prime Read: Read the first record to get the initial Department.
- Set Control: Move that Department to a "Save" or "Hold" variable.
- Loop: Start the main loop.
- Accumulate: Add the value (e.g., Salary) to the Department Total.
- Read Next: Read the next record.
- Check Break: If the new Department is different from the "Hold" variable (or if EOF is reached):
- Print: Output the line for the "Hold" Department.
- Reset: Zero out the Total.
- Update: Move the new Department to the "Hold" variable (if not EOF).
- Finalize: After the loop, print the final total (since the loop terminates on EOF before the last print can happen in the middle of the logic).
Refining Step 6 (Logic flaw check): If I read next, and the Dept changes, I print the previous total. This works. However, I need to ensure I don't print a blank line if the file was empty initially. Also, standard COBOL structure usually separates the paragraphs.
Let's write the solution. Here is the derivation of the COBOL logic for a Control Break report.
The Conceptual Logic
To implement a control break in COBOL, you must maintain a "switch" or a "hold" variable that remembers the Department code of the record you are currently processing.
- Initialization: Open files, initialize accumulators, and perform a Priming Read (reading the first record before the loop starts) to populate your hold variable.
- The Loop: Start a
PERFORM UNTIL EOFloop. - Accumulation: Inside the loop, add the transaction amount to a Working-Storage accumulator (e.g.,
WS-DEPT-TOTAL). - The Read: Read the next record.
- The Check (Control Break): Immediately after reading, compare the new Department Code against the held Department Code.
- If they match: Do nothing (the loop repeats, accumulating the next record).
- If they differ (or EOF): You have reached the end of that department group. You must now print the total for the held department, reset the accumulator to zero, and update the hold variable to the new department code.
- Termination: Close files.
The COBOL Code Structure
Here is the complete logical skeleton. This assumes a sequential input file sorted by Department.
1. DATA DIVISION (Definitions)
You need a variable to store the "Current" Department key to compare it against the "New" record key.
WORKING-STORAGE SECTION. 01 WS-EOF-FLAG PIC X VALUE 'N'. 88 WS-EOF VALUE 'Y'. 01 WS-CONTROL-FIELDS. 03 WS-HOLD-DEPT PIC X(05). <-- The "Memory" variable 03 WS-DEPT-TOTAL PIC 9(07)V99 VALUE 0. 03 WS-GRAND-TOTAL PIC 9(09)V99 VALUE 0. 01 WS-REPORT-LINE. 03 FILLER PIC X(10) VALUE 'DEPT: '. 03 WS-OUT-DEPT PIC X(05). 03 FILLER PIC X(10) VALUE ' TOTAL: '. 03 WS-OUT-TOTAL PIC Z(06)9.99. PROCEDURE DIVISION.2. PROCEDURE DIVISION (Main Logic)
0000-MAIN. PERFORM 1000-INITIALIZE PERFORM 2000-PROCESS-RECORD UNTIL WS-EOF PERFORM 9000-TERMINATE STOP RUN. 1000-INITIALIZE. OPEN INPUT INPUT-FILE OPEN OUTPUT REPORT-FILE INITIALIZE WS-CONTROL-FIELDS *> PRIMING READ: *> We must read the first record manually to put a value *> into WS-HOLD-DEPT before the main loop starts. READ INPUT-FILE AT END SET WS-EOF TO TRUE END-READ IF NOT WS-EOF MOVE INPUT-DEPT-CODE TO WS-HOLD-DEPT END-IF. 2000-PROCESS-RECORD. *> 1. Accumulate the current record's data ADD INPUT-SALES-AMT TO WS-DEPT-TOTAL *> 2. Read the NEXT record (Look-ahead) READ INPUT-FILE AT END SET WS-EOF TO TRUE END-READ *> 3. CONTROL BREAK CHECK *> If the file ended OR the Department changed... IF WS-EOF OR INPUT-DEPT-CODE NOT = WS-HOLD-DEPT PERFORM 3000-PRINT-DEPT-TOTAL END-IF. *> 4. Update the Hold Variable for the next iteration IF NOT WS-EOF MOVE INPUT-DEPT-CODE TO WS-HOLD-DEPT END-IF. 3000-PRINT-DEPT-TOTAL. *> Write the total line for the group we just finished MOVE WS-HOLD-DEPT TO WS-OUT-DEPT MOVE WS-DEPT-TOTAL TO WS-OUT-TOTAL WRITE REPORT-LINE FROM WS-REPORT-LINE ADD WS-DEPT-TOTAL TO WS-GRAND-TOTAL *> Reset the accumulator for the next Department MOVE 0 TO WS-DEPT-TOTAL. 9000-TERMINATE. *> Optional: Write Grand Total here if required CLOSE INPUT-FILE REPORT-FILE.Key Logic Explanations
1. The Priming Read (In
1000-INITIALIZE) Without the priming read, thePERFORM UNTILloop would start with garbage data inWS-HOLD-DEPT. We read the first record to "prime the pump" so the comparison logic works immediately.2. The Look-Ahead Logic (In
2000-PROCESS-RECORD) The standard logic is:- Process (Add to Total)
- Read (Get Next)
- Compare (Did the key change?)
This is distinct from "Read then Process." By reading at the end of the iteration, you can detect the End of File or a Department change and trigger the output for the previous group exactly once.
3. The Final Total Because the
READsets the EOF flag before the logic checks if it needs to print, the last group's totals are automatically printed inside the loop (via theIF WS-EOFcheck). You do not need special code after the loop to print the final group, provided yourIFcondition checks forWS-EOF.